import java.applet.*;
import java.awt.*;
public class WalkingText extends Applet implements Runnable {
protected String mesg = null;
protected int xloc, yloc, width, height, textWidth, textHeight;
protected Thread t;
protected boolean done = false;
/** How long to nap for each move */
protected int napTime = 150;
/** Applet Initializer */
public void init() {
xloc = 0;
//获得屏幕的大小
width = getSize().width;
height = getSize().height;
if ((mesg = getParameter("text")) == null)
mesg = "Hello World of Java";
String pSize = getParameter("fontsize");
if (pSize == null)
pSize = "12";
String fontName = getParameter("fontName");
if (fontName == null)
fontName = "Helvetica";
// System.out.println("Font is " + pSize + " point " + fontName);
Font f = new Font(fontName, Font.PLAIN, Integer.parseInt(pSize));
setFont(f);
FontMetrics fm = getToolkit().getFontMetrics(f);
textWidth = fm.stringWidth(mesg);
textHeight = fm.getHeight();
// use textHeight in y coordinate calculation
yloc = height - ((height-textHeight) / 2);
}
/** This is important: we create a thread, so we must kill it */
public void stop() {
done = true;
t = null;
}
/** create the thread and start it. */
public void start() {
if (t == null)
t = new Thread(this);
done = false;
t.start();
}
// Usage:
public String[][] getParameterInfo() {
String[][] params = {
{ "text", "text", "Text to walk across the screen" },
{ "fontName", "text", "Name of font to display in" },
{ "fontsize", "int", "How big to make the text" },
};
return params;
}
/** Run is called by the Thread class when there is work to do */
public void run() {
while (!done) {
if ((xloc+=5) > getSize().width)
xloc = 0;
repaint();
try {
Thread.sleep(napTime);
} catch (Exception e) {
System.out.println("Who dares to interrupt my Sleep()? " + e);
};
}
}
/** Paint is called by Applet to redraw the screen */
public void paint(Graphics g) {
g.drawString(mesg, xloc, yloc);
}
}
一个java 线程 的小例子
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
一个线程,从“生”到“死”经历的过程
通用的线程生命周期总体上可以分为五个状态:初始状态、可运行状态、运行状态、休眠状态和终止状态。
Java 生命周期 休眠状态 线程生命周期 -
一个Timer的小例子
对于在一段时间内,循环执行的任务我们可以使用Timer来实现。
职场 Timer 休闲 一个 小例子 -
【JAVA】一个抓取Web图片的小例子
try { URL url = new URL(inUrl);  
职场 休闲 web图片 JPEGCodec rt.jar -
因子分析的一个小例子
这是学习笔记的第 1997 篇文章
因子分析的一个小例子 -
Laravel实现一个简单的小例子
第一次学习框架,写错的地方麻烦各位大侠指导我一下首先实现过程如下:看完流程图获得了大概的印象吧,趁热打铁,把你的印象变成知识
Laravel PHP框架 php laravel Test