Home : Course Map : Chapter 7 : Java : Supplements :
The Canvas Component
JavaTech
Course Map
Chapter 7

Introduction
Event Overview
Event Processing
Button Events
  Demo 1
 Demo 2
Mouse Events
  Demo3

More Components
  Demo 4  Demo 5
  Demo 6  Demo 7

LayoutManagers-1
  Demo 8     Demo 9
  Demo 10  Demo 11
  Demo 12

LayoutManagers-2
  Demo 13  Demo 14
  Demo 15  Demo 16
  Demo 17

Inner Classes
Anonymous Class
Adapter Classes
  Demo 18  Demo 19
Frames & Menus
  Demo 20  Demo 21
Exercises

    Supplements
AWT Components
  Button
     Demo 1
  Canvas
     Demo 2
  AWT GUI Demo
     Demo 3
Swing Dialogs
JOptionPane Dialog
  Demo 1
JDialog
  Demo 2
UI Enhancement: P1
  Demo 1   Demo 2
  Demo 3

UI Enhancement: P2
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The Canvas component provides a blank area on which to draw. The Canvas class extends the Component class and adds little additional functionality other than offering an empty display.

You can also paint on a Panel component, though this wastes the Container capabilities that it inherits. In many of the applet programs in this course we paint on the applet's panel. Note that while Swing has a parallel class for most of the other AWT components (e.g. JButton vs. Button) but does not include a parallel JCanvas class. Instead, Swing provides JPanel for drawing. (It can be said, though, that there is a certain logical consistency in offering a component intended just as a drawing surface rather than also a container/drawing surface combo.)

The applet shown below uses a Canvas subclass as a component on which to draw a message.

CanvasApplet.java

 

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/** This program demonstrates the use of the
  * Canvas component for drawing.
 **/
public class CanvasApplet extends Applet
                           implements ActionListener
{
  MyCanvas fCanvas;
  Button fButton;

  /** Create an interface with a Canvas and button.**/
  public void init () {
    // Use a borderlayout to control where
    // the components go.
    setLayout (new BorderLayout ());

    // Create an instance of myCanvas
    fCanvas = new MyCanvas ();
    add ("North", fCanvas);

    // Set the canvas to a fixed size.
    fCanvas.setSize (200,120);

    // Make a button and use this applet
    // as it's listener
    fButton = new Button ("Push");
    fButton.addActionListener (this);

    // Use a panel to fillup the rest of the
    // space not used by the canvas on top.
    Panel p = new Panel ();
    setLayout (new BorderLayout ());
    p.setBackground (Color.cyan);

    // And put the button in the middle of the panel
    p.add ("Center", fButton);
    add ("South", p);

  } //init

  /** Each button click will redraw the canvas. **/
  public void actionPerformed  (ActionEvent e) {
    fCanvas.repaint ();
  }

} // class CanvasApplet

/** Create a Canvas subclass for drawing.
  * It will keep track of the number of times
  * that it is painted and draw that number on
  * its display.
 **/
class MyCanvas extends Canvas {

  int fNumCalls;

  MyCanvas () {
    fNumCalls = 0;
    setBackground (Color.yellow);
  } // ctor

  public void paint (Graphics g) {
    g.setColor (Color.black);
    fNumCalls++;
    String msg = "Number clicks = " + fNumCalls;
    // Draw at a hardwired coordinate position.
    g.drawString (msg, 40, 50);
  } //paint

} // class MyCanvas

 

Latest update: March 8, 2006

           Tech
Histogram UI
  Demo 1
Probablity Distrib.
  Demo 2 Demo 3
RejectionMethod
Histogram Stats
  Demo 4
Exercises

           Physics
Sim & Randomness
Custom Prob. Dist.
   Demo 1
Histogram Dist.
   Demo 2
Monte Carlo
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.