package components;
import java.awt.*;
import javax.swing.*;
public class xButton extends JButton{
public static final int RECT=0;
public static final int ELLIPSE=1;
public static final int ROUNDRECT=2;
private int cornerCurve=3;
private int shapeState=1;
private ImageIcon icon = null;
public xButton(){
this.setContentAreaFilled(false);
setCursor(new Cursor(Cursor.HAND_CURSOR));
}
/**
* Sets the Icon of the button with the desired Image
*
* @param ii new Icon
*
* @see xchan
*/
public void setImageIcon(ImageIcon ii){
icon = ii;
}
/**
*
* Sets the shape of the button
*
* @param shape 0(Rectangle) 1(Ellipse) 2(RoundRectanlge)
*/
public void setShape(int shape){
shapeState = shape;
}
/**
*
* Changes the corner curve of the Round Rectangle shaped buttons. This will only affect
* buttons with Round Rectangle shapes.
* @param c Corner Curve
*
*/
public void setCornerCurve(int c){
cornerCurve = c;
}
/**
* Returns the state of the button's shape.
* @return an integer valuing the buttons shape state
* @see setShape(int shape)
*/
public int getShape(){
return shapeState;
}
/**
* Returns the value of the curve of the corners of the Round Rectangle of the button.
* @return an integer valuing the curve of the Round Rectangle button's corners
* @see setCornerCurve(int c)
*/
public int getCornerCurve(){
return cornerCurve;
}
@Override
protected void paintComponent(Graphics g) {
if(getModel().isArmed()){
g.setColor(new Color(255,255,255,128));
}
else{
g.setColor(new Color(255,255,255,0));
}
if(icon!=null){
g.drawImage(icon.getImage(),0, 0, null);
}
switch(shapeState){
case 0:
g.fillRect(0, 0, getSize().width-1, getSize().height-1);
break;
case 1:
g.fillOval(0,0, getSize().width-1, getSize().height-1);
break;
case 2:
g.fillRoundRect(0, 0, getSize().width-1, getSize().height, cornerCurve, cornerCurve);
break;
default:
g.fillRect(0, 0, getSize().width-1, getSize().height-1);
break;
}
super.paintComponent(g);
}
@Override
public boolean contains(int i, int i1){
Shape s = new Ellipse2D.Float(0, 0, getSize().width-1, getSize().width-1);
return s.contains(i, i1);
}
public static void main(String args[]){
JFrame frame = new JFrame();
xButton b = new xButton();
frame.getContentPane().add(b);
frame.setVisible(true);
frame.pack();
}
}