import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
public class BotonRedondo extends JButton implements MouseListener
{
private Graphics2D g2d, g2d2;
private BufferedImage bimg;
private Shape AreaCubierta;
private Color descolorido, colorido, ColorClick, BGColor;
private GradientPaint despintado, pintado, ClickPaint;
private boolean mouseDown;
public BotonRedondo(String label)
{
super(label);
setContentAreaFilled(false);
descolorido = new Color(39,164,17);
colorido = new Color(181,0,0);
ColorClick = new Color(255,130,130);
setFocusable(false);
addMouseListener(this);
}
public BotonRedondo(Icon im)
{
super(im);
setContentAreaFilled(false);
descolorido = new Color(0,0,0);
colorido = new Color(181,0,0);
ColorClick = new Color(255,130,130);
addMouseListener(this);
}
public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me) {}
public void mousePressed(MouseEvent me)
{
mouseDown = true;
}
public void mouseReleased(MouseEvent me)
{
mouseDown = false;
}
public void paint(Graphics g)
{
Color col;
if (BGColor == null)
{
if (getParent() != null)
BGColor = getParent().getBackground();
setBackground(BGColor);
}
if (AreaCubierta == null)
AreaCubierta = new Ellipse2D.Float(0, 0, getSize().width-2, getSize().height-2);
if (pintado == null)
pintado = new GradientPaint(0, 0, colorido, 0, getSize().height, Color.white);
if (despintado == null)
despintado = new GradientPaint(0, 0, descolorido, 0, getSize().height, Color.white);
if (ClickPaint == null)
ClickPaint = new GradientPaint(0, getSize().height, ColorClick, 0, 0, Color.white);
g2d = createGraphics();
if (mouseDown)
g2d.setPaint(ClickPaint);
else if (getModel().isArmed())
g2d.setPaint(pintado);
else
g2d.setPaint(despintado);
g2d.fill(AreaCubierta);
if (mouseDown)
col = ColorClick;
else if (getModel().isArmed())
col = colorido;
else
col = descolorido;
g2d.setColor(col);
g2d.drawOval(0, 0, getSize().width-2, getSize().height-2);
g2d.dispose();
g.drawImage(bimg, 0, 0, this);
super.paintComponent(g);
}
protected Graphics2D createGraphics()
{
Graphics2D g2 = null;
if (bimg == null)
bimg = (BufferedImage) createImage(getSize().width, getSize().height);
g2 = bimg.createGraphics();
g2.setBackground(getBackground());
g2.clearRect(0, 0, getSize().width, getSize().height);
Component parent = getParent();
if (parent != null && parent instanceof SceneFrame)
{
Point point = getLocation();
Image theBg = ((SceneFrame) parent).getClip(point.x, point.y, getWidth(), getHeight());
if (theBg != null)
{
int x = 0, y = 0;
if (point.x < 0)
x = -point.x;
if (point.y < 0)
y = -point.y;
g2.drawImage(theBg, x, y, getBackground(), this);
}
}
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
return g2;
}
public boolean contains(int x, int y)
{
if (AreaCubierta == null)
return false;
return AreaCubierta.contains(x, y);
}
}
class SceneFrame extends JPanel
{
private ImageIcon myImage;
private BufferedImage myBufImage;
private JButton lastButton;
public SceneFrame()
{
myImage = new ImageIcon("galaxy.jpg");
setSize(myImage.getIconWidth(), myImage.getIconHeight());
PixelGrabber grabber = new PixelGrabber(myImage.getImage(), 0, 0,
myImage.getIconWidth(), myImage.getIconHeight(), true);
try
{
if (grabber.grabPixels())
{
int[] thePixels = (int[]) grabber.getPixels();
myBufImage = new BufferedImage(myImage.getIconWidth(), myImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
myBufImage.setRGB(0,0, myImage.getIconWidth(), myImage.getIconHeight(), thePixels, 0, myImage.getIconWidth());
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
public Image getClip(int x, int y, int width, int height)
{
if (x < 0)
x = 0;
if (y < 0)
y = 0;
if (x > myBufImage.getWidth() || x + width > myBufImage.getWidth())
width = myBufImage.getWidth() - x;
if (y > myBufImage.getHeight() || y + height > myBufImage.getHeight())
height = myBufImage.getHeight() - y;
if (height < 0 || width < 0)
return null;
return myBufImage.getSubimage(x, y, width, height);
}
public void paint(Graphics g)
{
g.clearRect(0,0,getWidth(),getHeight());
g.drawImage(myImage.getImage(), 0, 0, Color.white, this);
paintComponents(g);
}
}