1 /* -------------------------------------------------------------------
2 * Java source file for the class ScrollLabel
3 *
4 * Copyright (c), 2002, Masahiro Takatsuka.
5 * All Rights Researved.
6 *
7 * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8 * $Author: takatsukam $
9 *
10 * $Date: 2003/07/25 04:51:47 $
11 *
12 * $Id: ScrollLabel.java,v 1.1.1.1 2003/07/25 04:51:47 takatsukam Exp $
13 *
14 * Reference: Document no:
15 * ___ ___
16 *
17 * To Do:
18 * ___
19 *
20 ------------------------------------------------------------------- */
21
22 /* --------------------------- Package ---------------------------- */
23 package net.jbeans.ui.label;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */
26 import java.awt.*;
27 import java.io.*;
28 import java.net.*;
29 import java.util.*;
30
31 import javax.swing.*;
32
33 import net.jbeans.util.debug.*;
34
35 /*====================================================================
36 Implementation of class ScrollLabel
37 ====================================================================*/
38 /***
39 * ScrollLabel is a scrolling label.
40 *
41 * @version $Revision: 1.1.1.1 $
42 * @author Masahiro Takatsuka (masa@jbeans.net)
43 * @see JPanel
44 * @see Runnable, Serializable
45 */
46
47 public class ScrollLabel extends JPanel implements Runnable, Serializable {
48 /***
49 * debug flag
50 */
51 private static final boolean DEBUG = Debug.getDebugFlag(ScrollLabel.class);
52
53 public static final String DOWN = "down";
54 public static final String UP = "up";
55 public static final String LEFT = "left";
56 public static final String RIGHT = "right";
57
58 private static final String DEFAULT_DIR = LEFT;
59 private static final int DEFAULT_DELAY = 200;
60 private static final int DEFAULT_XSPEED = 2;
61 private static final int DEFAULT_YSPEED = 2;
62 private static final Color DEFAUTL_TEXT_COLOR = Color.black;
63 private static final Color DEFAUTL_BG_COLOR = Color.white;
64
65 private int delay;
66 private int xspeed;
67 private int yspeed;
68 private int xsize;
69 private int ysize;
70 private int xOffset;
71 private int yOffset;
72 private int index;
73 private OrientedLabel[] scrollLabels; // String
74 private String[] moveTypes; // Integer
75 private Color[] bgColors;
76
77 transient private int scrollWidth;
78 transient private int scrollHeight;
79 transient private int currentIndex;
80 transient private Thread runner;
81 transient private boolean running = false;
82 transient private boolean textChanged = true;
83
84 public ScrollLabel() {
85 this.delay = DEFAULT_DELAY;
86 this.xspeed = DEFAULT_XSPEED;
87 this.yspeed = DEFAULT_YSPEED;
88 this.xsize = -1;
89 this.ysize = -1;
90 initialize();
91 }
92
93 private void initialize() {
94 setLayout(null);
95 }
96
97 /***
98 * Serialization methods
99 */
100 private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
101 s.defaultReadObject();
102
103 initialize();
104 }
105
106 public void setDelay(int delay) {
107 this.delay = (delay > 0) ? delay : DEFAULT_DELAY;
108 }
109
110 public int getDelay() {
111 return this.delay;
112 }
113
114 public void setXSpeed(int speed) {
115 this.xspeed = (speed > 0) ? speed : DEFAULT_XSPEED;
116 }
117
118 public int getXSpeed() {
119 return this.xspeed;
120 }
121
122 public void setYSpeed(int speed) {
123 this.yspeed = (yspeed > 0) ? yspeed : DEFAULT_YSPEED;
124 }
125
126 public int getYspeed() {
127 return this.yspeed;
128 }
129
130 public void setNumberOfLabels(int num) {
131 this.index = num;
132 this.scrollLabels = new OrientedLabel[this.index];
133 this.moveTypes = new String[this.index];
134 for (int i = 0; i < this.index; i++) {
135 this.scrollLabels[i] = new OrientedLabel();
136 this.moveTypes[i] = DEFAULT_DIR;
137 }
138 }
139
140 public int getNumberOfLabels() {
141 return this.index;
142 }
143
144 public void setScrollTexts(String[] texts) {
145 int num = Math.min(this.index, texts.length);
146
147 for (int i = 0; i < num; i++) {
148 this.scrollLabels[i].setText(texts[i]);
149 }
150 }
151
152 public String[] getScrollTexts() {
153 String[] texts = new String[this.index];
154 for (int i = 0; i < this.index; i++) {
155 texts[i] = this.scrollLabels[i].getText();
156 }
157 return texts;
158 }
159
160 public void setScrollImages(Image[] images) {
161 int num = Math.min(this.index, images.length);
162
163 for (int i = 0; i < num; i++) {
164 if (images[i] != null) {
165 this.scrollLabels[i].setIcon(new ImageIcon(images[i]));
166 }
167 }
168 }
169
170 public Image[] getScrollImages() {
171 Image[] images = new Image[this.index];
172 for (int i = 0; i < this.index; i++) {
173 images[i] = ((ImageIcon) this.scrollLabels[i].getIcon()).getImage();
174 }
175 return images;
176 }
177
178 public void setTextColors(Color[] textColors) {
179 int num = Math.min(this.index, textColors.length);
180
181 for (int i = 0; i < num; i++) {
182 this.scrollLabels[i].setForeground(textColors[i]);
183 }
184 }
185
186 public Color[] getTextColors() {
187 Color[] colors = new Color[this.index];
188 for (int i = 0; i < this.index; i++) {
189 colors[i] = this.scrollLabels[i].getForeground();
190 }
191 return colors;
192 }
193
194 public void setMoveTypes(String[] moveTypes) {
195 int num = Math.min(this.index, moveTypes.length);
196
197 for (int i = 0; i < num; i++) {
198 this.moveTypes[i] = moveTypes[i];
199 }
200 }
201
202 public String[] getMoveTypes() {
203 return this.moveTypes;
204 }
205
206 private void setFonts(Font[] fonts) {
207 int num = Math.min(this.index, fonts.length);
208
209 for (int i = 0; i < num; i++) {
210 this.scrollLabels[i].setFont(fonts[i]);
211 }
212 }
213
214 private Font[] getFonts() {
215 Font[] fonts = new Font[this.index];
216 for (int i = 0; i < this.index; i++) {
217 fonts[i] = this.scrollLabels[i].getFont();
218 }
219 return fonts;
220 }
221
222 public void setFontNames(String[] names) {
223 int num = Math.min(this.index, names.length);
224
225 for (int i = 0; i < num; i++) {
226 Font oldfont = this.scrollLabels[i].getFont();
227 Font font = new Font(names[i],
228 oldfont.getStyle(), oldfont.getSize());
229 this.scrollLabels[i].setFont(font);
230 }
231 }
232
233 public String[] getFontNames() {
234 String[] names = new String[this.index];
235 for (int i = 0; i < this.index; i++) {
236 Font font = this.scrollLabels[i].getFont();
237 names[i] = font.getFontName();
238 }
239 return names;
240 }
241
242 public void setFontStyles(int[] styles) {
243 int num = Math.min(this.index, styles.length);
244
245 for (int i = 0; i < num; i++) {
246 Font oldfont = this.scrollLabels[i].getFont();
247 Font font = new Font(oldfont.getFontName(),
248 styles[i], oldfont.getSize());
249 this.scrollLabels[i].setFont(font);
250 }
251 }
252
253 public int[] getFontStyles() {
254 int[] styles = new int[this.index];
255 for (int i = 0; i < this.index; i++) {
256 Font font = this.scrollLabels[i].getFont();
257 styles[i] = font.getStyle();
258 }
259 return styles;
260 }
261
262 public void setFontSizes(int[] sizes) {
263 int num = Math.min(this.index, sizes.length);
264
265 for (int i = 0; i < num; i++) {
266 Font oldfont = this.scrollLabels[i].getFont();
267 Font font = new Font(oldfont.getFontName(),
268 oldfont.getStyle(), sizes[i]);
269 this.scrollLabels[i].setFont(font);
270 }
271 }
272
273 public int[] getFontSizes() {
274 int[] sizes = new int[this.index];
275 for (int i = 0; i < this.index; i++) {
276 Font font = this.scrollLabels[i].getFont();
277 sizes[i] = font.getSize();
278 }
279 return sizes;
280 }
281
282 public void setBackgroundColors(Color[] bgColors) {
283 int num = Math.min(this.index, bgColors.length);
284
285 for (int i = 0; i < num; i++) {
286 this.scrollLabels[i].setBackground(bgColors[i]);
287 }
288 }
289
290 public Color[] getBackgroundColors() {
291 Color[] colors = new Color[this.index];
292 for (int i = 0; i < this.index; i++) {
293 colors[i] = this.scrollLabels[i].getBackground();
294 }
295 return colors;
296 }
297
298 public void setSize(int width, int height) {
299 if (width != 0 && height != 0) {
300 this.xsize = width;
301 this.ysize = height;
302 }
303 super.setSize(width, height);
304 }
305
306 private void setOffset(int index) {
307 Dimension dim = this.scrollLabels[index].getPreferredSize();
308 this.scrollWidth = dim.width;
309 this.scrollHeight = dim.height;
310 if (this.moveTypes[index].equalsIgnoreCase(DOWN)) {
311 this.xOffset = (this.xsize - this.scrollWidth) / 2;
312 this.yOffset = 0;
313 return;
314 }
315
316 if(this.moveTypes[index].equalsIgnoreCase(UP)) {
317 this.xOffset = (this.xsize - this.scrollWidth) / 2;
318 this.yOffset = this.ysize;
319 return;
320 }
321
322 if(this.moveTypes[index].equalsIgnoreCase(LEFT)) {
323 this.xOffset = this.xsize;
324 this.yOffset = (this.ysize - this.scrollLabels[index].getPreferredSize().height) / 2;
325 return;
326 }
327
328 if(this.moveTypes[index].equalsIgnoreCase(RIGHT)) {
329 this.xOffset = - this.scrollWidth;
330 this.yOffset = (this.ysize - this.scrollLabels[index].getPreferredSize().height) / 2;
331 }
332 }
333
334 public void start() {
335 if (this.runner == null) {
336 this.runner = new Thread(this);
337 this.runner.start();
338 }
339 this.running = true;
340 }
341
342 public void stop() {
343 this.running = false;
344 this.runner = null;
345 }
346
347 public void run() {
348 long l = System.currentTimeMillis();
349 setOffset(this.currentIndex);
350 while (true) {
351 Thread.currentThread().yield();
352 if (!this.running) {
353 break;
354 }
355 try {
356 l += this.delay;
357 this.runner.sleep(Math.max(0L, l - System.currentTimeMillis()));
358 } catch(InterruptedException _ex) {
359 }
360 updateLocation();
361 repaint();
362 }
363 }
364
365 private void updateLocation() {
366 Component comp = this.scrollLabels[this.currentIndex];
367 comp.setLocation(this.xOffset, this.yOffset);
368 if (this.textChanged) {
369 if (getComponentCount() > 0) {
370 remove(0);
371 }
372 comp.setSize(comp.getPreferredSize());
373 comp.setVisible(true);
374 add(comp, 0);
375 this.textChanged = false;
376 }
377
378 if (this.moveTypes[this.currentIndex].equalsIgnoreCase(DOWN)) {
379 this.yOffset += this.yspeed;
380 } else if (this.moveTypes[this.currentIndex].equalsIgnoreCase(UP)) {
381 this.yOffset -= this.yspeed;
382 } else if (this.moveTypes[this.currentIndex].equalsIgnoreCase(LEFT)) {
383 this.xOffset -= this.xspeed;
384 } else if (this.moveTypes[this.currentIndex].equalsIgnoreCase(RIGHT)) {
385 this.xOffset += this.xspeed;
386 }
387 if (this.yOffset > this.ysize + this.scrollHeight ||
388 this.yOffset < - this.scrollHeight ||
389 this.xOffset > this.xsize ||
390 this.xOffset < - this.scrollWidth) {
391 comp.setVisible(false);
392 this.currentIndex++;
393 if (this.currentIndex >= this.index) {
394 this.currentIndex = 0;
395 }
396 setOffset(this.currentIndex);
397 this.textChanged = true;
398 }
399 }
400
401 public void paint(Graphics g) {
402 Dimension dim = getSize();
403 if (this.xsize != dim.width || this.ysize != dim.height) {
404 setSize(dim.width, dim.height);
405 setOffset(this.currentIndex);
406 }
407 if (dim.width == 0 || dim.height == 0) {
408 return;
409 }
410
411 g.setColor(this.scrollLabels[this.currentIndex].getBackground());
412 g.fillRect(0, 0, this.xsize, this.ysize);
413
414 super.paint(g);
415 }
416
417 protected void paintComponent(Graphics g) {
418 //NOP
419 }
420
421 public void update(Graphics g) {
422 paint(g);
423 }
424 }
This page was automatically generated by Maven