SLG 20.1 CS 4 Java - Awt.canvas
SLG 20.1 CS 4 Java - Awt.canvas
TARGET
By the end of this learning guide module, the students should be able to:
1. Demonstrate a window application using a canvas
HOOK
In the previous lesson, the bufferedwriter was discussed wherein this function is to writes text
to a character-output stream, buffering characters so as to provide for the efficient writing of single
characters, arrays, and strings.
In this module we will use the java.awt.canvas. This class is a component represents a blank
rectangular area of the screen onto which the application can draw or from which the application can
trap input events from the user.
An application must subclass the Canvas class in order to get useful functionality such as
creating a custom component. The paint method must be overridden in order to perform custom graphics
on the canvas.
IGNITE
java.awt.canvas
Methods Description
1. addNotify() Creates the peer of the canvas. This peer allows you to
change the user interface of the canvas without changing
its functionality.
2. paint(Graphics g) Most applications that subclass Canvas should override
this method in order to perform some useful operation
(typically, custom painting of the canvas). The default
operation is simply to clear the canvas. Applications
that override this method need not call super.paint(g).
3. update(Graphics g) This method is called in response to a call to repaint.
The canvas is first cleared by filling it with the
Example:
import java.awt.*;
public class thiscanvas
{
public thiscanvas ()
{
Frame fCanvas= new Frame("Canvas Example");
fCanvas.add(new MyCanvasExample());
fCanvas.setLayout(null);
fCanvas.setSize(400, 400);
fCanvas.setVisible(true);
}
public static void main(String args[])
{
new thiscanvas ();
}
}
class MyCanvas extends Canvas
{
public MyCanvasExample () {
setBackground (Color.blue);
setSize(300, 200);
}
public void paint(Graphics m)
{
m.setColor(Color.yellow);
m.fillOval(75, 75, 150, 75);
}
}
KNOT
References
[1] Java.awt.canvas – JavaTpoint. (n.d.). Retrieved March 9, 2021, from
https://www.javatpoint.com/Java.awt.canvas
[2] Java.awt.canvas – Tutorialspoint. (n.d.). Retrieved March 9, 2021, from
https://www.tutorialspoint.com/java/ Java.awt.canvas.htm
[3] Java.awt.canvas – java-examples (n.d.). Retrieved March 9, 2021, from
https://www.java-examples.com/Java.awt.canvas -examples
[3] Java.awt.canvas – docs.oracle (n.d.). Retrieved March 9, 2021, from
https://docs.oracle.com/javase/8/docs/api/java/lang/Java.awt.canvas.html