Class Canvas

java.lang.Object
  extended by Canvas

public class Canvas
extends Object

Class Canvas - a class to allow for simple graphical drawing on a canvas.

Version:
2006.03.30, 2012.09.03 CH Modified to allow window to close. Modified to make the Canvas visible on construction, 2012.10.15 JR Modified to make close window kill program (for external use), 2012.11.06 JR Modified to remove extraneous erase calls (reduces flicker in animation) Added "draw" method for circles and rectangles
Author:
Michael Kolling (mik), Bruce Quig

Constructor Summary
Canvas(String title)
          Create a Canvas with default height, width and background color (300, 300, white).
Canvas(String title, int width, int height)
          Create a Canvas with default background color (white).
Canvas(String title, int width, int height, Color bgColor, int locX, int locY)
          Create a Canvas.
 
Method Summary
 void closeCanvas()
          close the window This method sets the Canvas to invisible, then disposes of it.
 void draw(Shape shape)
          Draws the given shape with the current foreground color of the canvas.
 void drawCircle(int xPos, int yPos, int diameter)
          Draws a circle with the current foreground color of the canvas.
 boolean drawImage(Image image, int x, int y)
          Draws an image onto the canvas.
 void drawLine(int x1, int y1, int x2, int y2)
          Draws a line on the Canvas.
 void drawRectangle(int xPos, int yPos, int width, int height)
          Draws a rectangle with the current foreground color of the canvas.
 void drawString(String text, int x, int y)
          Draws a String on the Canvas.
 void erase()
          Erase the whole canvas.
 void erase(Shape shape)
          Erase a portion of the canvas with the given shape.
 void eraseCircle(int xPos, int yPos, int diameter)
          Erases a portion of the canvas in the shape of a circle.
 void eraseOutline(Shape shape)
          Erases a given shape's outline on the screen.
 void eraseRectangle(int xPos, int yPos, int width, int height)
          Erase a portion of the canvas in the shape of a rectangle.
 void fill(Shape shape)
          Fill the internal dimensions of a given shape with the current foreground color of the canvas.
 void fillCircle(int xPos, int yPos, int diameter)
          Fill the internal dimensions of the given circle with the current foreground color of the canvas.
 void fillRectangle(int xPos, int yPos, int width, int height)
          Fill the internal dimensions of the given rectangle with the current foreground color of the canvas.
 Color getBackgroundColor()
          Returns the current color of the background
 Font getFont()
          Returns the current font of the canvas.
 Color getForegroundColor()
          Returns the current color of the foreground.
 Dimension getSize()
          Returns the size of the canvas.
 boolean isVisible()
          Provide information on visibility of the Canvas.
 void refresh()
          Refreshes the drawing the the canvas, to help with things that aren't showing up.
 void setBackgroundColor(Color newColor)
          Sets the background color of the Canvas.
 void setFont(Font newFont)
          changes the current Font used on the Canvas
 void setForegroundColor(Color newColor)
          Sets the foreground color of the Canvas.
 void setSize(int width, int height)
          Sets the size of the canvas.
 void setVisible(boolean visible)
          Set the canvas visibility and brings canvas to the front of screen when made visible.
 void wait(int milliseconds)
          Waits for a specified number of milliseconds before finishing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Canvas

public Canvas(String title)
Create a Canvas with default height, width and background color (300, 300, white).

Parameters:
title - title to appear in Canvas Frame

Canvas

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

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

Canvas

public Canvas(String title,
              int width,
              int height,
              Color bgColor,
              int locX,
              int locY)
Create a Canvas. Makes the Canvas visible when it is constructed

Parameters:
title - title to appear in Canvas Frame
width - the desired width for the canvas
height - the desired height for the canvas
bgClour - the desired background color of the canvas
locX - x-coordinate of upper left corner of window
locY - y-coordinate of upper left corner of window
Method Detail

closeCanvas

public void closeCanvas()
close the window This method sets the Canvas to invisible, then disposes of it. Chuck Hommel 2012.09.03


draw

public void draw(Shape shape)
Draws the given shape with the current foreground color of the canvas.

Parameters:
shape - the shape object to be drawn

drawCircle

