FileDocCategorySizeDatePackage
ScribbleApp.javaAPI DocExample6914Sat Jan 24 10:44:32 GMT 2004je3.gui

ScribbleApp

public class ScribbleApp extends JFrame
This JFrame subclass is a simple "paint" application.

Fields Summary
ScribblePane
scribblePane
Constructors Summary
public ScribbleApp()
This constructor creates the GUI for this application.

	super("Scribble");  // Call superclass constructor and set window title

	// All content of a JFrame (except for the menubar) goes in the
	// Frame's internal "content pane", not in the frame itself.
	// The same is true for JDialog and similar top-level containers.
	Container contentPane = this.getContentPane();

	// Specify a layout manager for the content pane
	contentPane.setLayout(new BorderLayout());

	// Create the main scribble pane component, give it a border, and
	// a background color, and add it to the content pane
	scribblePane = new ScribblePane();
	scribblePane.setBorder(new BevelBorder(BevelBorder.LOWERED));
	scribblePane.setBackground(Color.white);
	contentPane.add(scribblePane, BorderLayout.CENTER);

	// Create a menubar and add it to this window.  Note that JFrame
	// handles menus specially and has a special method for adding them
	// outside of the content pane.
	JMenuBar menubar = new JMenuBar();  // Create a menubar
	this.setJMenuBar(menubar);          // Display it in the JFrame

	// Create menus and add to the menubar
	JMenu filemenu = new JMenu("File");
	JMenu colormenu = new JMenu("Color");
	menubar.add(filemenu);
	menubar.add(colormenu);
	
	// Create some Action objects for use in the menus and toolbars.
	// An Action combines a menu title and/or icon with an ActionListener.
	// These Action classes are defined as inner classes below.
	Action clear = new ClearAction();
	Action quit = new QuitAction();
	Action black = new ColorAction(Color.black);
	Action red = new ColorAction(Color.red);
	Action blue = new ColorAction(Color.blue);
	Action select = new SelectColorAction();

	// Populate the menus using Action objects
	filemenu.add(clear);
	filemenu.add(quit);
	colormenu.add(black);
	colormenu.add(red);
	colormenu.add(blue);
	colormenu.add(select);

	// Add a sub-menu for selecting a look-and-feel.
	// The LookAndFeelPrefs utility class is later in the chapter.
	colormenu.add(new JSeparator());
	colormenu.add(LookAndFeelPrefs.createLookAndFeelMenu(ScribbleApp.class,
	           new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		          SwingUtilities.updateComponentTreeUI(ScribbleApp.this);
		      }
		   }));

	// Now create a toolbar, add actions to it, and add it to the
	// top of the frame (where it appears underneath the menubar)
	JToolBar toolbar = new JToolBar();
	toolbar.add(clear);
	toolbar.add(select);
	toolbar.add(quit);
	contentPane.add(toolbar, BorderLayout.NORTH);

	// Create another toolbar for use as a color palette and add to 
	// the left side of the window.
	JToolBar palette = new JToolBar();
	palette.add(black);
	palette.add(red);
	palette.add(blue);
	palette.setOrientation(SwingConstants.VERTICAL);
	contentPane.add(palette, BorderLayout.WEST);
    
Methods Summary
public static voidmain(java.lang.String[] args)
The main method instantiates an instance of the class, sets it size, and makes it visible on the screen

	// Set the look-and-feel for the application.  
	// LookAndFeelPrefs is defined elsewhere in this package.
	LookAndFeelPrefs.setPreferredLookAndFeel(ScribbleApp.class);

	ScribbleApp scribble = new ScribbleApp();

	// Handle window close requests
	scribble.addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) { System.exit(0); }
	    });

	// Set the window size and make it visible.
	scribble.setSize(500, 300);
	scribble.setVisible(true);