ST: Java Programming

Homework: Listen!

See if you can split this class up into two classes. This simple example illustartes a MouseListener that listens to itself. Take out the implements part, and associated statements, and move them into another class, maybe called MyMouseListener. Then add an instance of this class as a listener to the applet.

You may have difficulty with the showStatus call, so comment it out and use the applet viewer to test your program. In this environment, the System.out will display in the DOS box as the applet runs.

-----start here-----
import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class MouseTales extends Applet implements MouseListener{
 public void init(){
  setBackground(Color.lightGray);
  addMouseListener(this);
 }

 public void mousePressed(MouseEvent e){
  messageOut("mousePressed  ",e);
 }

 public void mouseReleased(MouseEvent e){
  messageOut("mouseReleased  ",e);
 }

 public void mouseEntered(MouseEvent e){
  messageOut("mouseEnter ",e);
 }

 public void mouseExited(MouseEvent e){
  messageOut("mouseExited  ",e);
 }

 public void mouseClicked(MouseEvent e){
  messageOut("mouseClicked  ",e);
 }
 

 void messageOut(String m, MouseEvent e){
  m = m + "(" + e.getX() + ", " + e.getY() + "}";
  System.out.println(m);
  showStatus(m);
 }

}
-----end here-----
Here is the applet tag to put in a HTML file:

<applet code="MouseTales" width=350 height=350>
</applet>