001// LEDDisplay Bean Class 002// LEDDisplay.java 003/* 004 * LEDDisplay.java 005 * Copyright (c) 2002 Tom Gutwin 006 * 007 * This program is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU General Public License 009 * as published by the Free Software Foundation; either version 2 010 * of the License, or any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with this program; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 020 */ 021 022 023package ca.bc.webarts.widgets.led; 024 025// Imports 026import java.awt.*; 027import java.awt.event.WindowAdapter; 028import java.awt.event.WindowEvent; 029import java.beans.*; 030import java.io.Serializable; 031import java.net.URL; 032import javax.swing.ImageIcon; 033 034/** 035 * A Simple LED Looking Display of numbers that are specified and 036 * controlled by the user. 037 * 038 * @created May 26, 2002 039 */ 040public class LEDDisplay extends Canvas implements Serializable 041{ 042 private final static int DIGIT_WIDTH = 7; 043 private final static int DIGIT_HEIGHT = 16; 044 private transient Image[] digits; 045 private boolean raised = false; 046 private int numDigits = 5; 047 private int value = 0; 048 private static Frame frame = null; 049 public boolean autoCount = true; 050 051 // Constructors 052 /** Constructor for the LEDDisplay object */ 053 public LEDDisplay() 054 { 055 this(false, 5, 0); 056 } 057 058 059 /** 060 * Constructor for the LEDDisplay object 061 * 062 * @param r raise the numbers 063 * @param n set the number of digits 064 * @param v the number(value) to display 065 */ 066 public LEDDisplay(boolean r, int n, int v) 067 { 068 // Allow the superclass constructor to do its thing 069 super(); 070 071 // Load the digit images 072 digits = new Image[10]; 073 for (int i = 0; i < 10; i++) 074 { 075 String name = "/ca/bc/webarts/widgets/led/LED" + i + ".gif"; 076 digits[i] = loadImage(name); 077 if (digits[i] == null) 078 { 079 System.err.println("Couldn't load image " + name + "."); 080 } 081 } 082 083 // Set properties 084 setNumDigits(n); 085 raised = r; 086 value = v; 087 setBackground(Color.black); 088 } 089 090 091 /** main to run/test as an app */ 092 static public void main(String[] args) 093 { 094 System.out.println("Testing the LEDDisplay "); 095 final LEDDisplay ledCanvas = new LEDDisplay(); 096 097 ledCanvas.autoCount = true; 098 if (args.length > 0) 099 { 100 ledCanvas.setValue(Integer.parseInt(args[0])); 101 //ledCanvas.autoCount = false; 102 } 103 if (args.length > 1) 104 { 105 ledCanvas.setRaised((args[1].toLowerCase().equals("true")?true:false)); 106 } 107 108 frame = new Frame("LEDDisplay Test Frame"); 109 110 frame.setLayout (new BorderLayout()); 111 frame.add("Center", ledCanvas); 112 frame.pack(); 113 frame.show(); 114 frame.toFront(); 115 frame.setVisible(true); 116 //this.repaint(); 117 118 /* listen for closing */ 119 frame.addWindowListener(new WindowAdapter() 120 { 121 public void windowClosing(WindowEvent event) 122 { 123 ledCanvas.autoCount = false; 124 frame.dispose(); 125 System.exit(0); 126 } 127 }); /* inner WindowAdapter class */ 128 129 } 130 131 132 // Accessor methods 133 /** 134 * Gets the raised attribute of the LEDDisplay object 135 * 136 * @return The raised value 137 */ 138 public boolean isRaised() 139 { 140 return raised; 141 } 142 143 144 /** 145 * Sets the raised attribute of the LEDDisplay object 146 * 147 * @param r The new raised value 148 */ 149 public void setRaised(boolean r) 150 { 151 raised = r; 152 repaint(); 153 } 154 155 156 /** 157 * Gets the numDigits attribute of the LEDDisplay object 158 * 159 * @return The numDigits value 160 */ 161 public int getNumDigits() 162 { 163 return numDigits; 164 } 165 166 167 /** 168 * Sets the numDigits attribute of the LEDDisplay object 169 * 170 * @param n The new numDigits value 171 */ 172 public void setNumDigits(int n) 173 { 174 numDigits = Math.max(0, Math.min(8, n)); 175 // maximum of 8 digits 176 sizeToFit(); 177 } 178 179 180 /** 181 * Gets the value attribute of the LEDDisplay object 182 * 183 * @return The value value 184 */ 185 public int getValue() 186 { 187 return value; 188 } 189 190 191 /** 192 * Sets the value attribute of the LEDDisplay object 193 * 194 * @param v The new value value 195 */ 196 public void setValue(int v) 197 { 198 // Constrain to a maximum value of (10 ^ numDigits) - 1 199 if (v <= (int) Math.pow(10.0, (double) numDigits) - 1) 200 { 201 value = Math.max(0, v); 202 } 203 repaint(); 204 } 205 206 207 // Other public methods 208 /** 209 * Description of the Method 210 * 211 * @param g Description of the Parameter 212 */ 213 public synchronized void paint(Graphics g) 214 { 215 int width = size().width; 216 int height = size().height; 217 218 // Paint the background with 3D effects 219 g.setColor(getBackground()); 220 g.fillRect(1, 1, width - 2, height - 2); 221 g.setColor(Color.lightGray); 222 g.draw3DRect(0, 0, width - 1, height - 1, raised); 223 224 // Paint the LED digits exp 225 for (int i = 0; i < numDigits; i++) 226 { 227 int div = (int) Math.pow(10.0, (double) (numDigits - i - 1)); 228 g.drawImage(digits[(value / div) % 10], 229 3 + i * (2 + DIGIT_WIDTH), 3, this); 230 } 231 } 232 233 234 /** 235 * Gets the preferredSize attribute of the LEDDisplay object 236 * 237 * @return The preferredSize value 238 */ 239 public Dimension getPreferredSize() 240 { 241 // Calculate the preferred size based on the label text 242 return new Dimension(((2 + DIGIT_WIDTH) * numDigits) + 4, 243 DIGIT_HEIGHT + 6); 244 } 245 246 247 // Private support methods 248 /** Description of the Method */ 249 private void sizeToFit() 250 { 251 // Resize to the preferred size 252 Dimension d = getPreferredSize(); 253 resize(d.width, d.height); 254 Component p = getParent(); 255 if (p != null) 256 { 257 p.invalidate(); 258 p.layout(); 259 } 260 } 261 262 263 /** 264 * Description of the Method 265 * 266 * @param name Description of the Parameter 267 * @return Description of the Return Value 268 */ 269 public Image loadImage(String name) 270 { 271 try 272 { 273 URL url = getClass().getResource(name); 274 return (new ImageIcon(url)).getImage(); 275 } 276 catch (Exception e) 277 { 278 e.printStackTrace(); 279 return null; 280 } 281 } 282} 283