Java rounded Rectangle Button

Source: Internet
Author: User

 

 

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 );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.