/** Drawable - the ancestor of drawing objects **/ package ciips.animation; import java.awt.*; /** * Interface for the drawing object to be drawn in the drawing panel. *

* Any graphical objects to be displayed on the DrawingPanel * should implement this interface. * All the abstract methods of the interface must be defined in the * object class defining the graphical object. *

* For example, if Box is a class which implements * DrawingObj, i.e. the class declaration of Box * starts with the following line:

 *      class Box implements DrawingObj {
 *        ...
 * 
* Then any instance of class Box can be added to the drawing * canvas as follows:
 *      Box box = new Box(...);
 *      drawingPanel.addDrawingObj(box);
 *      box.move(x, y);
 *      drawingPanel.redraw();
 * 
* The first line declares an instance of class Box called * box. The next line uses the method addDrawingObj * to add box into the canvas drawingPanel, which * is an instance of the object class DrawingPanel. *

* The move method of the drawing object (which must be * specified) is then called to move the corresponding object to position * (x, y). Finally, the redraw() method of * DrawingPanel class is called to refresh the panel and * delay for the object to be visible. * @see DrawingPanel * @see DrawingPanel#addDrawingObj * @see DrawingPanel#redraw */ public abstract class Drawable implements DrawingObj { protected int x, y; protected boolean highlight, grey; protected Color colour; protected String label; public static final int DEFAULT_START = 20; public Drawable() { x = y = DEFAULT_START; colour = Color.blue; } public abstract void draw(Graphics g); /** * This method repositions the drawing object to the new location * specified by the paramters. * @param x The x coordinate of the drawing object's new position. * @param y The y coordinate of the drawing object's new position. */ public void move(int x, int y) { this.x = x; this.y = y; } /** * Returns the x coordinate of the drawing object's reference point. * @return The x coordinate of the drawing object's reference point. */ public int getX() { return x; } /** * Returns the y coordinate of the drawing object's reference point. * @return The y coordinate of the drawing object's reference point. */ public int getY() { return y; } /** * Set the highlight attribute */ public void setHighlight( boolean on ) { highlight = on; } /** * Set the grey attribute */ public void setGrey( boolean on ) { grey = on; } /** * Return the colour to be used to draw this object * based on it's current status. * Note that highlight takes precedence over grey **/ public Color getCurrentColour() { if ( highlight ) { return Color.yellow; } if ( grey ) { return Color.gray; } return colour; } /** * Set an object's label * **/ public void setText( String t ) { label = t; } } // interface