/** * COSC 286 Project Template * Spring 2007 * * This Java application provides a basic shell for implementing graphics * algorithms. If you are working on project 2, then make a copy of this * file named "p2.java". Then do a global search and replace the string * "ProjectTemplate" with "p2". * * When coding your project, most modifications should occur 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 p2.java * * Then run your project using the command: * * java p2 */ import java.awt.*; import java.awt.event.*; /*=======================================================================*/ 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.setColor(new Color(128,128,128)); // Sets the drawing color to gray g.drawLine(0, 0, d.width-1, d.height-1); // Draws a line across the screen g.setColor(new Color(255,0,0)); // Sets the drawing color to red g.drawLine(d.width-1, 0, 0, d.height-1); // Draws a line across the screen } // paint } // DrawingArea class /*=======================================================================*/ 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 image; image = new DrawingArea( this ); // Create a drawing area pane.add( image ); // 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.setVisible( true ); } // ProjectTemplate public static void main( String[] args ) { try { new ProjectTemplate( 300, 300 ); } // try catch( Exception e ) { System.out.println( e.getMessage() ); e.printStackTrace(); } // catch } // main } // ProjectTemplate class