KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > graph > MeterCanvas


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.swing.graph;
18
19 import java.awt.AlphaComposite JavaDoc;
20 import java.awt.Color JavaDoc;
21 import java.awt.Composite JavaDoc;
22 import java.awt.Font JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.awt.Graphics2D JavaDoc;
25 import java.awt.GridBagConstraints JavaDoc;
26 import java.awt.GridBagLayout JavaDoc;
27 import java.awt.Rectangle JavaDoc;
28 import java.awt.event.WindowAdapter JavaDoc;
29 import java.awt.event.WindowEvent JavaDoc;
30 import java.awt.font.TextLayout JavaDoc;
31 import java.awt.geom.Rectangle2D JavaDoc;
32
33 import javax.swing.JFrame JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36
37 /**
38  *
39  * @author Greg Hinkle (ghinkle@users.sourceforge.net), January 2002
40  * @version $Revision: 480 $($Author: ghinkl $ / $Date: 2004-10-05 01:17:41 -0400 (Tue, 05 Oct 2004) $)
41  */

42 public class MeterCanvas extends JPanel JavaDoc {
43     
44     /** Creates a new instance of MeterCanvas */
45     public MeterCanvas() {
46         this.setOpaque(false);
47     }
48     
49     private int min = 0;
50     private int max;
51     private int current;
52     
53     private String JavaDoc label = "label";
54     
55     private static final int BUBBLE_HEIGHT = 5;
56     
57     private static final Color JavaDoc DARK = Color.decode("0x336699");
58     private static final Color JavaDoc LIGHT = Color.decode("0x99CCFF");
59     
60     private static final Font JavaDoc KEY_FONT = Font.decode("Arial-BOLD-24");
61     private static final Font JavaDoc LABEL_FONT = Font.decode("Arial-BOLD-12");
62     
63     public void paintComponent(Graphics JavaDoc g) {
64         super.paintComponent(g);
65         Graphics2D JavaDoc gg = (Graphics2D JavaDoc) g;
66         
67         Rectangle JavaDoc rect = this.getBounds();
68         
69         //System.out.println("Bounds are: " + rect);
70
gg.clearRect(0,0, getWidth(), getHeight());
71         //gg.clearRect((int)rect.getX(),(int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight());
72

73         int h = 0;
74         
75         double topLine = ((double)getHeight() / (this.max - this.min)) * this.current;
76         
77         //System.out.println("\t" + topLine);
78
while ( h < getHeight()) {
79             if (h < topLine) {
80                 gg.setColor(DARK);
81                 gg.fillRoundRect(10, getHeight() - h, getWidth() - 20, BUBBLE_HEIGHT, 2, 2);
82                 //gg.fillRoundRect((int)rect.getX() + 10, (int)rect.getHeight() - h, (int) rect.getWidth() - 20, BUBBLE_HEIGHT, 2, 2);
83

84             } else {
85
86                 gg.setColor(LIGHT);
87                 gg.drawRoundRect( 10, getHeight() - h, getWidth() - 20, BUBBLE_HEIGHT, 2, 2);
88                 //gg.drawRoundRect((int)rect.getX() + 10, (int)rect.getHeight() - h, (int) rect.getWidth() - 20, BUBBLE_HEIGHT, 2, 2);
89

90             }
91             h += BUBBLE_HEIGHT + 2;
92         }
93         Composite JavaDoc basicComposite = gg.getComposite();
94         gg.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5F));
95         gg.setColor(Color.black);
96         
97         gg.setFont(KEY_FONT);
98         String JavaDoc text = String.valueOf(this.current);
99         TextLayout JavaDoc layout = new TextLayout JavaDoc(text, KEY_FONT, gg.getFontRenderContext());
100         Rectangle2D JavaDoc textBounds = layout.getBounds();
101         gg.drawString(text, (int) ((getWidth()/2) - (textBounds.getWidth()/2)), getHeight()/2);
102         
103         gg.setFont(LABEL_FONT);
104         text = this.label;
105         layout = new TextLayout JavaDoc(text, LABEL_FONT, gg.getFontRenderContext());
106         textBounds = layout.getBounds();
107
108         gg.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .3F));
109         gg.setColor(Color.white);
110         gg.fillRoundRect(15,(int) (getHeight() - textBounds.getHeight() - 25),(int) getWidth() - 30,(int) textBounds.getHeight() + 10, 5, 5);
111
112         gg.setComposite(basicComposite);
113         
114         gg.setColor(Color.black);
115         gg.drawString(text, (int) ((getWidth()/2) - (textBounds.getWidth()/2)), (int) (getHeight() - 20));
116
117     }
118     
119     /** Getter for property min.
120      * @return Value of property min.
121      *
122      */

