/*
* Drawing in a Canvas
* WRONG WAY
*/
// Can import whole packages this way
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main (String [] args) {
java.awt.EventQueue.invokeLater (new Runnable() {
public void run() {
new Main ();
}
});
}
public Main () {
// Window setup
JFrame frame = new JFrame();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (300, 300);
// Put a Canvas in
Canvas canvas = new Canvas (); //*1 Create a canvas and put in our window
frame.add (canvas); //*1
// Show the window
frame.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
}
}