|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.ObjectDrawingCanvas
public class DrawingCanvas
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);
| 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 |
|---|
public DrawingCanvas()
public DrawingCanvas(int w,
int h)
w - the width of the window.h - the height of the window.| Method Detail |
|---|
public void setTitle(java.lang.String title)
title - the title to be displayed in the window's border.public void setRepaintDelay(int delay)
delay milliseconds. The default delay is 100.
delay - the delay in millisecondspublic int getWidth()
public int getHeight()
public void setSize(java.awt.Dimension d)
d - the new dimension for this window.
public void setSize(int w,
int h)
w - the new width in pixels.h - the new height in pixels.public void setBackground(java.awt.Color c)
c - the background color.public java.awt.Graphics2D getGraphics()
public void clear()
public void setPixel(int x,
int y,
java.awt.Color c)
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.
public java.awt.Color getPixel(int x,
int y)
x - the x coordinate of the pixel.y - the y coordinate of the pixel.
public void drawImage(int x,
int y,
java.lang.String fileName)
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.
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||