Scribble Pad
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package je3.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
*
* @author MBC
*/
public class ScribbleApp extends JFrame
{
public static void main(String[] args)
{
LookAndFeelPrefs.setPreferredLookAndFeel(ScribbleApp.class);
ScribbleApp scribble = new ScribbleApp();
scribble.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
scribble.setSize(500, 300);
scribble.setVisible(true);
}
ScribblePane scribblePane;
public ScribbleApp()
{
super("Scribble");
Container contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());
scribblePane = new ScribblePane();
scribblePane.setBorder(new BevelBorder(BevelBorder.LOWERED));
scribblePane.setBackground(Color.white);
contentPane.add(scribblePane, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenu colorMenu = new JMenu("Color");
menuBar.add(fileMenu);
menuBar.add(colorMenu);
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();
fileMenu.add(clear);
fileMenu.add(quit);
colorMenu.add(black);
colorMenu.add(red);
colorMenu.add(blue);
colorMenu.add(select);
colorMenu.add(new JSeparator());
colorMenu.add(LookAndFeelPrefs.createLookAndFeelMenu(ScribbleApp.class, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
SwingUtilities.updateComponentTreeUI(ScribbleApp.this);
}
}));
JToolBar toolbar = new JToolBar();
toolbar.add(clear);
toolbar.add(select);
toolbar.add(quit);
contentPane.add(toolbar, BorderLayout.NORTH);
JToolBar palette = new JToolBar();
palette.add(black);
palette.add(red);
palette.add(blue);
palette.setOrientation(SwingConstants.VERTICAL);
contentPane.add(palette, BorderLayout.WEST);
}
class ClearAction extends AbstractAction
{
public ClearAction()
{
super("Clear");
}
public void actionPerformed(ActionEvent e)
{
scribblePane.clear();
}
}
class QuitAction extends AbstractAction
{
public QuitAction()
{
super("Quit");
}
public void actionPerformed(ActionEvent e)
{
int response = JOptionPane.showConfirmDialog(ScribbleApp.this, "Really Quit?");
if (response == JOptionPane.YES_OPTION)
System.exit(0);
}
}
class ColorAction extends AbstractAction
{
Color color;
public ColorAction(Color color)
{
this.color = color;
putValue(Action.SMALL_ICON, new ColorIcon(color));
}
public void actionPerformed(ActionEvent e)
{
scribblePane.setForeground(color);
}
}
static class ColorIcon implements Icon
{
Color color;
public ColorIcon(Color color)
{
this.color = color;
}
public int getIconHeight()
{
return 16;
}
public int getIconWidth()
{
return 16;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(color);
g.fillRect(x, y, 16, 16);
}
}
class SelectColorAction extends AbstractAction
{
public SelectColorAction()
{
super("Select Color...");
}
public void actionPerformed(ActionEvent e)
{
Color color = JColorChooser.showDialog(ScribbleApp.this, "Select Drawing Color",
scribblePane.getForeground());
if (color != null)
scribblePane.setForeground(color);
}
}
}