// // COSC 286 Project Template // Fall 1998 // // This Java application provides a basic shell for implementing graphics // algorithms. If you are working on project 1, then make a copy of this // file named "Project1.java". Then do a global search and replace the // string "Template" with "1". Most modifications should take place in // the paint() method of the DrawingArea class. However, keep in mind that // you may have to implement additional classes and methods to complete the // project. // // You may also want to change the dimensions of the window size in the // main procedure of the ProjectTemplate class. // // Compile your project using the command: // // javac Project1.java // // Then run your project using the command: // // java Project1 // import java.awt.*; import java.awt.event.*; /*=======================================================================*/ public class ProjectTemplate extends Frame { public ProjectTemplate(int w, int h) { super("COSC 286 Project Template"); // Create the window this.setSize(w, h); // Specify frame's size this.setResizable(false); // Prevent resizing (supposedly) // Create a ScrollPane ScrollPane pane = new ScrollPane(ScrollPane.SCROLLBARS_NEVER); pane.setSize(w, h); // Specify the pane's size this.add(pane, "Center"); // Add it to the frame DrawingArea scribble; scribble = new DrawingArea(this); // Create a drawing area pane.add(scribble); // Add it to the ScrollPane MenuBar menubar = new MenuBar(); // Create a menubar this.setMenuBar(menubar); // Add it to the frame Menu file = new Menu("File"); // Create a File menu menubar.add(file); // Add to menubar // Create a quit menu item with a shortcut, and add it to the menu MenuItem q; file.add(q = new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q))); // Create and register an action listener object for the menu item. q.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); // Create and register action listener for the window closing event. addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // Set the window size and pop it up. this.pack(); this.show(); } // ProjectTemplate public static void main(String[] args) { new ProjectTemplate(300, 300); } // main } // ProjectTemplate class /*=======================================================================*/ class DrawingArea extends Component { protected Frame frame; public DrawingArea(Frame frame) { this.frame = frame; } // DrawingArea public void setPixel(Graphics g, int x, int y) { g.drawLine(x, y, x, y); } // setPixel public void paint(Graphics g) { Dimension d = getSize(); // Dimensions of the drawing area g.setColor(Color.black); // Sets the drawing color to black setPixel(g, 20, 30); // Draws a single pixel g.drawLine(0, 0, d.width-1, d.height-1); // Draws a line across the screen } // paint } // DrawingArea class