Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto, 2002.

[Contents] [Next] [Previous]

Lab 4: Introduction to Threads


Making the codes (Editing the VisualMobileSystem Class)

This will be a quite long trail. Before going on, try to backup your VisualMobileSystem class declaration. Because by now you should already modified it and have defined the MobileTask Class in it. If you are lost somewhere, then you can recover from the backup and try again. To make things clear, just modify or add the mentioned method or fields. Do not mess with the other existing ones unless you really know what you are doing.

The Scenario

It is not so difficult to figure out the scenario (as you can see from the beginning of this Lab).

All the changes in the VisualMobileSystem Class should have to do with MouseEvent.
Here are the descriptions:

Adding new Class fields and modifying the init() method

In order to store a dynamic number of MobileStation Objects, we need a special Collection object because on the limitation of array. One of the useful Collection object is the Vector. A Vector (we use the name of mss) can store any object reference as well as dynamic size. To store an object in a Vector, we can just call add(Object), while accessing a Vector can be performed by Enumeration Object returned by the elements() method. The other Class fields that should be defined are the Timer Object and the Point Object for target coordinate. Observe that we still use the ms MobileStation field for the newly created MobileStation but we have to remove the new MobileStation() statement in the Constructor. Here are the definitions:

import java.applet.Applet;
import java.awt.*;
import java.lang.Math;
import java.awt.event.*;
import java.util.Timer;
import java.util.Enumeration;
import java.util.TimerTask;
import java.util.Vector;
import java.awt.Point;
import java.awt.Component;
public class VisualMobileSystem extends Applet implements MouseListener, MouseMotionListener
{
   // instance variables - replace the example below with your own

   private MobileSystem vodaPhone;
   private MobileStation ms;
   private Vector mss=new Vector(); 
   private TextArea textarea;
   private Point target;
   private Timer timer= new Timer(); 

   /**
   * Called by the browser or applet viewer to inform this Applet that it
   * has been loaded into the system. It is always called before the first 
   * time that the start method is called.
   */
   public void init()
   {
         // provide any initialisation necessary for your Applet
vodaPhone = new MobileSystem("VodaPhone Systems");
// ms = new MobileStation("Siemes 45i",100,100); // Remove this, by commenting it out
Label title = new Label(vodaPhone.getName());
textarea=new TextArea(4,80);
add(title);
add(textarea);
addMouseMotionListener(this);
addMouseListener(this);
} // Some other existing method declaration }

Declaring addMobileStation method

The addMobileStation method only have to do with adding the MobileStation into the mss Vector and trying to serve it by the MobileSystem.

   public void addMobileStation(MobileStation ms)
   {
      mss.add(ms);
      MobileSystem.serveMobileStation(ms,vodaPhone);
   }

Declaring removeMobileStation method

In the contrary to addMobileStation method, the removeMobileStation method will remove the MobileStation from the mss Vector and releases any Rbs connection.

   public void removeMobileStation(MobileStation ms)
   {
      if (mss.remove(ms)) {
         ms.releaseRbs();
      }
   }

Modifying the paint method

What should be included in the paint method?

    public void paint(Graphics g)
    {
        super.paint(g);
        // draw Msc topology
        vodaPhone.draw((Graphics2D)g); 
        for (Enumeration i = mss.elements(); i.hasMoreElements();)
        { 
            MobileStation theMs = (MobileStation) i.nextElement();
            if ( theMs != null) theMs.draw(g);
        }
        if ((target != null) && (ms != null))
            g.drawLine((int)ms.getX(),(int)ms.getY(),(int)target.getX(),(int)target.getY());
    }

Modifying the mousePressed method

Actually the codes below clearly defines what to do when the mouse is pressed. First clear the textarea, create a new MobileStation, then add it to the mss Vector, then update the textarea, then define a target Point Object (it will be used when the mouse is dragged or released) and repaint the Applet Panel.

   public void mousePressed(MouseEvent e) 
    {
        textarea.setText("");
        int x = e.getX();
        int y = e.getY();
        ms = new MobileStation("Some new Mobile",x,y);
        mss.add(ms);
        textarea.append(ms.getID() + " created at " + x + "," +    y + ".\n");
        target = new Point(x,y);
        repaint();
    }

Modifying the mouseDragged method

The only thing to do in the mouseDragged method is to move the target Point object following the mouse. You can just change the existing field name ms into the field name target .

    public void mouseDragged(MouseEvent e) 
    {
        int x = e.getX();
        int y = e.getY();
        target.setLocation(x,y);
        repaint(); 
    }

Modifying the mouseReleased method

Finally!! The most important method of all. The mouseRelease is used to perform the MobileTask scheduling using the timer Object. As explained in the Scheduling Task Using Timer Object section, it is actually just a single statement. After scheduling occurred, we should remove the reference in the target and ms field by setting the to null. For the target field, this means the Point Object will be destroyed (since no other reference exists), while for the ms field, the MobileStation Object will still exists since it is refered by the mss Vector for visualization in the paint() method..
Observe that we only have to use a single timer Object for scheduling all the MobileTasks created as mentioned in the discussion about Timer Class.

    public void mouseReleased(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();
        timer.schedule(new MobileTask(ms,x,y),100,100);
        target = null;
        ms = null;
        repaint();
    }

Modifying the moveMobileStation method

The Previous moveMobileStation method is intended for a single MobileStation refered by the Class identifier ms. Now, the ms is only used as a temporary reference to a MobileStation. Thus, we should pass the correct reference to the MobileStation being moved in the method argument.

    public void moveMobileStation(MobileStation ms, int x, int y) 
    {
       //the rest of the definition are the same.
    }

[Contents] [Next] [Previous]