// LineDraw - Nick Taylor (14.10.2003) // Import the Abstract Windows Toolkit import java.awt.*; // Import the event handler package so we can close the program neatly import java.awt.event.*; // Import geom package for Java 2D import java.awt.geom.*; public class LineDraw extends Frame { LineDraw() { // Make sure we can close the window with the appropriate button click addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); } public static void main (String[] args) { // Make a Frame -line2D- LineDraw line2D = new LineDraw(); // Frame size will be 200x200 *including* title bar and borders line2D.setSize(200,200); // Make the Frame visible line2D.setVisible(true); } public void paint (Graphics g) { /* paint() will be called whenever the display needs updating. It requires an old-style graphics object -g- and we cast it to a proper Graphics2D object -gg- so we can manipulate it */ Graphics2D gg = (Graphics2D) g; /* Create a shape -lineShape- which is a 2D line defined by two end points (x1,y1,x2,y2) all Floats here but could be Doubles */ Shape lineShape = new Line2D.Float(10,30,180,190); // Finally draw the object -lineShape- gg.draw(lineShape); } }