Class DisplayCanvas

java.lang.Object
java.awt.Component
java.awt.Canvas
edu.hws.jcm.draw.DisplayCanvas
All Implemented Interfaces:
Computable, ErrorReporter, InputObject, ImageObserver, MenuContainer, Serializable, Accessible

public class DisplayCanvas extends Canvas implements ErrorReporter, InputObject, Computable
A DisplayCanvas is a drawing area that can contain one or more CoordinateRects. Each CoordinateRect can, in turn, contain Drawable items. If you only want one CoordinateRect that fills the whole canvas, you can for the most part ignore the CoordinateRect and work only with the canvas. The option of using an offscreen image for double buffering is provided. By default, this option is on.

If a DisplayCanvas is added to a Controller, then it will take care of calling the checkInput() and compute() methods of the InputObjects and Computables that it contains, so there is no need to add them individually to the Controller. If the DisplayCanvas is added to a JCMPanel, it is automatically added to the Controller for that JCMPanel. (On the other hand, if a DisplayCanvas is added to a Controller, this means that all the items in the DisplayCanvas will be recomputed, even if only some of them need to be.)

The canvas can display an error message that goes away when the canvas is clicked or when clearErrorMessage() is called. This allows the Canvas to be used as an ErrorReporter for a Controller or LimitControlPanel.

When any changes are made to the contents of the Canvas, doRedraw() must be called for those changes to take effect. Normally, doRedraw() is called by the CoordinateRect or by one of the Drawables in a CoordinateRect. Use doRedraw(CoordinateRect c) or doRedraw(int index) to redraw a single CoordinateRect. Note that repainting the canvas is not enough, because this will not automatically refresh the off-screen image.

