001: /*************************************************************************
002: * *
003: * 1) This source code file, in unmodified form, and compiled classes *
004: * derived from it can be used and distributed without restriction, *
005: * including for commercial use. (Attribution is not required *
006: * but is appreciated.) *
007: * *
008: * 2) Modified versions of this file can be made and distributed *
009: * provided: the modified versions are put into a Java package *
010: * different from the original package, edu.hws; modified *
011: * versions are distributed under the same terms as the original; *
012: * and the modifications are documented in comments. (Modification *
013: * here does not include simply making subclasses that belong to *
014: * a package other than edu.hws, which can be done without any *
015: * restriction.) *
016: * *
017: * David J. Eck *
018: * Department of Mathematics and Computer Science *
019: * Hobart and William Smith Colleges *
020: * Geneva, New York 14456, USA *
021: * Email: eck@hws.edu WWW: http://math.hws.edu/eck/ *
022: * *
023: *************************************************************************/package edu.hws.jcm.draw;
024:
025: import java.awt.Graphics;
026:
027: /**
028: * A Drawable object can be added to a CoordinateRect, which is itself in
029: * a DisplayCanvas. Its purpose is, generally, to draw something in the
030: * rectangular area represented by the CoordinateRect. The drawing can
031: * use information in the CoordinateRect, which includes both the real
032: * number coordinates and the pixel coordinates of the rectangular area.
033: */
034: abstract public class Drawable implements java.io.Serializable {
035:
036: /**
037: * The CoordinateRect for the rectagular area where this
038: * Drawable is drawn. This is set automatically when the
039: * Drawable is added to a CoordingteRect and should not be
040: * changed. (It will be changed automatically if the
041: * Drawable is removed from the CoordinateRect.)
042: */
043: protected CoordinateRect coords;
044:
045: /**
046: * The canvas on which this Drawable is drawn. This is set
047: * automatically when the Drawable is added to a CoordinateRect
048: * and it should not be changed. (It will be changed automatically
049: * if the Drawable is removed from the CoordinateRect.)
050: */
051: protected DisplayCanvas canvas;
052:
053: private boolean visible = true; // If visible is false, then the CoordinateRect
054:
055: // that manages this Drawable will ignore it.
056: // It will not call the draw() routine, so when
057: // draw is called, it can be assumed that the
058: // Drawable is visible.
059:
060: /**
061: * Draw this drawable in the graphics context g. This is meant to
062: * be called only by the CoordinateRect, coords, that manages this Drawable.
063: * The coords contains information about the rectangular area in which
064: * this Drawable is displayed, both in terms of pixels and in terms
065: * of real (x,y)-coordinates.
066: * The value of coordsChanged is true if any of the values
067: * coords.getXmin(), coords.getXmax(), coords.getYmin(), coords.getYmax(),
068: * coords.getLeft(), coords.getRight(), coords.getTop(), coords.getBottom(),
069: * or coords.getGap() has changed. Drawables that depend only on this
070: * information can check the value of coordsChanged to see whether they
071: * need to update any previously computed member variables that
072: * depend on these values. This method is meant to be called only by the system.
073: *
074: * @param g The graphics context in which the Drawble is to be drawn. (The drawing
075: * can change the color in g, but should not permanently change font, painting mode, etc.
076: * Thus, every drawable is responsible for setting the color it wants to use.)
077: * @param coordsChanged Indicates whether the CoordinateRect has changed.
078: */
079: abstract public void draw(Graphics g, boolean coordsChanged);
080:
081: /**
082: * Return true if this Drawable is visible, false if it is hidden.
083: * A hidden Drawable is ignored by the CoordinateRect that manages it.
084: */
085: public boolean getVisible() {
086: return visible;
087: }
088:
089: /**
090: * Set the visibility of this Drawable. If show is false, then
091: * the Drawable is hidden. If it is true, the Drawable is shown.
092: */
093: public void setVisible(boolean show) {
094: if (show != visible) {
095: visible = show;
096: needsRedraw();
097: }
098: }
099:
100: /**
101: * This routine should be called if the appearance of the Drawable changes
102: * so that the rectangular area that it occupies has to be redrawn.
103: * The routine is generally meant to be called by the Drawable itself.
104: * It will notify the DisplayCanvas, canvas, that the CoordinateRect,
105: * coords, needs to be redrawn, where canvas and coords are the member
106: * variables in this class. If canvas is null, nothing happens, since
107: * presumably the Drawable is not displayed anywhere in that case.
108: */
109: public void needsRedraw() {
110: if (canvas != null)
111: canvas.doRedraw(coords);
112: }
113:
114: /**
115: * Sets the values of member variables canvas and coords. This is
116: * designed to be called only by the CoordinateRect class.
117: */
118: protected void setOwnerData(DisplayCanvas canvas,
119: CoordinateRect coords) {
120: this .canvas = canvas;
121: this .coords = coords;
122: }
123:
124: } // end class Drawable
|