KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > poa > gui > beans > FillLevelCanvas


1 package org.jacorb.poa.gui.beans;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22  
23 /**
24  * @author Reimo Tiedemann, FU Berlin
25  * @version 1.0, 05/03/99
26  * @see org.jacorb.poa.gui.beans.FillLevelBar
27  */

28 public class FillLevelCanvas extends java.awt.Canvas JavaDoc {
29     private java.awt.Graphics JavaDoc buf = null;
30     private java.awt.Image JavaDoc img = null;
31     private java.awt.Color JavaDoc color1 = java.awt.Color.orange;
32     private java.awt.Color JavaDoc color2 = java.awt.Color.red;
33     private int width = 0;
34     private int height = 0;
35     private int max = 0;
36     private int avg = 0;
37     private int min = 0;
38     private int cur = 0;
39     private int yAvg = 0;
40     private int yCur = 0;
41     private boolean useAvg;
42 /**
43  * Constructor
44  */

45 /* WARNING: THIS METHOD WILL BE REGENERATED. */
46 public FillLevelCanvas() {
47     super();
48     initialize();
49 }
50 public int getYAvg() {
51     if (useAvg) return yAvg;
52     return 0;
53 }
54 /**
55  * Called whenever the part throws an exception.
56  * @param exception java.lang.Throwable
57  */

58 private void handleException(Throwable JavaDoc exception) {
59
60     /* Uncomment the following lines to print uncaught exceptions to stdout */
61     // System.out.println("--------- UNCAUGHT EXCEPTION ---------");
62
// exception.printStackTrace(System.out);
63
}
64 public void init(int _min, int _avg, int _max, java.awt.Color JavaDoc _color1, java.awt.Color JavaDoc _color2, boolean _useAvg) {
65     min = _min;
66     avg = _avg;
67     max = _max;
68     useAvg = _useAvg;
69     yCompute();
70     if (_color1 != null) color1 = _color1;
71     if (_color2 != null) color2 = _color2;
72 }
73 /**
74  * Initialize the class.
75  */

76 /* WARNING: THIS METHOD WILL BE REGENERATED. */
77 private void initialize() {
78     // user code begin {1}
79
// user code end
80
setName("FillLevelCanvas");
81     setBackground(java.awt.Color.white);
82     setSize(15, 100);
83     // user code begin {2}
84
width = getBounds().width;
85     height = getBounds().height;
86     // user code end
87
}
88 /**
89  * main entrypoint - starts the part when it is run as an application
90  * @param args java.lang.String[]
91  */

92 public static void main(java.lang.String JavaDoc[] args) {
93     try {
94         java.awt.Frame JavaDoc frame;
95         try {
96             Class JavaDoc aFrameClass = Class.forName("com.ibm.uvm.abt.edit.TestFrame");
97             frame = (java.awt.Frame JavaDoc)aFrameClass.newInstance();
98         } catch (java.lang.Throwable JavaDoc ivjExc) {
99             frame = new java.awt.Frame JavaDoc();
100         }
101         FillLevelCanvas aFillLevelCanvas;
102         aFillLevelCanvas = new FillLevelCanvas();
103         frame.add("Center", aFillLevelCanvas);
104         frame.setSize(aFillLevelCanvas.getSize());
105         frame.setVisible(true);
106     } catch (Throwable JavaDoc exception) {
107         System.err.println("Exception occurred in main() of org.jacorb.poa.gui.FillLevelCanvas");
108         exception.printStackTrace(System.out);
109     }
110 }
111 public void paint(java.awt.Graphics JavaDoc g) {
112     if (buf == null) {
113         img = createImage(getBounds().width, getBounds().height);
114         buf = img.getGraphics();
115     } else {
116         buf.setColor(getBackground());
117         buf.fillRect(0, 0, width, height);
118         buf.setColor(getForeground());
119     }
120     paintUnbuffered(buf);
121     g.drawImage(img, 0, 0, this);
122 }
123 public void paintUnbuffered(java.awt.Graphics JavaDoc g) {
124     if (useAvg) {
125         if (cur <= avg) {
126             g.setColor(color1);
127             g.fillRect(0, yCur, width, height - yCur);
128
129         } else {
130             g.setColor(color2);
131             g.fillRect(0, yCur, width, height - yCur);
132             g.setColor(color1);
133             g.fillRect(0, yAvg, width, height - yAvg);
134         }
135         if (avg < max) {
136             g.setColor(java.awt.Color.black);
137             g.drawLine(0, yAvg, width, yAvg);
138         }
139
140     } else {
141         g.setColor(color1);
142         g.fillRect(0, yCur, width, height - yCur);
143     }
144 }
145 public void setAvg(int value) {
146     avg = value;
147     yCompute();
148     repaint();
149 }
150 public void setCurrent(int value) {
151     cur = value;
152     yCompute();
153     repaint();
154 }
155 public void setMax(int value) {
156     max = value;
157     yCompute();
158     repaint();
159 }
160 public void setMin(int value) {
161     min = value;
162     yCompute();
163     repaint();
164 }
165 private void yCompute() {
166     float helpF;
167     if (useAvg) {
168         helpF = ((float)avg)/((float)max-min) * ((float)height);
169         yAvg = yTransform((int) helpF);
170     }
171     helpF = ((float)cur)/((float)max-min) * ((float)height);
172     yCur = yTransform((int) helpF);
173 }
174 private int yTransform(int y) {
175     return height - y;
176 }
177
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
Popular Tags