1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
package CustomizedComponent;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JButton;
import javax.swing.JFrame;
public class CoolButton extends JButton{
private Paint LIGHT_GRADIENT = null;
private Paint OUTER = Color.BLACK;
private Paint INNER = Color.CYAN;
public CoolButton(String string) {
super(string);
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
RenderingHints renderHints =
new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
renderHints.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(renderHints);
if(LIGHT_GRADIENT == null){
LIGHT_GRADIENT =
new GradientPaint(0, 0, Color.BLACK,
0,getHeight()/2, Color.CYAN,
true); // true means to repeat pattern;
}
int w = getWidth(), h = getHeight();
/*Initial Graphics clip is the full bounds of the component:
*create a rounded clip LARGER than the compolor.r*/
RoundRectangle2D.Float r2d =
new RoundRectangle2D.Float(
0, 0, w - 1, h - 1,h, h);
/*intersect this with the existing clip*/
g2d.clip(r2d);
/* fill the clipped area*/
g2d.setPaint(LIGHT_GRADIENT);
g2d.fillRoundRect(0, 0, w - 1, h - 1, h, h);
/* restore original clip
* paint outer border*/
g2d.setPaint(OUTER);
g2d.drawRoundRect(0, 0, w - 2, h - 2, h, h);
/* paint inner border*/
g2d.setPaint(INNER);
g2d.drawRoundRect(1, 1, w -4, h - 4,h-2, h-2);
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.3f));
/**page4**/
/* paint the text and icon*/
super.paintComponent(g);
// create shadow by adding groups of buttons
// to a DropShadowPanel
}
public static void main(String[] args) {
new JFrame(){
{
setLayout(new FlowLayout());
//setLayout(null);
CoolButton b = new CoolButton("hello");
b.setBounds(12,12, 400,200);
add(b);
setSize(300,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
};
}
} |
Partager