Would you like to make this site your homepage? It's fast and easy...
Yes, Please make this my home page!
Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto,
2002.
[Contents] [Next] [Previous]
Lab 3: Building Applets - Graphical representation
Handling MouseEvent
A MouseEvent is actually also an object in Java. In order to handle
a MouseEvent, we should define a Class that at least implements
MouseListener which is a sub class of the interface EventListener and add
that class using addMouseListener(MouseListener) method in the
Applet (or other Component) to handle MouseEvent occurs in the Applet (or Component).
Here are two types of Mouse related Listener interfaces that can be used:
- MouseListener will enable implementation of mouse actions such as:
mousePressed, mouseReleased, mouseEntered, mouseExited
and mouseClicked.
- MouseMotionListener will enable implementation of mouse actions such
as: mouseMoved, and mouseDragged.
In this Lab, we would like to handle MouseEvents such as mousePressed,
and mouseDragged. Thus, a MouseListener interface will not be enough.
We need both of the interfaces. But still, all other methods listed in the interface
should be implemented. Since we don't use them, we simply create a blank method
for them.
Here are the steps of coding the MouseEvent handler:
- Add both MouseListener and MouseMotion Listener interface
implementation declaration in the VisualMobileSystem Class declaration
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class VisualMobileSystem extends Applet implements MouseListener, MouseMotionListener
|
- Create a blank method for all unused Events
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) { }
|
- Define the mousePressed(MouseEvent) method.
Whenever the mouse is pressed, the TextArea will be cleared and the mobile
station will be moved to the location given by the mouse.
We will define a moveMobileStation(x,y) method later to handle all
the necessary processes when a Mobile Station moved.
public void mousePressed(MouseEvent e) { textarea.setText(""); //Clear the content of textarea int x = e.getX(); int y = e.getY(); moveMobileStation(x,y); //call method to set new mobilestation location
}
|
- Define the mouseDragged(MouseEvent) method.
The same as mousePressed(MouseEvent) method, we would like the MobileStation
to move to the location indicated by the mouse.
But the difference is, we don't want the TextArea to be cleared so that we
have all the history of the movement.
public void mouseDragged(MouseEvent e) { int x = e.getX(); int y = e.getY(); moveMobileStation(x,y); //call method to set new mobilestation location
}
|
- Define the moveMobileStation(x,y) method.
Besides setting the new location for the MobileStation and try to serve the
MobileStation, we would like to refresh the Graphics after adding some information
into the TextArea. The statement below is very straight forward in performing
the operation.
private void moveMobileStation(int x, int y)
{ ms.setLocation(x,y); serveMobileStation(ms,vodaPhone); updateStatus(ms);
repaint(); }
private static void serveMobileStation(MobileStation ms, MobileSystem msc) { Rbs servingRbs = msc.findRbs(ms.getX(),ms.getY()); if ( servingRbs != null) ms.connectRbs(servingRbs); else ms.releaseRbs(); } private void updateStatus(MobileStation ms) { int x = (int) ms.getX(); int y = (int) ms.getY(); Rbs rbs = ms.connectedRbs(); if (rbs != null ) textarea.append("Point " + x +","+y+" served by:"+rbs+"\n"); else textarea.append("Point " + x +","+y+" cannot be served.\n"); }
|
- Add the MouseListener and MouseMotionListener Class to the Applet.
We need to activate the Mouse Handling in the Applet. In order to do that,
we must call the addMouseListener(MouseListener) and addMouseMotionListener(MouseMotionListener)
method in the Applet initialization process. Use the this-reference
since the handler is defined in inside the class itself.
public void init()
{ // . . . the rest of the code previously defined.
addMouseMotionListener(this); addMouseListener(this); }
|
The complete code in Java is shown in the followings:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class VisualMobileSystem extends Applet implements MouseListener, MouseMotionListener
{
// instance variables
private MobileSystem vodaPhone;
private MobileStation ms;
private TextArea textarea;
public void init()
{
// provide any initialisation necessary for your Applet
vodaPhone = new MobileSystem("VodaPhone Systems");
ms = new MobileStation("Siemes 45i",100,100);
Label title = new Label(vodaPhone.getName());
textarea=new TextArea(4,80);
add(title);
add(textarea);
addMouseMotionListener(this); addMouseListener(this);
}
public void paint(Graphics g)
{
super.paint(g); // to allow painting any contained components
vodaPhone.draw(g);
ms.draw(g);
}
public String getAppletInfo()
{
// replace this with your own info
return "Title: Mobile System Applet \n" +
"Author: CPwJ\n" +
"A simple applet that draws the mobile system topology.";
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mousePressed(MouseEvent e) { textarea.setText(""); //Clear the content of textarea mouseDragged(e);
}
public void mouseDragged(MouseEvent e) { int x = e.getX(); int y = e.getY(); moveMobileStation(x,y); //call method to set new mobilestation location
}
private void moveMobileStation(int x, int y)
{ ms.setLocation(x,y); serveMobileStation(ms,vodaPhone); updateStatus(ms);
repaint(); //Refresh Graphics which in turn will call the paint(Graphics) method }
private static void serveMobileStation(MobileStation ms, MobileSystem msc) { Rbs servingRbs = msc.findRbs(ms.getX(),ms.getY()); if ( servingRbs != null) ms.connectRbs(servingRbs); else ms.releaseRbs(); }
private void updateStatus(MobileStation ms) { int x = (int) ms.getX(); int y = (int) ms.getY(); Rbs rbs = ms.connectedRbs(); if (rbs != null ) textarea.append("Point " + x +","+y+" served by:"+rbs+"\n"); else textarea.append("Point " + x +","+y+" cannot be served.\n"); }
}
|
[Contents] [Next] [Previous]