KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > swing > CustomToggleButton


1 package org.antlr.works.swing;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.font.TextLayout JavaDoc;
6 import java.awt.geom.Rectangle2D JavaDoc;
7 /*
8
9 [The "BSD licence"]
10 Copyright (c) 2005-2006 Jean Bovet
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
15 are met:
16
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22 3. The name of the author may not be used to endorse or promote products
23 derived from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 */

37
38 public class CustomToggleButton extends JToggleButton {
39
40     public int tag;
41     public final int round = 4;
42     public final int height = 22;
43     public final float fontSize = 12f;
44
45     public CustomToggleButton(String JavaDoc title) {
46         super(title);
47         setBorderPainted(false);
48         setMaximumSize(new Dimension(0, height));
49         setFont(getFont().deriveFont(fontSize));
50     }
51     
52     public void setTag(int tag) {
53         this.tag = tag;
54     }
55
56     public int getTag() {
57         return tag;
58     }
59
60     public void paintComponent(Graphics g) {
61         g.setColor(getBackground());
62         g.fillRect(0, 0, getWidth(), getHeight());
63         paintButton((Graphics2D)g, 0, 0, getWidth()-2, getHeight()-1, isSelected());
64     }
65
66     public void paintButton(Graphics2D g2d, int x, int y, int width, int height, boolean selected) {
67         Color topColor;
68         Color middleUpColor;
69         Color middleDownColor;
70         Color bottomColor;
71         final Color snowColor = new Color(0.95f, 0.95f, 0.95f);
72
73         if(selected) {
74             topColor = new Color(0.7f, 0.9f, 1.0f);
75             middleUpColor = new Color(0.5f, 0.7f, 1.0f);
76             middleDownColor = new Color(0.1f, 0.6f, 0.9f);
77             bottomColor = new Color(0.8f, 0.9f, 1.0f);
78         } else {
79             topColor = new Color(0.99f, 0.99f, 0.99f);
80             middleUpColor = new Color(0.9f, 0.9f, 0.9f);
81             middleDownColor = new Color(0.85f, 0.85f, 0.85f);
82             bottomColor = new Color(0.99f, 0.99f, 0.99f);
83         }
84
85         GradientPaint gradient = new GradientPaint(x, y, topColor,
86                 x, y+height/2, middleUpColor);
87         g2d.setPaint(gradient);
88         g2d.fillRect(x, y, width, height/2);
89
90         g2d.setColor(snowColor);
91         g2d.drawLine(x, y+1, x+width, y+1);
92
93         gradient = new GradientPaint(x, y+height/2, middleDownColor,
94                 x, y+height, bottomColor);
95         g2d.setPaint(gradient);
96         g2d.fillRect(x, y+height/2, width, height/2);
97
98         if(selected)
99             g2d.setColor(Color.darkGray);
100         else
101             g2d.setColor(Color.gray);
102         g2d.drawRoundRect(x, y, width, height, round, round);
103
104         // Label
105
TextLayout JavaDoc layout = new TextLayout JavaDoc(getText(), g2d.getFont(), g2d.getFontRenderContext());
106         Rectangle2D JavaDoc r = layout.getBounds();
107
108         float tx = (float) (width*0.5f-r.getWidth()*0.5f);
109         float ty = (float) (height*0.5f+r.getHeight()*0.5f);
110         g2d.setColor(Color.black);
111         layout.draw(g2d, tx, ty);
112     }
113
114 }
115
Popular Tags