java swing定制jbutton 圆角显示

/**
 * @(#)RJButton.java  0.1.0  2007-9-11
 
*/
package  ruislan.rswing;

import
 java.awt.AlphaComposite;
import
 java.awt.Color;
import
 java.awt.Dimension;
import
 java.awt.Font;
import
 java.awt.GradientPaint;
import
 java.awt.Graphics;
import
 java.awt.Graphics2D;
import
 java.awt.Point;
import
 java.awt.RenderingHints;
import
 java.awt.Shape;
import
 java.awt.event.MouseAdapter;
import
 java.awt.event.MouseEvent;
import
 java.awt.geom.Arc2D;
import
 java.awt.geom.GeneralPath;
import
 java.awt.geom.RoundRectangle2D;

import
 javax.swing.JButton;

/**

 * Custom JButton
 * 
 * 
@version  0.1.0
 * 
@author
 ruislan <a href="mailto:[email protected]"/>
 
*/

public class RButton extends  JButton {
    
private static final long serialVersionUID = 39082560987930759L
;
    
public static final Color BUTTON_COLOR1 = new Color(205255205
);
    
public static final Color BUTTON_COLOR2 = new Color(5115447
);
    
//
 public static final Color BUTTON_COLOR1 = new Color(125, 161, 237);
    
// public static final Color BUTTON_COLOR2 = new Color(91, 118, 173);

    public static final Color BUTTON_FOREGROUND_COLOR =  Color.WHITE;
    
private boolean
 hover;
    
private int
 style;
    
public static final int ROUND_RECT = 0
;
    
public static final int LEFT_ROUND_RECT = 1
;
    
public static final int RIGHT_ROUND_RECT = 2
;
    
public static final int BALL = 3
;
    
public static final int STAR = 4
;

    
public
 RButton() {
        
this
(ROUND_RECT);
    }

    
public RButton(int
 style) {
        
this.style =
 style;
        
if (BALL ==
 style) {
            setPreferredSize(
new Dimension(4242
));
        } 
else if (STAR ==
 style) {
            setPreferredSize(
new Dimension(4242
));
        }
        setFont(
new Font("system", Font.PLAIN, 12
));
        setBorderPainted(
false
);
        setForeground(BUTTON_COLOR2);
        setFocusPainted(
false
);
        setContentAreaFilled(
false
);
        addMouseListener(
new
 MouseAdapter() {
            @Override
            
public void
 mouseEntered(MouseEvent e) {
                setForeground(BUTTON_FOREGROUND_COLOR);
                hover 
= true
;
                repaint();
            }

            @Override
            
public void
 mouseExited(MouseEvent e) {
                setForeground(BUTTON_COLOR2);
                hover 
= false
;
                repaint();
            }
        });
    }

    @Override
    
protected void
 paintComponent(Graphics g) {
        Graphics2D g2d 
=
 (Graphics2D) g.create();
        
int h =
 getHeight();
        
int w =
 getWidth();
        
float tran =
 1F;
        
if (!
hover) {
            tran 
= 0.3F
;
        }

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        GradientPaint p1;
        GradientPaint p2;
        
if
 (getModel().isPressed()) {
            p1 
= new GradientPaint(00new Color(000), 0, h - 1
,
                    
new Color(100100100
));
            p2 
= new GradientPaint(01new Color(00050), 0, h - 3
,
                    
new Color(255255255100
));
        } 
else
 {
            p1 
= new GradientPaint(00new Color(100100100), 0, h - 1
,
                    
new Color(000
));
            p2 
= new GradientPaint(01new Color(255255255100), 0
,
                    h 
- 3new Color(00050
));
        }
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                tran));
        GradientPaint gp 
= new GradientPaint(0.0F0.0F, BUTTON_COLOR1, 0.0F
,
                h, BUTTON_COLOR2, 
true
);
        g2d.setPaint(gp);
        
