大家好呀,我是计算机专业的学生,我有个问题想请教哦。
import javax.swing.JApplet;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class RaceApplet extends JApplet implements Runnable {
private final static int NUMRUNNERS = 2;
private final static int SPACING = 20;
private Runner[] runners = new Runner[NUMRUNNERS];
private Thread updateThread = null;
public void init() {
String raceType = getParameter("type");
for (int i = 0; i < NUMRUNNERS; i++) {
runners[i] = new Runner();
if (raceType.compareTo("unfair") == 0)
runners[i].setPriority(i+2);
else
runners[i].setPriority(2);
}
if (updateThread == null) {
updateThread = new Thread(this, "Thread Race");
updateThread.setPriority(NUMRUNNERS+2);
}
addMouseListener(new MyAdapter());
setContentPane(new AppletContentPane());
}
class MyAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent evt) {
if (!updateThread.isAlive())
updateThread.start();
for (int i = 0; i < NUMRUNNERS; i++) {
if (!runners[i].isAlive())
runners[i].start();
}
}
}
public void run() {
Thread myThread = Thread.currentThread();
while (updateThread == myThread) {
repaint();
try {
Thread.sleep(3);
} catch (InterruptedException e) { }
}
}
public void stop() {
for (int i = 0; i < NUMRUNNERS; i++) {
if (runners[i].isAlive())
runners[i] = null;
}
if (updateThread.isAlive())
updateThread = null;
}
class AppletContentPane extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width, getSize().height);
g.setColor(Color.black);
for (int i = 0; i < NUMRUNNERS; i++) {
int pri = runners[i].getPriority();
g.drawString(new Integer(pri).toString(), 0, (i+1)*SPACING);
}
for (int i = 0; i < NUMRUNNERS; i++) {
g.drawLine(SPACING, (i+1)*SPACING,
SPACING + (runners[i].tick)/1000, (i+1)*SPACING);
}
}
}
}
这个RaceApplet.java的html是怎么写的呢?我试了很久也没有结果,请大家指点迷津哦!!!
不胜感谢!
3.12 植 树 节 ,我 们 期 待 绿 水 青 山
在 这 个 大 地 回 春 、欣 欣 向 荣 的 日 子 里 ,您 履 行 植 树 义 务 了 吗 ?
----------------解决方案--------------------------------------------------------
<applet code=RaceApplet.class width=300 height=300></applet>
----------------解决方案--------------------------------------------------------
大哥这样我试过了呀,不可以的呀
下面是Runner类的代码,你能将此调试成功后再告诉我吗?
不胜感谢哦!!!
----------------解决方案--------------------------------------------------------
<HTML>
<HEAD>
</HEAD>
<BODY BGCOLOR="000000">
<CENTER>
<APPLET
code = "RaceApplet.class"
width = 500
height = 500
>
<param name=type value="unfair">
</APPLET>
</CENTER>
</BODY>
</HTML>
但是你的程序我看不懂
Runner 是干什么的 是不是应该是Runnable类呢
runners[i].tick 是什么意思啊
----------------解决方案--------------------------------------------------------
这样还是不对的呀,谁能调试好后再发给我吗,我实在是不知道是怎么回事的哦?
谢谢
----------------解决方案--------------------------------------------------------
还是没有看到你的Runner类的代码啊
----------------解决方案--------------------------------------------------------
代码没给全啊……这让我们怎么调试
----------------解决方案--------------------------------------------------------
不好意思哦
Runner类的代码如下:
public class Runner extends Thread {
public int tick = 1;
public void run() {
while (tick < 400000000)
tick++;
}
}
----------------解决方案--------------------------------------------------------
你这个程序编译能通过但是运行时会抛出空指针异常,我看了你的代码
也不知道你这个applet到底想实现什么功能
具体说一下你想实现的功能,我帮你改造一下
----------------解决方案--------------------------------------------------------
谢谢各位了哦,我现在已经明白了,是请教我们老师的哦!
这个程序是有关优先级高低,线程执行的快慢的问题了,大家可以看看了哦
下面是这个程序的HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<applet code="RaceApplet.class" width=200 height=300>
<PARAM name=type value="unfair">
</applet>
<BODY>
</BODY>
</HTML>
是老师给的哦
----------------解决方案--------------------------------------------------------