See Also:
  • Constructor Details

    • DisplayCanvas

      public DisplayCanvas()
      Create a DisplayCanvas with a white background containing no CoordinateRects.
    • DisplayCanvas

      public DisplayCanvas(CoordinateRect c)
      Create a DisplayCanvas with a white background and containing the specified CoordinateRect. The CoordinateRect fills the entire canvas.
      Parameters:
      c - The CoordinateRect that will fill the canvas. If c is null, no CoordinateRect is added to the canvas.
  • Method Details

    • releaseResources

      public void releaseResources()
      Release the memory used the by the off-screen image, if any, that is used for double-buffering. It's a good idea to call this if the DisplayCanvas is in an applet and the applet is stopped.
    • setHandleMouseZooms

      public void setHandleMouseZooms(boolean handle)
      Set the "handleMouseZooms" property of this DisplayCanvas. IF the value is true, then clicking on the canvas will zoom in on the point that is clicked and shift-clicking will zoom out from that point. Only the CoordinateRect, if any, that contains the point is zoomed. Furthermore, if the user clicks-and-drags, a rectangle is drawn. When the mouse is released, the interior of the rectangle is zoomed to fill the CoordinateRect. This property is false by default.
    • getHandleMouseZooms

      public boolean getHandleMouseZooms()
      Get the "handleMouseZooms" property of this DisplayCanvas, which determines whether the canvas reacts to mouse events by zooming the CoordinateRect that is clicked.
    • getUseOffscreenCanvas

      public boolean getUseOffscreenCanvas()
      Set the "useOffscreenCanvas" property of this DisplayCanvas. IF the value is true, an off-screen image is used for double buffering. This property is true by default.
    • setUseOffscreenCanvas

      public void setUseOffscreenCanvas(boolean use)
      Get the "useOffscreenCanvas" property of this DisplayCanvas, which determines whether double-buffering is used.
    • add

      public void add(Drawable d)
      Add the specified Drawable item to the first CoordinateRect in this DisplayCanvas. If no CoordinateRect is associated with the canvas, one is created to fill the entire canvas.
    • add

      public void add(Drawable d, int coordRectIndex)
      Add a Drawable item to one of the CoordinateRects associated with the Canvas.
      Parameters:
      d - The Drawable item to be added to a CoordinateRect
      coordRectIndex - The index of the CoordinateRect, where the index of the first CoordinateRect that was added to the cavas is zero, the index of the second is one, and so on. A CoordinateRect with the specified index must already exist in the canvas, or an IllegalArgumentException is thrown.
    • addCoordinateRect

      public void addCoordinateRect(CoordinateRect c)
      Add the specified CoordinateRect to this DisplayCanvas, filling the entire canvas, and with background color equal to the background color of the canvas.
      Parameters:
      c - the CoordinateRect to be added. If null, an IllegalArgumentException is thrown.
    • addCoordinateRect

      public void addCoordinateRect(CoordinateRect coords, double hmin, double hmax, double vmin, double vmax, Color background)
      Add a CoordinateRect to the canvas, occupying a specified region of the canvas.
      Parameters:
      coords - The CoordinateRect to be added. If this is null, an IllegalArgumentExceptionis thrown.
      hmin - Specifies the left edge of the CoordinateRect in the canvas, as a fraction of the size of the canvas. This must be in the range form 0 to 1, or an IllegalArgumentException is thrown.
      hmax - Specifies the right edge of the CoordinateRect in the canvas, as a fraction of the size of the canvas. This must be in the range form 0 to 1 and must be strictly greater than hmin, or an IllegalArgumentException is thrown.
      vmin - Specifies the top edge of the CoordinateRect in the canvas, as a fraction of the size of the canvas. This must be in the range form 0 to 1, or an IllegalArgumentException is thrown.
      vmax - Specifies the bottom edge of the CoordinateRect in the canvas, as a fraction of the size of the canvas. This must be in the range form 0 to 1 and must be strictly greater than vmin, or an IllegalArgumentException is thrown.
      background - The background color of the CoordinateRect. The CoordinateRect is filled with this color before the Drawables that it contains are drawn. If background is null, no filling takes place and the canvas shows through.
    • addNewCoordinateRect

      public int addNewCoordinateRect(double hmin, double hmax, double vmin, double vmax)
      Add a newly created CoordinateRect covering the specified section of the canvas. hmin, hmax, vmin, vmax must be in the range 0 to 1. The index of the new CoordinateRect is returned.
    • addNewCoordinateRect

      public int addNewCoordinateRect(double hmin, double hmax, double vmin, double vmax, Color background)
      Add a newly created CoordinateRect covering the specified section of the canvas, with the specfied background color. hmin, hmax, vmin, vmax must be in the range 0 to 1. The index of the new CoordinateRect is returned.
    • getCoordinateRect

      public CoordinateRect getCoordinateRect()
      Get the first CoordinateRect in this canvas. (If none exists, one is created and added to the canvas.)
    • getCoordinateRect

      public CoordinateRect getCoordinateRect(int i)
      Get the i-th CoordinateRect in this DisplayCanvas. They are numbered staring from zero. If there is no i-th rect, null is returned, except that if there are NO coordinate rects and a request for rect 0 is received, then a new CoordinateRect is added to fill the entire canvas.
    • findCoordinateRectAt

      public CoordinateRect findCoordinateRectAt(int pixelX, int pixelY)
      Return CoordinateRect that contains the specified pixel, or null if there is none. The CoordinateRects are searched in reverse order, so that the "top" CoordinateRect at that point is returned. Note that this method only makes sense if the canvas has already been displayed. (Mostly, this is for internal use in this class.)
    • doRedraw

      public void doRedraw()
      Should be called whenever the contents of the canvas have changed and so it needs to need to be redrawn. (This causes the off-screen image to be redrawn. A simple call to repaint() does not do this.) If only one CoordinateRect needs to be repainted, you can call doRedraw(int i) or or doRedraw(CoordinateRect c), which can be more efficient than redrawing the whole canvas. If an error message is displayed, it will be cleared.
    • doRedraw

      public void doRedraw(int coordRectIndex)
      To be called when the contents of one of the CordinateRects have changed and so it needs to need to be redrawn. (This causes the off-screen image to be redrawn. A simple call to repaint() does not do this.) If an error message is displayed, it will be cleared.
      Parameters:
      coordRectIndex - The index of the CoordinateRect to be redrawn, where the first CoordinateRect is at index zero. If there is no such CoordinateRect, then nothing is done.
    • doRedraw

      public void doRedraw(CoordinateRect coords)
      To be called when the contents of one of the CordinateRects have changed and so it needs to need to be redrawn. (This causes the off-screen image to be redrawn. A simple call to repaint() does not do this.) If an error message is displayed, it will be cleared.
      Parameters:
      coords - The CoordinateRect to be redrawn. If coords is not in this DisplayCanvas, nothing is done.
    • checkInput

      public void checkInput()
      This is generally called by a Controller. It calls the checkInput() method of any InputObject displayed on this Canvas.
      Specified by:
      checkInput in interface InputObject
    • compute

      public void compute()
      This is generally called by a Controller. It calls the compute() method of any InputObject displayed on this Canvas.
      Specified by:
      compute in interface Computable
    • notifyControllerOnChange

      public void notifyControllerOnChange(Controller c)
      Method required by InputObject interface; in this class, calls the same method recursively on any CoordinateRects contained in this DisplayCanvas. This is meant to be called by JCMPanel.gatherInputs().
      Specified by:
      notifyControllerOnChange in interface InputObject
    • getErrorBackground

      public Color getErrorBackground()
      Get color that is used as a background when the canvas displays an error message.
    • setErrorBackground

      public void setErrorBackground(Color c)
      Set color to be used as a background when the canvas displays an error message. The default is a light green. If the specified Color value is null, nothing is done.
    • getErrorForeground

      public Color getErrorForeground()
      Get color that is used for the text when the canvas displays an error message.
    • setErrorForeground

      public void setErrorForeground(Color c)
      Set color to be used for the text when the canvas displays an error message. The default is a dark green. If the specified Color value is null, nothing is done.
    • getErrorMessage

      public String getErrorMessage()
      Get the error message that is currently displayed on the canvas. If no error is displyed, the return value is null.
      Specified by:
      getErrorMessage in interface ErrorReporter
    • setErrorMessage

      public void setErrorMessage(Controller c, String message)
      Set an error message to be displayed on the canvas. This method is generally called by a Controller or a LimitControlPanel. If you call it directly, use null as the first parameter.
      Specified by:
      setErrorMessage in interface ErrorReporter
      Parameters:
      c - The Controller, if any, that is calling this routine. This controller will be notified when the error message is cleared. If the method is not being called by a contrller, this parameter should be set to null.
      message - The error message to be displayed. If the value is null or is a blank string, the current message, if any, is cleared.
    • clearErrorMessage

      public void clearErrorMessage()
      Clear the error message, if any, that is currently displayed on the canvas.
      Specified by:
      clearErrorMessage in interface ErrorReporter
    • processMouseEvent

      public void processMouseEvent(MouseEvent evt)
      This has been overridden to handle the mouse zoom feature. Not meant to be called directly.
      Overrides:
      processMouseEvent in class Component
    • processMouseMotionEvent

      public void processMouseMotionEvent(MouseEvent evt)
      This has been overridden to handle the mouse zoom feature. Not meant to be called directly.
      Overrides:
      processMouseMotionEvent in class Component
    • getPreferredSize

      public Dimension getPreferredSize()
      This has been overridden to return a default size of 350-by-350 pixels. Not usually called directly.
      Overrides:
      getPreferredSize in class Component
    • update

      public void update(Graphics g)
      This has been overridden to implemnt double-buffering. Not meant to be called directly.
      Overrides:
      update in class Canvas
    • paint

      public void paint(Graphics g)
      Draw the contents of the DisplayCanvas. Not usually called directly.
      Overrides:
      paint in class Canvas
    • drawTemp

      public void drawTemp(DrawTemp drawItem)
      Draws the specified item in the first CoordinateRect in this canvas. It is drawn on screen and on the off-screen canvas, if there is one. However, no information is kept about this item, so the drawing will disappear the next time the off-screen canvas is re-drawn (if there is an off-screen canvas) or the next time the canvas is repainted (is there is no off-screen canvas). If the canvas contains no CoordinateRect when this is called, a new one is added. Note that this method should only be called after the canvas has appeared on the screen.
    • drawTemp

      public void drawTemp(DrawTemp drawItem, int coordRectIndex)
      Draws the specified item in the specified CoordinateRect in this canvas. It is drawn on screen and on the off-screen canvas, if there is one. However, no information is kept about this item, so the drawing will disappear the next time the off-screen canvas is re-drawn (if there is an off-screen canvas) or the next time the canvas is repainted (is there is no off-screen canvas). Note that this method should only be called after the canvas has appeared on the screen.
      Parameters:
      drawItem - The non-null object that is to be drawn
      coordRectIndex - The index of the CoordinateRect in which it is to be drawn, where the index of the fist CoordinateRect added to the canvas is zero, and so on. If there is no CoordinateRect with the specified index, an IllegalArgumentException is thrown.