switch
 (style) {
        
case
 ROUND_RECT: {
            RoundRectangle2D.Float r2d 
= new RoundRectangle2D.Float(00
,
                    w 
- 1, h - 12020
);
            Shape clip 
=
 g2d.getClip();
            g2d.clip(r2d);
            g2d.fillRect(
00
, w, h);
            g2d.setClip(clip);
            g2d.setPaint(p1);
            g2d.drawRoundRect(
00, w - 1, h - 12020
);
            g2d.setPaint(p2);
            g2d.drawRoundRect(
11, w - 3, h - 31818
);
            
break
;
        }
        
case
 LEFT_ROUND_RECT: {
            RoundRectangle2D.Float r2d 
= new RoundRectangle2D.Float(00
,
                    (w 
- 1+ 20, h - 12020
);
            Shape clip 
=
 g2d.getClip();
            g2d.clip(r2d);
            g2d.fillRect(
00
, w, h);
            g2d.setClip(clip);
            g2d.setPaint(p1);
            g2d.drawRoundRect(
00, (w - 1+ 20, h - 12020
);
            g2d.setPaint(p2);
            g2d.drawRoundRect(
11, (w - 3+ 20, h - 31818
);
            g2d.setPaint(p1);
            g2d.drawLine(w 
- 11, w - 1
, h);
            g2d.setPaint(p2);
            g2d.drawLine(w 
- 22, w - 2, h - 1
);
            
break
;
        }
        
case
 RIGHT_ROUND_RECT: {
            RoundRectangle2D.Float r2d 
= new RoundRectangle2D.Float(-200
,
                    (w 
- 1+ 20, h - 12020
);
            Shape clip 
=
 g2d.getClip();
            g2d.clip(r2d);
            g2d.fillRect(
00
, w, h);
            g2d.setClip(clip);
            g2d.setPaint(p1);
            g2d.drawRoundRect(
-200, (w - 1+ 20, h - 12020
);
            g2d.setPaint(p2);
            g2d.drawRoundRect(
-191, (w - 3+ 20, h - 31818
);
            g2d.setPaint(p1);
            g2d.drawLine(
010
, h);
            g2d.setPaint(p2);
            g2d.drawLine(
121, h - 1
);
            
break
;
        }
        
case
 BALL: {
            Arc2D.Float a2d 
= new Arc2D.Float(00, w, h, 0360
, Arc2D.CHORD);
            Shape clip 
=
 g2d.getClip();
            g2d.clip(a2d);
            g2d.fillRect(
00
, w, h);
            g2d.setClip(clip);
            g2d.setPaint(p1);
            g2d.drawOval(
00, w - 1, h - 1
);
            g2d.setPaint(p2);
            g2d.drawOval(
11, w - 3, h - 3
);
            
break
;
        }
        
case
 STAR: {
            
int x = w / 2
;
            
int y = h / 2
;
            
int r = w / 2
;
            
// 计算五个顶点

            Point[] ps = new Point[5 ];
            
for (int i = 0; i <= 4; i++
) {
                ps[i] 
= new Point((int) (x -
 r
                        
* Math.sin((i * 72 + 36* 2 * Math.PI / 360
)),
                        (
int) (y +
 r
                                
* Math.cos((i * 72 + 36* 2 * Math.PI / 360
)));
            }
            GeneralPath star 
= new
 GeneralPath();
            star.moveTo(ps[
3].x, ps[3
].y);
            star.lineTo(ps[
0].x, ps[0
].y);
            star.lineTo(ps[
2].x, ps[2
].y);
            star.lineTo(ps[
4].x, ps[4
].y);
            star.lineTo(ps[
1].x, ps[1
].y);
            star.lineTo(ps[
3].x, ps[3
].y);
            star.closePath();
            Shape clip 
=
 g2d.getClip();
            g2d.clip(star);
            g2d.fillRect(
00
, w, h);
            g2d.setClip(clip);
            g2d.setPaint(p1);
            g2d.draw(star);
            g2d.setPaint(p2);
            g2d.draw(star);
            
break
;
        }
        
default
:
            
break
;
        }
        g2d.dispose();
        
super
.paintComponent(g);
    }
}

你可能感兴趣的:(java swing定制jbutton 圆角显示)