123     public int getMin() {
124         return min;
125     }
126     
127     /** Setter for property min.
128      * @param min New value of property min.
129      *
130      */

131     public void setMin(int min) {
132         this.min = min;
133     }
134     
135     /** Getter for property max.
136      * @return Value of property max.
137      *
138      */

139     public int getMax() {
140         return max;
141     }
142     
143     /** Setter for property max.
144      * @param max New value of property max.
145      *
146      */

147     public void setMax(int max) {
148         this.max = max;
149     }
150     
151     /** Getter for property current.
152      * @return Value of property current.
153      *
154      */

155     public int getCurrent() {
156         return current;
157     }
158     
159     /** Setter for property current.
160      * @param current New value of property current.
161      *
162      */

163     public void setCurrent(int current) {
164         this.current = current;
165         this.repaint();
166         //System.out.println(current);
167
}
168     
169
170     
171     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
172         
173         final MeterCanvas mc = new MeterCanvas();
174         mc.setMax(200);
175         mc.setMin(0);
176         mc.setLabel("Free Memory");
177         mc.setMinimumSize(null); //new Dimension(40, 120));
178
mc.setPreferredSize(null); //new Dimension(80, 200));
179

180         JFrame JavaDoc frame = new JFrame JavaDoc("Meter test");
181         frame.addWindowListener(new WindowAdapter JavaDoc() {
182             public void windowClosed(WindowEvent JavaDoc we) {
183                 mc.setLabel(null);
184             }
185         });
186         
187         
188         frame.setSize(150,350);
189         frame.getContentPane().setLayout(new GridBagLayout JavaDoc());
190         GridBagConstraints JavaDoc gc = new GridBagConstraints JavaDoc();
191         gc.gridx = 2;
192         gc.gridy = 1;
193         gc.fill = gc.BOTH;
194         gc.weightx = 0.5;
195         gc.weighty = 0.5;
196         frame.getContentPane().add(mc,gc);
197
198         JLabel JavaDoc testLabel = new JLabel JavaDoc("Hello, World!");
199         GridBagConstraints JavaDoc tc = new GridBagConstraints JavaDoc();
200         tc.gridx = 1;
201         tc.gridy = 1;
202         tc.fill = gc.BOTH;
203         //tc.weightx = 0.5;
204
//tc.weighty = 0.5;
205
frame.getContentPane().add(testLabel, tc);
206         
207         
208         
209         GridBagConstraints JavaDoc sc = new GridBagConstraints JavaDoc();
210         sc.gridx = 3;
211         sc.gridy = 1;
212         sc.fill = sc.BOTH;
213         sc.weightx = 0.5;
214         sc.weighty = 0.5;
215         frame.getContentPane().add(new JLabel JavaDoc("This is a longer test. testing."),sc);
216         
217         
218         frame.getContentPane().doLayout();
219         frame.setVisible(true);
220         int i = 0;
221         while (mc.getLabel() != null) {
222             i++;
223             
224             mc.setCurrent((int) (Math.sin(i*.06) * 100.0) + 100);
225
226             Thread.currentThread().sleep(50);
227         }
228     }
229     
230     /** Getter for property label.
231      * @return Value of property label.
232      *
233      */

234     public java.lang.String JavaDoc getLabel() {
235         return label;
236     }
237     
238     /** Setter for property label.
239      * @param label New value of property label.
240      *
241      */

242     public void setLabel(java.lang.String JavaDoc label) {
243         this.label = label;
244     }
245     
246 }
247
Popular Tags