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:

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:

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]