mmm este esta mucho mas currado:
import java.awt.AWTEvent;
import java.awt.AWTEventMulticaster;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* A circular button.
*
*/
public class CircularButton extends Component
{
/**
* The label on the button.
*
*/
private String _strLabel;
/**
* Constructs a circular button with no label.
*
*/
public CircularButton()
{
this("");
}
/**
* Constructs a circular button with the specified label.
*
*/
public CircularButton(String strLabel)
{
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
enableEvents(AWTEvent.FOCUS_EVENT_MASK);
_strLabel = strLabel;
}
/**
* Gets the label.
*
*/
public String getLabel()
{
return _strLabel;
}
/**
* Sets the label.
*
*/
public void setLabel(String strLabel)
{
_strLabel = strLabel;
invalidate();
repaint();
}
/**
* Is the button pressed?
*
*/
private boolean _boolPressed = false;
/**
* Does the button have the input focus?
*
*/
private boolean _boolFocus = false;
/**
* Paint the circular button.
*
*/
public void paint(Graphics g)
{
Dimension dim = getSize();
int n = (dim.width < dim.height ? dim.width : dim.height) - 1;
// draw the interior
if(_boolPressed)
{
g.setColor(getBackground().darker().darker());
}
else
{
g.setColor(getBackground());
}
g.fillArc(6, 6, n - 12, n - 12, 0, 360);
// draw the perimeter
g.setColor(getBackground().darker().darker().darker().darker());
g.drawArc(6, 6, n - 12, n - 12, 0, 360);
// draw the ring
g.drawArc(0, 0, n, n, 0, 360);
// fill the ring
if (_boolFocus == true)
{
g.drawArc(1, 1, n - 2, n - 2, 0, 360);
g.drawArc(2, 2, n - 4, n - 4, 0, 360);
g.drawArc(3, 3, n - 6, n - 6, 0, 360);
g.drawArc(0, 0, n - 1, n - 1, 0, 360);
g.drawArc(1, 1, n - 1, n - 1, 0, 360);
g.drawArc(1, 1, n - 3, n - 3, 0, 360);
g.drawArc(2, 2, n - 3, n - 3, 0, 360);
g.drawArc(2, 2, n - 5, n - 5, 0, 360);
g.drawArc(3, 3, n - 5, n - 5, 0, 360);
}
// draw the label
Font font = getFont();
if (font != null)
{
FontMetrics fontmetrics = getFontMetrics(font);
g.setColor(getForeground());
g.drawString(_strLabel,
n/2 - fontmetrics.stringWidth(_strLabel)/2,
n/2 + fontmetrics.getMaxDescent());
}
}
/**
* The circular button's minimum size.
*
*/
private static Dimension _dimMinimumSize = new Dimension(100, 100);
/**
* Gets the minimum size.
*
*/
public Dimension getMinimumSize()
{
return new Dimension(_dimMinimumSize);
}
/**
* Gets the preferred size (based on the label).
*
*/
public Dimension getPreferredSize()
{
Font font = getFont();
if (font == null)
{
return new Dimension(_dimMinimumSize);
}
else
{
FontMetrics fontmetrics = getFontMetrics(font);
int n = Math.max(fontmetrics.stringWidth(_strLabel) + 35,
fontmetrics.getHeight() + 35);
return new Dimension(n, n);
}
}
/**
* The chain of action listeners.
*
*/
private ActionListener _al = null;
/**
* Adds an action listener.
*
*/
public void addActionListener(ActionListener al)
{
_al = AWTEventMulticaster.add(_al, al);
}
/**
* Removes an action listener.
*
*/
public void removeActionListener(ActionListener al)
{
_al = AWTEventMulticaster.remove(_al, al);
}
/**
* Determines whether or not the circular button contains
* the point and the specified coordinates.
*/
public boolean contains(int x, int y)
{
Dimension dim = getSize();
int nX = dim.width/2;
int nY = dim.height/2;
return ((nX-x)*(nX-x)+(nY-y)*(nY-y)) <= (nX*nY);
}
/**
* The currently active button.
*
*/
private Component _component = null;
/**
* Process focus events.
*
*/
public void processFocusEvent(FocusEvent e)
{
switch (e.getID())
{
case FocusEvent.FOCUS_GAINED:
if (_boolFocus == false)
{
_boolFocus = true;
repaint();
}
break;
case FocusEvent.FOCUS_LOST:
if (_boolFocus == true)
{
_boolFocus = false;
repaint();
}
break;
}
super.processFocusEvent(e);
}
/**
* Process mouse events.
*
*/
public void processMouseEvent(MouseEvent e)
{
switch (e.getID())
{
case MouseEvent.MOUSE_PRESSED:
requestFocus();
_component = e.getComponent();
_boolPressed = true;
repaint();
break;
case MouseEvent.MOUSE_RELEASED:
_component = null;
if (_boolPressed == true)
{
if (_al != null)
{
_al.actionPerformed
(
new ActionEvent
(
this,
ActionEvent.ACTION_PERFORMED,
_strLabel,
e.getModifiers()
)
);
}
_boolPressed = false;
repaint();
}
break;
case MouseEvent.MOUSE_ENTERED:
if (_component == e.getComponent() && _boolPressed == false)
{
_boolPressed = true;
repaint();
}
break;
case MouseEvent.MOUSE_EXITED:
if (_boolPressed == true)
{
_boolPressed = false;
repaint();
}
break;
}
super.processMouseEvent(e);
}
/**
* Process mouse motion events.
*
*/
public void processMouseMotionEvent(MouseEvent e)
{
switch (e.getID())
{
case MouseEvent.MOUSE_DRAGGED:
if (_component == e.getComponent() &&
!contains(e.getX(), e.getY()) &&
_boolPressed == true)
{
_boolPressed = false;
repaint();
}
else if (_component == e.getComponent() &&
contains(e.getX(), e.getY()) &&
_boolPressed == false)
{
_boolPressed = true;
repaint();
}
break;
}
super.processMouseMotionEvent(e);
}
}
Hereda de un Componente en vez de un boton, se tiene que currar mas movidillas pero queda mejor.
Salu2o3