Crossfire JXClient, Trunk
Blink.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
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  *
15  * - Neither the name of Oracle nor the names of its
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 
32 /*
33  * This source code is provided to illustrate the usage of a given feature
34  * or technique and has been deliberately simplified. Additional steps
35  * required for a production-quality application, such as security checks,
36  * input validation and proper error handling, might not be present in
37  * this sample code.
38  */
39 
40 
41 
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;
59 
60 
61 public class Blink extends java.applet.Applet {
62 
63  private static final long serialVersionUID = -775844794477507646L;
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 
68  @Override
69  public void init() {
70  String blinkFrequency = getParameter("speed");
71  delay = (blinkFrequency == null) ? 400
72  : (1000 / Integer.parseInt(blinkFrequency));
73  labelString = getParameter("lbl");
74  if (labelString == null) {
75  labelString = "Blink";
76  }
77  Font font = new java.awt.Font("Serif", Font.PLAIN, 24);
78  setFont(font);
79  }
80 
81  @Override
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
85 
86  // overrides the run method to provide functionality
87  @Override
88  public void run() {
89  repaint();
90  }
91  }, delay, delay);
92  }
93 
94  @Override
95  public void paint(Graphics g) {
96  int fontSize = g.getFont().getSize();
97  int x = 0, y = fontSize, space;
98  int red = (int) (50 * Math.random());
99  int green = (int) (50 * Math.random());
100  int blue = (int) (256 * Math.random());
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);
106  t.hasMoreTokens();) {
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  }
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 {
117  g.setColor(getBackground());
118  }
119  g.drawString(word, x, y);
120  x += w; //shift to the right to draw the next word
121  }
122  }
123 
124  @Override
125  public void stop() {
126  timer.cancel(); //stops the timer
127  }
128 
129  @Override
130  public String getAppletInfo() {
131  return "Title: Blinker\n"
132  + "Author: Arthur van Hoff\n"
133  + "Displays multicolored blinking text.";
134  }
135 
136  @Override
137  public String[][] getParameterInfo() {
138  String pinfo[][] = {
139  { "speed", "string", "The blink frequency" },
140  { "lbl", "string", "The text to blink." }, };
141  return pinfo;
142  }
143 }