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