java线程的例子_一个java 线程 的小例子

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值