import java.awt.*; import java.awt.event.*; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Vector; public class GraphicsCanvas extends Frame { static final Color[] color_types = { Color.black , Color.blue , Color.cyan , Color.darkGray , Color.gray , Color.green , Color.lightGray, Color.magenta , Color.orange , Color.pink , Color.red , Color.white , Color.yellow }; static final String[] color_names = { "Black" , "Blue" , "Cyan" , "Dark Gray" , "Gray" , "Green" , "Light Gray", "Magenta" , "Orange" , "Pink" , "Red" , "White" , "Yello" }; Vector objects = new Vector( ); String whichFunc = null; Color whichColor = Color.black; ActionListener objectListener = new ActionListener( ) { public void actionPerformed( ActionEvent e ) { GraphicsCanvas.this.whichFunc = e.getActionCommand( ); } }; ActionListener colorListener = new ActionListener( ) { public void actionPerformed( ActionEvent e ) { GraphicsCanvas.this.whichColor = Color.decode( e.getActionCommand( ) ); } }; public GraphicsCanvas ( ) { super( "Paint Program" ); //////////////////////////////////////// // Set the MenuBar for this Frame. MenuBar menuBar = new MenuBar( ); Menu objMenu = new Menu( "Objects" ); Menu colMenu = new Menu( "Colors" ); //////////////////////////////////////// // Open configuration file and read in // Class names. try { FileReader fRead = new FileReader( "Config.txt" ); BufferedReader rRead = new BufferedReader( fRead ); String tempString = null; while( (tempString = rRead.readLine( )) != null) { MenuItem item = new MenuItem( tempString ); item.setActionCommand( tempString ); item.addActionListener( objectListener ); objMenu.add( item ); } rRead.close( ); } catch (FileNotFoundException e) { System.err.println( "Can't find Config.txt" ); System.exit( 1 ); } catch (IOException e) { System.err.println( "Error in file stream" ); System.exit( 2 ); } for (int i = 0; i < color_types.length; i++ ) { MenuItem item = new MenuItem( color_names[i] ); item.setActionCommand( "" + color_types[i].getRGB( ) ); item.addActionListener( colorListener ); colMenu.add( item ); } menuBar.add( objMenu ); menuBar.add( colMenu ); setMenuBar( menuBar ); //////////////////////////////////////// // Add WindowListener to allow closing // of Window. addWindowListener( new WindowAdapter( ) { public void windowClosing( WindowEvent e ) { GraphicsCanvas.this.dispose( ); System.exit( 0 ); } }); //////////////////////////////////////// // Add MouseListener to create and reg- // ister new GraphicsObjects when the // mouse button is pressed within the // Frame. addMouseListener( new MouseAdapter( ) { public void mousePressed( MouseEvent e ) { if (whichFunc != null) { //////////////////////////////////////// // If a GraphicsObject has been speci- // fied create, initialize, and register // the object. // // Note that this is done dynamically so // that new GraphicsObjects can be made // and easily utilized by the Graphics- // Canvas. try { Class c = Class.forName( whichFunc ); GraphicsObject f = (GraphicsObject) c.newInstance( ); f.setColor( GraphicsCanvas.this.whichColor ); f.setContainer( GraphicsCanvas.this ); f.setLocation( e.getX( ), e.getY( ) ); GraphicsCanvas.this.objects.addElement( f ); } catch (ClassNotFoundException excep) { System.err.println( "No such graphics class" ); GraphicsCanvas.this.dispose( ); System.exit( 1 ); } catch (InstantiationException excep) { System.err.println( "Could not create class " + whichFunc ); GraphicsCanvas.this.dispose( ); System.exit( 2 ); } catch (IllegalAccessException excep) { System.err.println( "No default constructor for " + whichFunc ); GraphicsCanvas.this.dispose( ); System.exit( 3 ); } } } }); setSize( 300, 400 ); show(); } public void update( Graphics g) { paint( g ); } public void paint( Graphics g ) { for( int i = 0; i < objects.size( ); i++ ) ((GraphicsFunction) objects.elementAt( i )).draw( ); } //////////////////////////////////////// // Start application via main. public static void main( String[] args ) { new GraphicsCanvas(); } }