0% found this document useful (0 votes)
16 views3 pages

SLG 20.1 CS 4 Java - Awt.canvas

This learning guide module focuses on using the java.awt.canvas class to create window applications that allow for custom drawing. Students are expected to demonstrate their understanding by creating a canvas and painting on it. Key methods of the Canvas class, such as paint and createBufferStrategy, are discussed along with an example implementation.

Uploaded by

ddungala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

SLG 20.1 CS 4 Java - Awt.canvas

This learning guide module focuses on using the java.awt.canvas class to create window applications that allow for custom drawing. Students are expected to demonstrate their understanding by creating a canvas and painting on it. Key methods of the Canvas class, such as paint and createBufferStrategy, are discussed along with an example implementation.

Uploaded by

ddungala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Subject Code : CS4

Module Code : 18 - Canvas


Lesson Code : 18.1
Topic : Java.awt.canvas
Time Frame : 30 minutes

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

Computer Science 4 | Page 1 of 3


background color, and then completely redrawn by calling
this canvas's paint method. Note: applications that
override this method should either call super.update(g)
or incorporate the functionality described above into
their own code.
4. createBufferStrategy(int Creates a new strategy for multi-buffering on this
numBuffers) component. Multi-buffering is useful for rendering
performance. This method attempts to create the best
strategy available with the number of buffers supplied.
It will always create a BufferStrategy with that number
of buffers. A page-flipping strategy is attempted first,
then a blitting strategy using accelerated buffers.
Finally, an unaccelerated blitting strategy is used.
5. getBufferStrategy() Returns the BufferStrategy used by this component. This
method will return null if a BufferStrategy has not yet
been created or has been disposed.

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);
}
}

Figure 1.0 Example Output.


Computer Science 4 | Page 2 of 3
NAVIGATE

Learning Exercise: None - Graded

Create a java program a to create a canvas and paint the canvas.

This is the sample output:

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

Prepared by : Leonil Tayco Suarez


Position : Special Science Teacher 3
Campus : PSHS-MRC

Computer Science 4 | Page 3 of 3


© 2020 Philippine Science High School System. All rights reserved. This document may contain proprietary information and may only be released
to third parties with approval of management. Document is uncontrolled unless otherwise marked; uncontrolled documents are not subject to
update notification.

You might also like