Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/applets/NervousText/NervousText.java
38829 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import java.awt.Graphics;42import java.awt.Font;43import java.applet.Applet;44import java.awt.event.MouseEvent;45import java.awt.event.MouseListener;464748/**49* An applet that displays jittering text on the screen.50*51* @author Daniel Wyszynski 04/12/9552* @author 05/09/95 kwalrath Changed string; added thread suspension53* @author 02/06/98 madbot removed use of suspend and resume and cleaned up54*/55@SuppressWarnings("serial")56public class NervousText extends Applet implements Runnable, MouseListener {5758String banner; // The text to be displayed59char bannerChars[]; // The same text as an array of characters60char attributes[]; // Character attributes ('^' for superscript)61Thread runner = null; // The thread that is displaying the text62boolean threadSuspended; // True when thread suspended (via mouse click)63static final int REGULAR_WD = 15;64static final int REGULAR_HT = 36;65static final int SMALL_WD = 12;66static final int SMALL_HT = 24;67Font regularFont = new Font("Serif", Font.BOLD, REGULAR_HT);68Font smallFont = new Font("Serif", Font.BOLD, SMALL_HT);6970@Override71public void init() {72banner = getParameter("text");73if (banner == null) {74banner = "HotJava";75}7677int bannerLength = banner.length();78StringBuilder bc = new StringBuilder(bannerLength);79StringBuilder attrs = new StringBuilder(bannerLength);80int wd = 0;81for (int i = 0; i < bannerLength; i++) {82char c = banner.charAt(i);83char a = 0;84if (c == '^') {85i++;86if (i < bannerLength) {87c = banner.charAt(i);88a = '^';89wd += SMALL_WD - REGULAR_WD;90} else {91break;92}93}94bc.append(c);95attrs.append(a);96wd += REGULAR_WD;97}9899bannerLength = bc.length();100bannerChars = new char[bannerLength];101attributes = new char[bannerLength];102bc.getChars(0, bannerLength, bannerChars, 0);103attrs.getChars(0, bannerLength, attributes, 0);104105threadSuspended = false;106resize(wd + 10, 50);107addMouseListener(this);108}109110@Override111public void destroy() {112removeMouseListener(this);113}114115@Override116public void start() {117runner = new Thread(this);118runner.start();119}120121@Override122public synchronized void stop() {123runner = null;124if (threadSuspended) {125threadSuspended = false;126notify();127}128}129130@Override131public void run() {132Thread me = Thread.currentThread();133while (runner == me) {134try {135Thread.sleep(100);136synchronized (this) {137while (threadSuspended) {138wait();139}140}141} catch (InterruptedException e) {142}143repaint();144}145}146147@Override148public void paint(Graphics g) {149int length = bannerChars.length;150for (int i = 0, x = 0; i < length; i++) {151int wd, ht;152if (attributes[i] == '^') {153wd = SMALL_WD;154ht = SMALL_HT;155g.setFont(smallFont);156} else {157wd = REGULAR_WD;158ht = REGULAR_HT;159g.setFont(regularFont);160}161int px = (int) (10 * Math.random() + x);162int py = (int) (10 * Math.random() + ht);163g.drawChars(bannerChars, i, 1, px, py);164x += wd;165}166}167168@Override169public synchronized void mousePressed(MouseEvent e) {170e.consume();171threadSuspended = !threadSuspended;172if (!threadSuspended) {173notify();174}175}176177@Override178public void mouseReleased(MouseEvent e) {179}180181@Override182public void mouseEntered(MouseEvent e) {183}184185@Override186public void mouseExited(MouseEvent e) {187}188189@Override190public void mouseClicked(MouseEvent e) {191}192193@Override194public String getAppletInfo() {195return "Title: NervousText\nAuthor: Daniel Wyszynski\n"196+ "Displays a text banner that jitters.";197}198199@Override200public String[][] getParameterInfo() {201String pinfo[][] = {202{ "text", "string", "Text to display" }, };203return pinfo;204}205}206207208