// ColorButton.java import java.awt.*; import java.awt.event.*; public class ColorButton extends Canvas implements MouseListener { public static final int ORIGINAL = 1, CENTER = 2, FULL = 3; public boolean hasLife = false; private int alignment; private Color color = null; private boolean raised = true, canChange = true; private ActionListener listener = null; private Image image; public ColorButton(Image _image, int _alignment, Color _color) { super(); image = _image; alignment = _alignment; color = _color; addMouseListener(this); } public void isChangeable(boolean _canChange) { canChange = _canChange; } public void paint(Graphics g) { g.setColor(color); g.fill3DRect(0, 0, getSize().width, getSize().height, raised); if (hasLife) if (alignment == ORIGINAL) g.drawImage(image, 0, 0, this); else if (alignment == FULL) g.drawImage(image, 10, 10, getSize().width, getSize().height, this); else { int x = Math.max((getSize().width - image.getWidth(this))/2, 0); int y = Math.max((getSize().height - image.getHeight(this))/2, 0); g.drawImage(image, x, y, this); } } public Dimension getPreferredSize() { return new Dimension(image.getWidth(this), image.getHeight(this)); } public void addActionListener(ActionListener _listener) { listener = AWTEventMulticaster.add(listener, _listener); } public void mouseEntered(MouseEvent me) { raised = false; repaint(); } public void mouseExited(MouseEvent me) { raised = true; repaint(); } public void mousePressed(MouseEvent me) { if (listener != null) listener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "ColorButton")); if (canChange) { if (hasLife == false) hasLife = true; else hasLife = false; repaint(); } } public void mouseClicked(MouseEvent me) { } public void mouseReleased(MouseEvent me) { } public Color getColor() { return color; } }