001: package org.jacorb.poa.gui.beans;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: /**
024: * @author Reimo Tiedemann, FU Berlin
025: * @version 1.0, 05/03/99
026: * @see org.jacorb.poa.gui.beans.FillLevelBar
027: */
028: public class FillLevelCanvas extends java.awt.Canvas {
029: private java.awt.Graphics buf = null;
030: private java.awt.Image img = null;
031: private java.awt.Color color1 = java.awt.Color.orange;
032: private java.awt.Color color2 = java.awt.Color.red;
033: private int width = 0;
034: private int height = 0;
035: private int max = 0;
036: private int avg = 0;
037: private int min = 0;
038: private int cur = 0;
039: private int yAvg = 0;
040: private int yCur = 0;
041: private boolean useAvg;
042:
043: /**
044: * Constructor
045: */
046: /* WARNING: THIS METHOD WILL BE REGENERATED. */
047: public FillLevelCanvas() {
048: super ();
049: initialize();
050: }
051:
052: public int getYAvg() {
053: if (useAvg)
054: return yAvg;
055: return 0;
056: }
057:
058: /**
059: * Called whenever the part throws an exception.
060: * @param exception java.lang.Throwable
061: */
062: private void handleException(Throwable exception) {
063:
064: /* Uncomment the following lines to print uncaught exceptions to stdout */
065: // System.out.println("--------- UNCAUGHT EXCEPTION ---------");
066: // exception.printStackTrace(System.out);
067: }
068:
069: public void init(int _min, int _avg, int _max,
070: java.awt.Color _color1, java.awt.Color _color2,
071: boolean _useAvg) {
072: min = _min;
073: avg = _avg;
074: max = _max;
075: useAvg = _useAvg;
076: yCompute();
077: if (_color1 != null)
078: color1 = _color1;
079: if (_color2 != null)
080: color2 = _color2;
081: }
082:
083: /**
084: * Initialize the class.
085: */
086: /* WARNING: THIS METHOD WILL BE REGENERATED. */
087: private void initialize() {
088: // user code begin {1}
089: // user code end
090: setName("FillLevelCanvas");
091: setBackground(java.awt.Color.white);
092: setSize(15, 100);
093: // user code begin {2}
094: width = getBounds().width;
095: height = getBounds().height;
096: // user code end
097: }
098:
099: /**
100: * main entrypoint - starts the part when it is run as an application
101: * @param args java.lang.String[]
102: */
103: public static void main(java.lang.String[] args) {
104: try {
105: java.awt.Frame frame;
106: try {
107: Class aFrameClass = Class
108: .forName("com.ibm.uvm.abt.edit.TestFrame");
109: frame = (java.awt.Frame) aFrameClass.newInstance();
110: } catch (java.lang.Throwable ivjExc) {
111: frame = new java.awt.Frame();
112: }
113: FillLevelCanvas aFillLevelCanvas;
114: aFillLevelCanvas = new FillLevelCanvas();
115: frame.add("Center", aFillLevelCanvas);
116: frame.setSize(aFillLevelCanvas.getSize());
117: frame.setVisible(true);
118: } catch (Throwable exception) {
119: System.err
120: .println("Exception occurred in main() of org.jacorb.poa.gui.FillLevelCanvas");
121: exception.printStackTrace(System.out);
122: }
123: }
124:
125: public void paint(java.awt.Graphics g) {
126: if (buf == null) {
127: img = createImage(getBounds().width, getBounds().height);
128: buf = img.getGraphics();
129: } else {
130: buf.setColor(getBackground());
131: buf.fillRect(0, 0, width, height);
132: buf.setColor(getForeground());
133: }
134: paintUnbuffered(buf);
135: g.drawImage(img, 0, 0, this );
136: }
137:
138: public void paintUnbuffered(java.awt.Graphics g) {
139: if (useAvg) {
140: if (cur <= avg) {
141: g.setColor(color1);
142: g.fillRect(0, yCur, width, height - yCur);
143:
144: } else {
145: g.setColor(color2);
146: g.fillRect(0, yCur, width, height - yCur);
147: g.setColor(color1);
148: g.fillRect(0, yAvg, width, height - yAvg);
149: }
150: if (avg < max) {
151: g.setColor(java.awt.Color.black);
152: g.drawLine(0, yAvg, width, yAvg);
153: }
154:
155: } else {
156: g.setColor(color1);
157: g.fillRect(0, yCur, width, height - yCur);
158: }
159: }
160:
161: public void setAvg(int value) {
162: avg = value;
163: yCompute();
164: repaint();
165: }
166:
167: public void setCurrent(int value) {
168: cur = value;
169: yCompute();
170: repaint();
171: }
172:
173: public void setMax(int value) {
174: max = value;
175: yCompute();
176: repaint();
177: }
178:
179: public void setMin(int value) {
180: min = value;
181: yCompute();
182: repaint();
183: }
184:
185: private void yCompute() {
186: float helpF;
187: if (useAvg) {
188: helpF = ((float) avg) / ((float) max - min)
189: * ((float) height);
190: yAvg = yTransform((int) helpF);
191: }
192: helpF = ((float) cur) / ((float) max - min) * ((float) height);
193: yCur = yTransform((int) helpF);
194: }
195:
196: private int yTransform(int y) {
197: return height - y;
198: }
199:
200: }
|