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

[Contents] [Next] [Previous]

Lab 3: Building Applets - Graphical representation


Adding draw(Graphics) method in SquareCell Class

Similar to the process in defining draw(Graphics) method in the CircleCell, the declaration of SquareCell Class should change into:

public class SquareCell extends Cell implements Visualization    

Then add a new method called draw(Graphics g) inside the Class definition. We will use the drawRect(int x, int y, int width, int height) method.
It will draws the outline of the specified rectangle in the Graphics context. The left and right edges of the rectangle are at x and x + width.
The top and bottom edges are at y and y + height. The rectangle is drawn using the graphics context's current color.


The complete implementation in Java is shown below.

import java.lang.Math;
import java.awt.Graphics;
public class SquareCell extends Cell implements Visualization
{
     // . . . all other code of the original SquareCell Class
       
     public void draw(Graphics g)
     {
int width = (int) (getR()*Math.sqrt(2.0)); int x = (int) (getX() - width/2);
int y = (int) (getY() - width/2); g.drawRect(x,y,width,width); } }

 


[Contents] [Next] [Previous]