Applet Mouse Event MouseAdapter Example


We have seen earlier a similar program Handling Mouse Events with MouseAdapter Example with an introduction to MouseAdapter, but the window here is frame. Let us do another program with applet window.

The following program displays a message "Mouse is clicked" in the applet window and "MouseMotionAdapter is in action" at status bar when mouse is clicked in the applet window.

For a clear adapters notes refer Java AWT Adapters.

HTML file to run the applet



import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class AppletMouseAdapterDemo extends Applet
{
  String str="";
  public void init()
  {
    PleaseHelp ph = new PleaseHelp(this);
    addMouseListener(ph);
  }

  public void paint(Graphics g)
  {
    g.drawString(str, 50, 100);
    showStatus("MouseMotionAdapter is in action");
  }
}

class PleaseHelp extends MouseAdapter
{
  AppletMouseAdapterDemo amad2;
  public PleaseHelp(AppletMouseAdapterDemo amad1)
  {
    amad2 = amad1;
  }
  public void mouseClicked(MouseEvent e)
  {
    amad2.str = "Mouse is clicked";
    amad2.setForeground(Color.red);
    amad2.repaint();
  }
}

One advantage of adapters is it eliminates to override all the abstract methods of the corresponding listener. One disadvantage, for a novice, is it forces to write one more new class (PleaseHelp) apart the main program (AppletMouseAdapterDemo). The reason is Java does not support multiple inheritance.

MouseAdapter, being an abstract class, should be inherited with extends keyword. But the main program AppletMouseAdapterDemo is already extending the Applet. For this reason, a separate custom-defined class, PleaseHelp, is created and extended MouseAdapter.

Okay, it is right. But the question is how to link these two custom-defined classes where both are not involved in inheritance? Here is a small logic which every novice should try to understand. See the following discussion.

PleaseHelp ph = new PleaseHelp(this);
addMouseListener(ph);

Whole logic starts here only. "this" parameter of PleaseHelp constructor implicitly refers a current object of AppletMouseAdapterDemo (that extends Frame) as PleaseHelp constructor requires an object of AppletMouseAdapterDemo. Observe the PleaseHelp class.

AppletMouseAdapterDemo amad2;
public PleaseHelp(AppletMouseAdapterDemo amad1)
{
amad2 = amad1;
}

AppletMouseAdapterDemo amad1 refers the main class (in PleaseHelp class) and indirectly the Frame. To use amad1 in other part of the program (now its scope is in constructor), another object amad2 is created assigned with amad1.

amad2.setForeground(Color.red);
amad2.repaint();

As amad2 refers a frame, the Frame class methods (like setForeground()) can be used with.

The repaint() method calls implicitly paint() method. Do not call paint() directly, it raises error while compilation.

Output screen when mouse is clicked on applet window.

Applet Mouse Event MouseAdapter Example

Leave a Comment

Your email address will not be published.