author | stuefe |
Thu, 10 May 2018 07:26:18 +0200 | |
changeset 50074 | 26ac622a4cab |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
8969 | 2 |
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
|
5 |
* modification, are permitted provided that the following conditions |
|
6 |
* are met: |
|
7 |
* |
|
8 |
* - Redistributions of source code must retain the above copyright |
|
9 |
* notice, this list of conditions and the following disclaimer. |
|
10 |
* |
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
|
12 |
* notice, this list of conditions and the following disclaimer in the |
|
13 |
* documentation and/or other materials provided with the distribution. |
|
14 |
* |
|
5506 | 15 |
* - Neither the name of Oracle nor the names of its |
2 | 16 |
* contributors may be used to endorse or promote products derived |
17 |
* from this software without specific prior written permission. |
|
18 |
* |
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 |
*/ |
|
31 |
||
10292
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8969
diff
changeset
|
32 |
/* |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8969
diff
changeset
|
33 |
* This source code is provided to illustrate the usage of a given feature |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8969
diff
changeset
|
34 |
* or technique and has been deliberately simplified. Additional steps |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8969
diff
changeset
|
35 |
* required for a production-quality application, such as security checks, |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8969
diff
changeset
|
36 |
* input validation and proper error handling, might not be present in |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8969
diff
changeset
|
37 |
* this sample code. |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8969
diff
changeset
|
38 |
*/ |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8969
diff
changeset
|
39 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8969
diff
changeset
|
40 |
|
2 | 41 |
|
42 |
/** |
|
43 |
* I love blinking things. |
|
44 |
* |
|
45 |
* @author Arthur van Hoff |
|
8969 | 46 |
* @author 04/24/96 Jim Hagen use getBackground |
47 |
* @author 02/05/98 Mike McCloskey removed use of deprecated methods |
|
48 |
* @author 04/23/99 Josh Bloch, use timer instead of explicit multithreading. |
|
49 |
* @author 07/10/00 Daniel Peek brought to code conventions, minor changes |
|
2 | 50 |
*/ |
8969 | 51 |
import java.awt.Color; |
52 |
import java.awt.Dimension; |
|
53 |
import java.awt.Font; |
|
54 |
import java.awt.FontMetrics; |
|
55 |
import java.awt.Graphics; |
|
56 |
import java.util.StringTokenizer; |
|
57 |
import java.util.Timer; |
|
58 |
import java.util.TimerTask; |
|
2 | 59 |
|
60 |
||
61 |
public class Blink extends java.applet.Applet { |
|
8969 | 62 |
|
63 |
private static final long serialVersionUID = -775844794477507646L; |
|
2 | 64 |
private Timer timer; // Schedules the blinking |
65 |
private String labelString; // The label for the window |
|
66 |
private int delay; // the delay time between blinks |
|
67 |
||
8969 | 68 |
@Override |
2 | 69 |
public void init() { |
70 |
String blinkFrequency = getParameter("speed"); |
|
8969 | 71 |
delay = (blinkFrequency == null) ? 400 |
72 |
: (1000 / Integer.parseInt(blinkFrequency)); |
|
2 | 73 |
labelString = getParameter("lbl"); |
8969 | 74 |
if (labelString == null) { |
2 | 75 |
labelString = "Blink"; |
8969 | 76 |
} |
2 | 77 |
Font font = new java.awt.Font("Serif", Font.PLAIN, 24); |
78 |
setFont(font); |
|
79 |
} |
|
80 |
||
8969 | 81 |
@Override |
2 | 82 |
public void start() { |
83 |
timer = new Timer(); //creates a new timer to schedule the blinking |
|
84 |
timer.schedule(new TimerTask() { //creates a timertask to schedule |
|
8969 | 85 |
|
2 | 86 |
// overrides the run method to provide functionality |
8969 | 87 |
@Override |
2 | 88 |
public void run() { |
89 |
repaint(); |
|
90 |
} |
|
8969 | 91 |
}, delay, delay); |
2 | 92 |
} |
93 |
||
8969 | 94 |
@Override |
2 | 95 |
public void paint(Graphics g) { |
96 |
int fontSize = g.getFont().getSize(); |
|
97 |
int x = 0, y = fontSize, space; |
|
8969 | 98 |
int red = (int) (50 * Math.random()); |
99 |
int green = (int) (50 * Math.random()); |
|
100 |
int blue = (int) (256 * Math.random()); |
|
2 | 101 |
Dimension d = getSize(); |
102 |
g.setColor(Color.black); |
|
103 |
FontMetrics fm = g.getFontMetrics(); |
|
104 |
space = fm.stringWidth(" "); |
|
105 |
for (StringTokenizer t = new StringTokenizer(labelString); |
|
8969 | 106 |
t.hasMoreTokens();) { |
2 | 107 |
String word = t.nextToken(); |
108 |
int w = fm.stringWidth(word) + space; |
|
109 |
if (x + w > d.width) { |
|
110 |
x = 0; |
|
111 |
y += fontSize; //move word to next line if it doesn't fit |
|
112 |
} |
|
8969 | 113 |
if (Math.random() < 0.5) { |
114 |
g.setColor(new java.awt.Color((red + y * 30) % 256, |
|
115 |
(green + x / 3) % 256, blue)); |
|
116 |
} else { |
|
2 | 117 |
g.setColor(getBackground()); |
8969 | 118 |
} |
2 | 119 |
g.drawString(word, x, y); |
120 |
x += w; //shift to the right to draw the next word |
|
121 |
} |
|
122 |
} |
|
123 |
||
8969 | 124 |
@Override |
2 | 125 |
public void stop() { |
126 |
timer.cancel(); //stops the timer |
|
127 |
} |
|
128 |
||
8969 | 129 |
@Override |
2 | 130 |
public String getAppletInfo() { |
131 |
return "Title: Blinker\n" |
|
8969 | 132 |
+ "Author: Arthur van Hoff\n" |
133 |
+ "Displays multicolored blinking text."; |
|
2 | 134 |
} |
135 |
||
8969 | 136 |
@Override |
2 | 137 |
public String[][] getParameterInfo() { |
138 |
String pinfo[][] = { |
|
8969 | 139 |
{ "speed", "string", "The blink frequency" }, |
140 |
{ "lbl", "string", "The text to blink." }, }; |
|
2 | 141 |
return pinfo; |
142 |
} |
|
143 |
} |