Class Canvas
Class Canvas
- java.lang.Object
-
- Canvas
-
public class Canvas extends java.lang.Object
Class Canvas – a class to allow for simple graphical drawing on a canvas.- Version:
- 2016.02.29
- Author:
- Michael Kölling (mik), Bruce Quig
-
Constructor Summary
Constructors Constructor and Description 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
All MethodsInstance MethodsConcrete Methods Modifier and Type Method and Description 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 backgroundjava.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 Canvasvoid
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.
-
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 Framewidth
– the desired width for the canvasheight
– 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 Framewidth
– the desired width for the canvasheight
– the desired height for the canvasbgClour
– the desired background color of 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 displayedx
– x co-ordinate for Image placementy
– y co-ordinate for Image placement- Returns:
- returns boolean value representing whether the image was completely loaded
-
drawLine[Show source in BlueJ]
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 liney1
– y co-ordinate of start of linex2
– x co-ordinate of end of liney2
– 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 displayedx
– x co-ordinate for text placementy
– y co-ordinate for text placement
-
erase[Show source in BlueJ]
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[Show source in BlueJ]
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[Show source in BlueJ]
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 displayedx
– x co-ordinate for text placementy
– 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[Show source in BlueJ]
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.- Parameters:
xPos
– The x-coordinate of the circle center pointyPos
– The y-coordinate of the circle center pointdiameter
– The diameter of the circle to be drawn
-
fillRectangle[Show source in BlueJ]
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[Show source in BlueJ]
public java.awt.Color getBackgroundColor()
Returns the current color of the background- Returns:
- the color of the background of the Canvas
-
getFont[Show source in BlueJ]
public java.awt.Font getFont()
Returns the current font of the canvas.- Returns:
- the font currently in use
-
getForegroundColor[Show source in BlueJ]
public java.awt.Color getForegroundColor()
Returns the current color of the foreground.- Returns:
- the color of the foreground of the Canvas
-
getSize[Show source in BlueJ]
public java.awt.Dimension getSize()
Returns the size of the canvas.- Returns:
- The current dimension of the canvas
-
isVisible[Show source in BlueJ]
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[Show source in BlueJ]
public void setSize(int width, int height)
Sets the size of the canvas.- Parameters:
width
– new widthheight
– new height
-
setVisible[Show source in BlueJ]
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[Show source in BlueJ]
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
-