import javax.swing.*; import javax.swing.plaf.*; import java.awt.*; import java.awt.event.*; import java.util.*; /** * This class implements a simple drawing program in java * * @author Stuart Hansen * @version September 28, 2008 */ public class DrawProgram extends JFrame { // These elements form the GUI of the application DrawPanel drawPanel = new DrawPanel(); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu ("File"); JMenuItem colorItem = new JMenuItem("Color"); JMenuItem clearItem = new JMenuItem("Clear"); JMenuItem exitItem = new JMenuItem ("Exit"); public DrawProgram () { super ("Line Draw"); // add the menu to the application Frame setJMenuBar (menuBar); menu.add(colorItem); menu.add(clearItem); menu.add(exitItem); menuBar.add(menu); // setup the drawPanel getContentPane().add(drawPanel); drawPanel.setBackground(Color.white); // Set the current Color drawPanel.setColor(Color.black); // Change the drawing color colorItem.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { Color oldColor = drawPanel.getColor(); Color newColor = JColorChooser.showDialog(null, "Choose a new color", oldColor); if (newColor != null) drawPanel.setColor(newColor); } } ); // Clear the drawing by replacing the DrawingPanel clearItem.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { drawPanel.reset(); //repaint(); } } ); // Exit the system elegantly exitItem.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { System.exit(0); } } ); // Set the application window's properties setSize (400, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // display the application window setVisible(true); } // a very simple main program public static void main (String args[]) { new DrawProgram(); } }