Draw1 (the WRONG way): Main.java

/*
 * Drawing in a Canvas
 * WRONG WAY
 */

// Can import whole packages this way
import java.awt.*;
import javax.swing.*;

public class Main extends JFrame {
    public static void main (String [] args) {
	java.awt.EventQueue.invokeLater (new Runnable() {
            public void run() {
		new Main ();
            }
        });
    }

    public Main () {
	// Window setup
	setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
	setSize (300, 300);
	
	// Put a Canvas in
	Canvas canvas = new Canvas (); //*1 Create a canvas and put in our window
	add (canvas); //*1
    
	// Show the window
	setVisible (true); //*2 Wrong way to draw

	// WRONG WAY -- Now draw on the canvas //*2
	System.out.println ("Drawing line now..."); //*2
	canvas.getGraphics().drawLine (50, 50, 100, 50); //*2
    }
}

[download file]