Draw1 (the WRONG way)

Main.java  [download]

/*
 * 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);
	Container content = getContentPane();
	
	// Put a Canvas in
	MyCanvas canvas = new MyCanvas ();
	content.add (canvas);
    
	// Show the window
	setVisible (true); //*1 Wrong way to draw

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

MyCanvas.java  [download]

import java.awt.*;
import javax.swing.*;

public class MyCanvas extends JPanel { //*1 Use a plain JPanel as our "Canvas"
    //*2 Nothing for it to do in this version 
} //*2