csa120.shape
Class Canvas

java.lang.Object
  extended bycsa120.shape.Canvas
Direct Known Subclasses:
World

public class Canvas
extends Object

Canvas is a class to allow for simple graphical drawing on a canvas. Drawable objects can be added to and removed from the canvas. The canvas is drawn only when the refresh() method is explicitly called. Furthermore, if this canvas is created by a ControlledThread the refresh() will incur a forced pause or delay based upon the settings of the Controller. This code was originally based upon a general purpose Canvas, made for the BlueJ "shapes" example, by Bruce Quig and Michael Kolling.


Constructor Summary
Canvas(String title, int width, int height)
          Create a Canvas (with white background as default)
Canvas(String title, int width, int height, Color bgColor)
          Create a Canvas.
Canvas(String title, int width, int height, String bgName)
          Create a Canvas.
 
Method Summary
 void add(Drawable obj)
          Add a drawable object to the canvas (however canvas is not automatically refreshed)
 void clear()
          Clear all drawable objects from the canvas (however canvas is not automatically refreshed)
 void destroy()
          Destroys the canvas, closing the associated window.
 Color getBackgroundColor()
          Returns current background color
 int getHeight()
          get the height of the canvas
 int getWidth()
          get the width of the canvas
 void refresh()
          Redraw all objects currently on the Canvas and then process desired pause if in a ControlledThread
 void remove(Drawable obj)
          Remove a drawable object from the canvas (however canvas is not automatically refreshed)
 void setBackgroundColor(Color c)
          Sets the current background color
 void setBackgroundColor(String cName)
          Sets the current background color based upon a list of several known color names from java.awt.Color.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Canvas

public Canvas(String title,
              int width,
              int height,
              Color bgColor)
Create a Canvas.

Parameters:
title - title to appear in Canvas Frame
width - the desired width for the canvas
height - the desired height for the canvas
bgColor - the desired background color of the canvas

Canvas

public Canvas(String title,
              int width,
              int height,
              String bgName)
Create a Canvas.

Parameters:
title - title to appear in Canvas Frame
width - the desired width for the canvas
height - the desired height for the canvas
bgName - the name of the desired background color

Canvas

public Canvas(String title,
              int width,
              int height)
Create a Canvas (with white background as default)

Parameters:
title - title to appear in Canvas Frame
width - the desired width for the canvas
height - the desired height for the canvas
Method Detail

getWidth

public int getWidth()
get the width of the canvas

Returns:
canvas width

getHeight

public int getHeight()
get the height of the canvas

Returns:
canvas height

remove

public void remove(Drawable obj)
Remove a drawable object from the canvas (however canvas is not automatically refreshed)

Parameters:
obj - object to remove

destroy

public void destroy()
Destroys the canvas, closing the associated window.


clear

public void clear()
Clear all drawable objects from the canvas (however canvas is not automatically refreshed)


add

public void add(Drawable obj)
Add a drawable object to the canvas (however canvas is not automatically refreshed)

Parameters:
obj - object to add

refresh

public void refresh()
Redraw all objects currently on the Canvas and then process desired pause if in a ControlledThread


getBackgroundColor

public Color getBackgroundColor()
Returns current background color

Returns:
the color

setBackgroundColor

public void setBackgroundColor(Color c)
Sets the current background color

Parameters:
c - the new color

setBackgroundColor

public void setBackgroundColor(String cName)
Sets the current background color based upon a list of several known color names from java.awt.Color. (e.g., "black", "blue", "cyan", ...)

Parameters:
cName - the name of the desired new color