1 /* 2 * File: Paint.java 3 * 4 * A paint program 5 * 6 * Copyright: Northeast Parallel Architectures Center 7 * 8 */ 9 10 import java.applet.Applet; 11 import java.awt.*; 12 import java.awt.event.*; 13 14 public class Paint extends Applet implements ActionListener { 15 16 String localTools[] = { "Draw", "Redraw", "Clear" }; 17 Label toolLabel, counterLabel; 18 DrawCanvas canvas; 19 20 public void init() { 21 22 setBackground(Color.white); 23 setForeground(Color.black); 24 setLayout( new BorderLayout() ); 25 26 // Create a canvas and add it to the applet: 27 canvas = new DrawCanvas(); 28 add( canvas, BorderLayout.CENTER ); 29 30 // A generic button: 31 Button button; 32 // A panel of button tools: 33 Panel toolPanel = new Panel(); 34 // Get a list of drawing tools: 35 String tools[] = canvas.getTools(); 36 // The total number of tools: 37 int n = tools.length + localTools.length; 38 // Buttons are arranged vertically: 39 toolPanel.setLayout( new GridLayout( n, 1 ) ); 40 // Add each button to the button panel and register the 41 // applet to listen for button events (i.e., ActionEvents): 42 for ( int i = 0; i < tools.length; i++ ) { 43 toolPanel.add( button = new Button( tools[i] ) ); 44 button.addActionListener( this ); 45 } 46 for ( int i = 0; i < localTools.length; i++ ) { 47 toolPanel.add( button = new Button( localTools[i] ) ); 48 button.addActionListener( this ); 49 } 50 51 // Add the tool panel to the applet: 52 add( toolPanel, BorderLayout.WEST ); 53 54 // Labels for the status line: 55 toolLabel = new Label( canvas.getTool(), Label.CENTER ); 56 counterLabel = new Label(); 57 counterLabel.setAlignment( Label.RIGHT ); 58 setCounterLabel(); 59 60 // Build the status line and add it to the applet: 61 Panel statusLine = new Panel(); 62 statusLine.setLayout( new GridLayout( 1, 3 ) ); 63 statusLine.add( new Label() ); // a placeholder 64 statusLine.add( toolLabel ); 65 statusLine.add( counterLabel ); 66 add( statusLine, BorderLayout.SOUTH ); 67 68 // Register the applet to listen for MouseEvents generated 69 // by the canvas: 70 canvas.addMouseListener( 71 new MouseAdapter() { 72 public void mouseReleased( MouseEvent e ) { 73 setCounterLabel(); 74 } 75 } 76 ); 77 78 } // end method init 79 80 public void setCounterLabel() { 81 Integer objectCounter = new Integer( canvas.getNumObj() ); 82 counterLabel.setText( objectCounter.toString() ); 83 } 84 85 // This method implements the ActionListener interface: 86 public void actionPerformed( ActionEvent e ) { 87 88 Button button = (Button) e.getSource(); 89 String command = e.getActionCommand(); 90 91 if ( command.equals( "Draw" ) ) { 92 button.setLabel( "Fill" ); 93 canvas.setFill( false ); 94 } else if ( command.equals( "Fill" ) ) { 95 button.setLabel( "Draw" ); 96 canvas.setFill( true ); 97 } else if ( command.equals( "Redraw" ) ) { 98 canvas.repaint(); 99 } else if ( command.equals( "Clear" ) ) { 100 canvas.clear(); 101 setCounterLabel(); 102 } else { 103 canvas.setMode( command ); 104 toolLabel.setText( command ); 105 } 106 107 } // end method actionPerformed 108 109 } // end class Paint