Class Canvas

java.lang.Object
  extended byCanvas

public class Canvas
extends java.lang.Object

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

Version:
1.8 (23.01.2002)
Author:
Michael Kolling (mik), Bruce Quig

Constructor Summary
Canvas(java.lang.String title)
          Create a Canvas with default height, width and background color (300, 300, white).
Canvas(java.lang.String title, int width, int height)
          Create a Canvas with default background color (white).
Canvas(java.lang.String title, int width, int height, java.awt.Color bgColor)
          Create a Canvas.
 
Method Summary
 void draw(java.awt.Shape shape)
          Draw the outline of a given shape onto the canvas.
 boolean drawImage(java.awt.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 drawString(java.lang.String text, int x, int y)
          Draws a String on the Canvas.
 void erase()
          Erase the whole canvas.
 void erase(java.awt.Shape shape)
          Erase a given shape's interior on the screen.
 void eraseCircle(int xPos, int yPos, int diameter)
          Erase the internal dimensions of the given circle.
 void eraseOutline(java.awt.Shape shape)
          Erases a given shape's outline on the screen.
 void eraseRectangle(int xPos, int yPos, int width, int height)
          Erase the internal dimensions of the given rectangle.
 void eraseString(java.lang.String text, int x, int y)
          Erases a String on the Canvas.
 void fill(java.awt.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.
 java.awt.Color getBackgroundColor()
          Returns the current color of the background
 java.awt.Font getFont()
          Returns the current font of the canvas.
 java.awt.Color getForegroundColor()
          Returns the current color of the foreground.
 java.awt.Dimension getSize()
          Returns the size of the canvas.
 boolean isVisible()
          Provide information on visibility of the Canvas.
 void setBackgroundColor(java.awt.Color newColor)
          Sets the background color of the Canvas.
 void setFont(java.awt.Font newFont)
          changes the current Font used on the Canvas
 void setForegroundColor(java.awt.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(java.lang.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(java.lang.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(java.lang.String title,
              int width,
              int height,
              java.awt.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
Method Detail

draw

public void draw(java.awt.Shape shape)
Draw the outline of a given shape onto the canvas.

Parameters:
shape - the shape object to be drawn on the canvas

drawImage

public boolean drawImage(java.awt.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

drawString

public void drawString(java.lang.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(java.awt.Shape shape)
Erase a given shape's interior on the screen.

Parameters:
shape - the shape object to be erased

eraseCircle

public void eraseCircle(int xPos,
                        int yPos,
                        int diameter)
Erase the internal dimensions of the given circle. This is a convenience method. A similar effect can be achieved with the "erase" method.


eraseOutline

public void eraseOutline(java.awt.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 the internal dimensions of the given rectangle. This is a convenience method. A similar effect can be achieved with the "erase" method.


eraseString

public void eraseString(java.lang.String text,
                        int x,
                        int y)
Erases 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

fill

public void fill(java.awt.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 java.awt.Color getBackgroundColor()
Returns the current color of the background

Returns:
the color of the background of the Canvas

getFont

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

Returns:
the font currently in use

getForegroundColor

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

Returns:
the color of the foreground of the Canvas

getSize

public java.awt.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

setBackgroundColor

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

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

setFont

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

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

setForegroundColor

public void setForegroundColor(java.awt.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