1 package org.jacorb.poa.gui.beans; 2 3 22 23 28 public class FillLevelCanvas extends java.awt.Canvas { 29 private java.awt.Graphics buf = null; 30 private java.awt.Image img = null; 31 private java.awt.Color color1 = java.awt.Color.orange; 32 private java.awt.Color 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 45 46 public FillLevelCanvas() { 47 super(); 48 initialize(); 49 } 50 public int getYAvg() { 51 if (useAvg) return yAvg; 52 return 0; 53 } 54 58 private void handleException(Throwable exception) { 59 60 61 } 64 public void init(int _min, int _avg, int _max, java.awt.Color _color1, java.awt.Color _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 76 77 private void initialize() { 78 setName("FillLevelCanvas"); 81 setBackground(java.awt.Color.white); 82 setSize(15, 100); 83 width = getBounds().width; 85 height = getBounds().height; 86 } 88 92 public static void main(java.lang.String [] args) { 93 try { 94 java.awt.Frame frame; 95 try { 96 Class aFrameClass = Class.forName("com.ibm.uvm.abt.edit.TestFrame"); 97 frame = (java.awt.Frame )aFrameClass.newInstance(); 98 } catch (java.lang.Throwable ivjExc) { 99 frame = new java.awt.Frame (); 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 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 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 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 |