提交 a7dd4655 编写于 作者: M mrkam

7027687: /applets/NervousText demo needs to be improved

Reviewed-by: alexp
上级 fb07f23a
/* /*
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -29,37 +29,37 @@ ...@@ -29,37 +29,37 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
*/
import java.awt.event.*;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Font; import java.awt.Font;
import java.applet.Applet; import java.applet.Applet;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/** /**
* An applet that displays jittering text on the screen. * An applet that displays jittering text on the screen.
* *
* @author Daniel Wyszynski 04/12/95 * @author Daniel Wyszynski 04/12/95
* @modified 05/09/95 kwalrath Changed string; added thread suspension * @author 05/09/95 kwalrath Changed string; added thread suspension
* @modified 02/06/98 madbot removed use of suspend and resume and cleaned up * @author 02/06/98 madbot removed use of suspend and resume and cleaned up
*/ */
@SuppressWarnings("serial")
public class NervousText extends Applet implements Runnable, MouseListener { public class NervousText extends Applet implements Runnable, MouseListener {
String banner; // The text to be displayed String banner; // The text to be displayed
char bannerChars[]; // The same text as an array of characters char bannerChars[]; // The same text as an array of characters
char attributes[]; // Character attributes ('^' for superscript) char attributes[]; // Character attributes ('^' for superscript)
Thread runner = null; // The thread that is displaying the text Thread runner = null; // The thread that is displaying the text
boolean threadSuspended; // True when thread suspended (via mouse click) boolean threadSuspended; // True when thread suspended (via mouse click)
static final int REGULAR_WD = 15; static final int REGULAR_WD = 15;
static final int REGULAR_HT = 36; static final int REGULAR_HT = 36;
static final int SMALL_WD = 12; static final int SMALL_WD = 12;
static final int SMALL_HT = 24; static final int SMALL_HT = 24;
Font regularFont = new Font("Serif", Font.BOLD, REGULAR_HT); Font regularFont = new Font("Serif", Font.BOLD, REGULAR_HT);
Font smallFont = new Font("Serif", Font.BOLD, SMALL_HT); Font smallFont = new Font("Serif", Font.BOLD, SMALL_HT);
@Override
public void init() { public void init() {
banner = getParameter("text"); banner = getParameter("text");
if (banner == null) { if (banner == null) {
...@@ -67,8 +67,8 @@ public class NervousText extends Applet implements Runnable, MouseListener { ...@@ -67,8 +67,8 @@ public class NervousText extends Applet implements Runnable, MouseListener {
} }
int bannerLength = banner.length(); int bannerLength = banner.length();
StringBuffer bc = new StringBuffer(bannerLength); StringBuilder bc = new StringBuilder(bannerLength);
StringBuffer attrs = new StringBuffer(bannerLength); StringBuilder attrs = new StringBuilder(bannerLength);
int wd = 0; int wd = 0;
for (int i = 0; i < bannerLength; i++) { for (int i = 0; i < bannerLength; i++) {
char c = banner.charAt(i); char c = banner.charAt(i);
...@@ -89,7 +89,7 @@ public class NervousText extends Applet implements Runnable, MouseListener { ...@@ -89,7 +89,7 @@ public class NervousText extends Applet implements Runnable, MouseListener {
} }
bannerLength = bc.length(); bannerLength = bc.length();
bannerChars = new char[bannerLength]; bannerChars = new char[bannerLength];
attributes = new char[bannerLength]; attributes = new char[bannerLength];
bc.getChars(0, bannerLength, bannerChars, 0); bc.getChars(0, bannerLength, bannerChars, 0);
attrs.getChars(0, bannerLength, attributes, 0); attrs.getChars(0, bannerLength, attributes, 0);
...@@ -99,15 +99,18 @@ public class NervousText extends Applet implements Runnable, MouseListener { ...@@ -99,15 +99,18 @@ public class NervousText extends Applet implements Runnable, MouseListener {
addMouseListener(this); addMouseListener(this);
} }
@Override
public void destroy() { public void destroy() {
removeMouseListener(this); removeMouseListener(this);
} }
@Override
public void start() { public void start() {
runner = new Thread(this); runner = new Thread(this);
runner.start(); runner.start();
} }
@Override
public synchronized void stop() { public synchronized void stop() {
runner = null; runner = null;
if (threadSuspended) { if (threadSuspended) {
...@@ -116,22 +119,24 @@ public class NervousText extends Applet implements Runnable, MouseListener { ...@@ -116,22 +119,24 @@ public class NervousText extends Applet implements Runnable, MouseListener {
} }
} }
@Override
public void run() { public void run() {
Thread me = Thread.currentThread(); Thread me = Thread.currentThread();
while (runner == me) { while (runner == me) {
try { try {
Thread.sleep(100); Thread.sleep(100);
synchronized(this) { synchronized (this) {
while (threadSuspended) { while (threadSuspended) {
wait(); wait();
} }
} }
} catch (InterruptedException e){ } catch (InterruptedException e) {
} }
repaint(); repaint();
} }
} }
@Override
public void paint(Graphics g) { public void paint(Graphics g) {
int length = bannerChars.length; int length = bannerChars.length;
for (int i = 0, x = 0; i < length; i++) { for (int i = 0, x = 0; i < length; i++) {
...@@ -152,33 +157,41 @@ public class NervousText extends Applet implements Runnable, MouseListener { ...@@ -152,33 +157,41 @@ public class NervousText extends Applet implements Runnable, MouseListener {
} }
} }
@Override
public synchronized void mousePressed(MouseEvent e) { public synchronized void mousePressed(MouseEvent e) {
e.consume(); e.consume();
threadSuspended = !threadSuspended; threadSuspended = !threadSuspended;
if (!threadSuspended) if (!threadSuspended) {
notify(); notify();
}
} }
@Override
public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) {
} }
@Override
public void mouseEntered(MouseEvent e) { public void mouseEntered(MouseEvent e) {
} }
@Override
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e) {
} }
@Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
} }
@Override
public String getAppletInfo() { public String getAppletInfo() {
return "Title: NervousText\nAuthor: Daniel Wyszynski\nDisplays a text banner that jitters."; return "Title: NervousText\nAuthor: Daniel Wyszynski\n"
+ "Displays a text banner that jitters.";
} }
@Override
public String[][] getParameterInfo() { public String[][] getParameterInfo() {
String pinfo[][] = { String pinfo[][] = {
{"text", "string", "Text to display"}, { "text", "string", "Text to display" }, };
};
return pinfo; return pinfo;
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册