public void drawCircle(int xPos,
                       int yPos,
                       int diameter)
Draws a circle with the current foreground color of the canvas. This circle is unfilled.


drawImage

public boolean drawImage(Image image,
                         int x,
                         int y)
Draws an image onto the canvas.

Parameters:
image - the Image object to be displayed
x - x co-ordinate for Image placement
y - y co-ordinate for Image placement
Returns:
returns boolean value representing whether the image was completely loaded

drawLine

public void drawLine(int x1,
                     int y1,
                     int x2,
                     int y2)
Draws a line on the Canvas.

Parameters:
x1 - x co-ordinate of start of line
y1 - y co-ordinate of start of line
x2 - x co-ordinate of end of line
y2 - y co-ordinate of end of line

drawRectangle

public void drawRectangle(int xPos,
                          int yPos,
                          int width,
                          int height)
Draws a rectangle with the current foreground color of the canvas. This rectangle is unfilled.


drawString

public void drawString(String text,
                       int x,
                       int y)
Draws a String on the Canvas.

Parameters:
text - the String to be displayed
x - x co-ordinate for text placement
y - y co-ordinate for text placement

erase

public void erase()
Erase the whole canvas.


erase

public void erase(Shape shape)
Erase a portion of the canvas with the given shape.

Parameters:
shape - the shape object to be erased

eraseCircle

public void eraseCircle(int xPos,
                        int yPos,
                        int diameter)
Erases a portion of the canvas in the shape of a circle. This is a convenience method. See also the "erase" method.


eraseOutline

public void eraseOutline(Shape shape)
Erases a given shape's outline on the screen.

Parameters:
shape - the shape object to be erased

eraseRectangle

public void eraseRectangle(int xPos,
                           int yPos,
                           int width,
                           int height)
Erase a portion of the canvas in the shape of a rectangle. This is a convenience method. See also the "erase" method.


fill

public void fill(Shape shape)
Fill the internal dimensions of a given shape with the current foreground color of the canvas.

Parameters:
shape - the shape object to be filled

fillCircle

public void fillCircle(int xPos,
                       int yPos,
                       int diameter)
Fill the internal dimensions of the given circle with the current foreground color of the canvas.


fillRectangle

public void fillRectangle(int xPos,
                          int yPos,
                          int width,
                          int height)
Fill the internal dimensions of the given rectangle with the current foreground color of the canvas. This is a convenience method. A similar effect can be achieved with the "fill" method.


getBackgroundColor

public Color getBackgroundColor()
Returns the current color of the background

Returns:
the color of the background of the Canvas

getFont

public Font getFont()
Returns the current font of the canvas.

Returns:
the font currently in use

getForegroundColor

public Color getForegroundColor()
Returns the current color of the foreground.

Returns:
the color of the foreground of the Canvas

getSize

public Dimension getSize()
Returns the size of the canvas.

Returns:
The current dimension of the canvas

isVisible

public boolean isVisible()
Provide information on visibility of the Canvas.

Returns:
true if canvas is visible, false otherwise

refresh

public void refresh()
Refreshes the drawing the the canvas, to help with things that aren't showing up.


setBackgroundColor

public void setBackgroundColor(Color newColor)
Sets the background color of the Canvas.

Parameters:
newColor - the new color for the background of the Canvas

setFont

public void setFont(Font newFont)
changes the current Font used on the Canvas

Parameters:
newFont - new font to be used for String output

setForegroundColor

public void setForegroundColor(Color newColor)
Sets the foreground color of the Canvas.

Parameters:
newColor - the new color for the foreground of the Canvas

setSize

public void setSize(int width,
                    int height)
Sets the size of the canvas.

Parameters:
width - new width
height - new height

setVisible

public void setVisible(boolean visible)
Set the canvas visibility and brings canvas to the front of screen when made visible. This method can also be used to bring an already visible canvas to the front of other windows.

Parameters:
visible - boolean value representing the desired visibility of the canvas (true or false)

wait

public void wait(int milliseconds)
Waits for a specified number of milliseconds before finishing. This provides an easy way to specify a small delay which can be used when producing animations.

Parameters:
milliseconds - the number