Class DrawingCanvas

java.lang.Object
  extended by DrawingCanvas

public class DrawingCanvas
extends java.lang.Object

This is a simple class that implements a blank canvas in a JFrame that can be used for basic 2D drawing. To use it, simply create an instance of DrawingCanvas, retrieve the Graphics2D object by calling getGraphics(), and begin calling some of the drawing methods. Upon instantiation, this class creates a JFrame and makes it visible on the screen.

You can retrieve the Graphics2D object suitable for drawing onto this window by calling the getGraphics() method. It is important to realize that the Graphics2D object is valid only until the window is resized by calling setSize(). After a call to setSize(), a new Graphics2D object needs to be retrieved by calling getGraphics().

Simple animation can be achieved by using Thread.sleep() to slow down the execution of drawing calls to this object.

Individual pixels can be modified by calling setPixel().

An example of the use of this class is shown below:

DrawingCanvas canvas = new DrawingCanvas();
Graphics g = canvas.getGraphics();

canvas.setBackground(new Color(1.0f,1.0f,0.0f));
g.setColor(new Color(1.0f,0.0f,0.0f,0.5f));
g.fillRect(20, 20, 320, 240);

Author:
David Wolff

Constructor Summary
DrawingCanvas()
          Creates and displays a new DrawingCanvas of size 640x480.
DrawingCanvas(int w, int h)
          Creates and displays a new DrawingCanvas with the given dimensions.
 
Method Summary
 void clear()
          Clears the window to the background color.
 void drawImage(int x, int y, java.lang.String fileName)
          Draws an image that is loaded from a file into this DrawingCanvas.
 java.awt.Graphics2D getGraphics()
          Returns a Graphics2D object suitable for drawing onto the window.
 int getHeight()
          Returns the height of the inside of this DrawingCanvas.
 java.awt.Color getPixel(int x, int y)
          Gets the color for the pixel at the given location.
 int getWidth()
          Returns the width of the inside of this DrawingCanvas.
 void setBackground(java.awt.Color c)
          Sets the background color for this window.
 void setPixel(int x, int y, java.awt.Color c)
          Sets the pixel at the given location to the given color.
 void setRepaintDelay(int delay)
          Sets how often the window is repainted.
 void setSize(java.awt.Dimension d)
          Changes the size of this window to the given dimension.
 void setSize(int w, int h)
          Changes the size of this window to the given width and height.
 void setTitle(java.lang.String title)
          Sets the title for this window to the specified string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DrawingCanvas

public DrawingCanvas()
Creates and displays a new DrawingCanvas of size 640x480.


DrawingCanvas

public DrawingCanvas(int w,
                     int h)
Creates and displays a new DrawingCanvas with the given dimensions.

Parameters:
w - the width of the window.
h - the height of the window.
Method Detail

setTitle

public void setTitle(java.lang.String title)
Sets the title for this window to the specified string.

Parameters:
title - the title to be displayed in the window's border.

setRepaintDelay

public void setRepaintDelay(int delay)
Sets how often the window is repainted. Drawing will not appear on the canvas until the window is repainted. Hence, one can expect that updates to the image will appear on the screen in a maximum of delay milliseconds. The default delay is 100.

Parameters:
delay - the delay in milliseconds

getWidth

public int getWidth()
Returns the width of the inside of this DrawingCanvas.

Returns:
the width of the inside of this DrawingCanvas.

getHeight

public int getHeight()
Returns the height of the inside of this DrawingCanvas.

Returns:
the height of the inside of this DrawingCanvas.

setSize

public void setSize(java.awt.Dimension d)
Changes the size of this window to the given dimension. This invalidates the current Graphics object, so one should retrieve the Graphics object by calling getGraphics after executing this method.

Parameters:
d - the new dimension for this window.

setSize

public void setSize(int w,
                    int h)
Changes the size of this window to the given width and height. This invalidates the current Graphics object, so one should retrieve the Graphics object by calling getGraphics after executing this method.

Parameters:
w - the new width in pixels.
h - the new height in pixels.

setBackground

public void setBackground(java.awt.Color c)
Sets the background color for this window.

Parameters:
c - the background color.

getGraphics

public java.awt.Graphics2D getGraphics()
Returns a Graphics2D object suitable for drawing onto the window. The Graphics2D object is invalidated when setSize is called, hence should be kept around only as long as needed to execute the desired drawing operations, then discarded for safety.

Returns:
a Graphics2D object suitable for drawing onto this window.

clear

public void clear()
Clears the window to the background color.


setPixel

public void setPixel(int x,
                     int y,
                     java.awt.Color c)
Sets the pixel at the given location to the given color.

Parameters:
x - the x coordinate of the pixel (between 0 and the width of the window minus one).
y - the y coordinate of the pixel (between 0 and the height of the window minus one).
c - the color of the pixel.

getPixel

public java.awt.Color getPixel(int x,
                               int y)
Gets the color for the pixel at the given location.

Parameters:
x - the x coordinate of the pixel.
y - the y coordinate of the pixel.
Returns:
the color of the pixel.

drawImage

public void drawImage(int x,
                      int y,
                      java.lang.String fileName)
Draws an image that is loaded from a file into this DrawingCanvas.

Parameters:
x - the x coordinate of the upper left of the image.
y - the y coordinate of the upper left of the image.
fileName - the file name of the image file.