import javax.swing.*; import javax.swing.plaf.*; import java.awt.*; import java.awt.event.*; import java.util.*; /** * This program implements a simple drawing program in java * * @author Stuart Hansen * @version December 22, 2000 * modified October 16, 2001 -- added alternative colors * modified February 5, 2002 -- made resizing work * modified February 4, 2003 -- changed to drawing with lines */ public class LineDraw extends JFrame { /** draw is the area of the JFrame we actually draw in */ DrawPanel drawPanel; /** the constructor */ public LineDraw () { super ("Line Draw"); // setup the drawing area and add it to the application Frame drawPanel = new DrawPanel(); getContentPane().add(drawPanel); // add the menu to the application Frame setJMenuBar (new DrawMenuBar() ); // Set the application window's properties setSize (400, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // display the application window setVisible(true); } /** This class represents our menu */ private class DrawMenuBar extends JMenuBar { // the menu and menu items JMenu menu = new JMenu ("File"); JMenuItem colorItem = new JMenuItem("Color"); JMenuItem clearItem = new JMenuItem("Clear"); JMenuItem exitItem = new JMenuItem ("Exit"); // The menu bar contains a menu with two items public DrawMenuBar () { // Change the drawing color colorItem.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { // We save the old color in case the user cancels Color oldColor = drawPanel.getCurrentColor(); Color currentColor = JColorChooser.showDialog(null, "Choose a new color", oldColor); if (currentColor != null) drawPanel.setCurrentColor(currentColor); } } ); // Clear the drawing by replacing the DrawingPanel clearItem.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { drawPanel.clear(); } } ); // Exit the system elegantly exitItem.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { System.exit(0); } } ); // Add the menu items to the menu and the menu to the bar menu.add(colorItem); menu.add(clearItem); menu.add(exitItem); add(menu); } } // a very simple main program public static void main (String args[]) { new LineDraw(); } }