/** * GraphicsButton is a parent class for animated buttons. By itself, it * implements a simple round button useful for testing. * * Copyright &copy; 1996-1998 Martin Minow. All Rights Reserved.<p> * * Permission to use, copy, modify, and redistribute this software and its * documentation for personal, non-commercial use is hereby granted provided that * this copyright notice and appropriate documentation appears in all copies. This * software may not be distributed for fee or as part of commercial, "shareware," * and/or not-for-profit endevors including, but not limited to, CD-ROM collections, * online databases, and subscription services without specific license.<p> * * @author <a href="mailto:minow@apple.com">Martin Minow</a> * @version 1.0 * Set tabs every 8 characters. */package org.minow.applets.sunsphere;import java.applet.Applet;import java.awt.*;import java.awt.event.*;import java.util.*;/* * All image-drawing interactive classes are subclasses of GraphicsButton. * This is needed so that nextEventID is unique across all classes in * the applet. */class GraphicsButton extends Component	implements MouseListener{    public static final int	defaultButtonSize   = 12;    protected boolean	        mouseDown;	    /* User clicking us now?	*/    protected boolean		mouseInButton;	    /* Click, still in button?	*/    protected int		buttonWidth;    protected int		buttonHeight;    public boolean		logMouseEvents	    = false;	/* Debug only	*/    private static int nextEventID		    = AWTEvent.RESERVED_ID_MAX + 1;    private int			eventID		    = nextEventID++;    private String		label		    = "GraphicsButton " + eventID;    private String		actionCommand	    = null;    private ActionListener	listeners	    = null;        public GraphicsButton()    {	this(	    defaultButtonSize,	    defaultButtonSize	);    }    /**     * Create a new GraphicsButton. This will usually be subclassed.     * @param	buttonWidth	    The width of the button.     * @param	buttonHeigh	    The height of the button.     */    public GraphicsButton(    	    int			buttonWidth,    	    int			buttonHeight	)    {    	super();    	this.buttonWidth	= buttonWidth;    	this.buttonHeight	= buttonHeight;	addMouseListener(this);		/* We don't add a MouseMotion listener */    }    public int getButtonWidth()    {    	return (buttonWidth);    }    public int getButtonHeight()    {        return (buttonHeight);    }    public void setButtonWidth(	    int			buttonWidth	)    {    	this.buttonWidth	= buttonWidth;    	repaint();    }    public void setButtonHeight(	    int			buttonHeight	)    {    	this.buttonHeight	= buttonHeight;	repaint();    }    /*     * Duplicate the event management methods in the Button class.     */    public int getEventID()    {	return (eventID);    }    public void setEventID(	    int			eventID	)    {	this.eventID = eventID;    }    public String getLabel()    {	return (label);    }    public void setLabel(	    String	    label	)    {	this.label	= label;    }    public Dimension getPreferredSize()		/* Java 1.1	    */    {	Dimension	    d = new Dimension(buttonWidth, buttonHeight);	return (d);    }    public Dimension getMinimumSize()    {	return (getPreferredSize());    }    public String getActionCommand()    {	return ((actionCommand == null) ? getLabel() : actionCommand);    }    public void setActionCommand(	    String	    actionCommand	)    {	this.actionCommand  = actionCommand;    }    /**     * Subclasses will normally override paint. This is a     * sample implementation for testing and debugging.     */    public void paint(	    Graphics	    g	)    {    	logMouseEvent("paint");	Dimension d	= size();	int x		= (d.width - buttonWidth) / 2;	int y		= (d.height - buttonHeight) / 2;	g.setColor(getForeground());	g.drawOval(x, y, buttonWidth - 1, buttonHeight - 1);	if (mouseDown && mouseInButton) {	    g.setXORMode(getBackground());	    g.fillOval(x+1, y+1, buttonWidth - 2, buttonHeight - 2);	    g.setPaintMode();	}    }    /*     * Event management classes.     */    public synchronized void addActionListener(	    ActionListener	listener	)    {	listeners = AWTEventMulticaster.add(listeners, listener);    }    public synchronized void removeActionListener(	    ActionListener	listener	)    {	listeners = AWTEventMulticaster.remove(listeners, listener);    }    protected void processEvent(	    AWTEvent		event	)    {	if (event instanceof ActionEvent) {	    processActionEvent((ActionEvent) event);	}	else {	    super.processEvent(event);	}    }    /*     * Subclasses call processActionEvent when they're clicked.     */    protected void processActionEvent(	    ActionEvent		event	)    {	if (listeners != null) {	    listeners.actionPerformed(event);	}    }    /*     * MouseEvent handler.     */    public void mouseClicked(    	    MouseEvent		event    	)    {     	logMouseEvent("mouseClicked");   	return;	/* Handled in the mouseReleased method */    }    public void mouseEntered(    	    MouseEvent		event    	)    {    	mouseInButton		= true;    	logMouseEvent("mouseEntered");    	if (mouseDown) {    	    repaint();    	}    	return;    }    public void mouseExited(    	    MouseEvent		event    	)    {    	mouseInButton		= false;    	logMouseEvent("mouseExited");    	if (mouseDown) {    	    repaint();    	}    	return;    }    public void mousePressed(    	    MouseEvent		event    	)    {    	mouseInButton		= true;    	mouseDown		= true;    	logMouseEvent("mousePressed");    	repaint();    	return;    }    public void mouseReleased(    	    MouseEvent		event    	)    {    	logMouseEvent("mouseReleased");    	if (mouseInButton && mouseDown) {    	    processActionEvent(new ActionEvent(this, getEventID(), getActionCommand()));	}	mouseInButton		= false;	mouseDown		= false;	repaint();    	return;    }    protected String paramString()    {	return (getLabel());    }    public void logMouseEvent(		/* Debug only */    	    String		which    	)    {    	if (logMouseEvents) {	/* Enable to log events */	    System.out.println(which    		+ ": mouseDown = " + mouseDown    		+ ", mouseInButton = " + mouseInButton    	    );    	}    }    /*     * Stand-along main program for testing.     */    public static void main(	String[]	args    )    {	Frame window	= new Frame("Graphics Button Test");	window.addWindowListener(	    new WindowAdapter()	    {		public void windowClosing(			WindowEvent	event		    )		{		    System.exit(0);		}	    }	);	GraphicsButton button	= new GraphicsButton();	button.logMouseEvents	= true;	button.addActionListener(	    new ActionListener() {		public void actionPerformed(ActionEvent e) {		    System.out.println("Graphics button clicked.");		}	   }	);	window.setLayout(new FlowLayout());	window.add(button);	window.setSize(270, 270);	window.pack();	window.setVisible(true);    }    }