Import java. awt. BorderLayout;
Import java. awt. Color;
Import java. awt. Graphics;
Import java. awt. Shape;
Import java. awt. event. MouseEvent;
Import java. awt. event. MouseListener;
Import java. awt. geom. RoundRectangle2D;
Import javax. swing. Icon;
Import javax. swing. JButton;
Import javax. swing. JFrame;
Public class MyButton extends JButton implements MouseListener {
Private Shape shape = null;
Private Color click = new Color (0, 0,113); // the Color of the press.
Private Color quit = new Color (237,234,228); // Color when leaving
Public MyButton (String s ){
Super (s );
AddMouseListener (this );
SetContentAreaFilled (false); // whether to display the area of the peripheral rectangle
}
Public MyButton (){
Super ();
AddMouseListener (this );
}
Public MyButton (String text, Icon icon ){
Super (text, icon );
AddMouseListener (this );
}
Public MyButton (Icon ){
Super (icon );
AddMouseListener (this );
}
Public void setColor (Color c, Color q ){
Click = c;
Quit = q;
}
Public void mouseClicked (MouseEvent e ){
System. out. println ("mouseClicked ");
}
Public void mousePressed (MouseEvent e ){
System. out. println ("mousePressed ");
}
Public void mouseReleased (MouseEvent e ){
System. out. println ("mouseReleased ");
}
Public void mouseExited (MouseEvent e ){
System. out. println ("mouseExited ");
}
Public void mouseEntered (MouseEvent e ){
System. out. println ("mouseEntered ");
}
Public void paintComponent (Graphics g ){
// If you press the button, the color is CLICK or the color is QUIT.
If (getModel (). isArmed ()){
G. setColor (click );
} Else {
G. setcolor (quit );
}
// Fill the rounded rectangle area with other images.
G
. Fillroundrect (0, 0, getsize (). Width-1, getsize (). Height-1,
20, 20 );
// It must be placed at the end or cannot be drawn.
Super. paintcomponent (g );
}
Public void paintborder (Graphics g ){
// Draw the Border Area
G. setcolor (click );
G
. Drawroundrect (0, 0, getsize (). Width-1, getsize (). Height-1,
20, 20 );
}
Public Boolean contains (int x, int y ){
// Determine whether the vertex (X, Y) is in the button
If (shape = null |! (Shape. getBounds (). equals (getBounds ()))){
Shape = new RoundRectangle2D. Float (0, 0, getWidth (), getHeight (),
20, 20 );
}
Return shape. contains (x, y );
}
Public static void main (String [] argv ){
JFrame f = new JFrame ();
F. setSize (400,300 );
MyButton btn = new MyButton ("button ");
F. getContentPane (). add (btn, BorderLayout. NORTH );
F. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
F. setVisible (true );
}
}