threadex.java
来自「how to make a game using java.」· Java 代码 · 共 80 行
JAVA
80 行
// ThreadLines.java
// [Imperial Snowman Soft]
import java.awt.*;
import java.applet.*;
import GameLib.*;
public class ThreadEx extends Applet implements Runnable {
Thread appletThread;
Color lineColor;
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
float red, green, blue;
int times = 0;
public void init()
{
times = Integer.parseInt(getParameter("times"));
setBackground(Color.black);
}
public void start()
{
if(appletThread==null)
{
appletThread = new Thread(this); //make a Thread of this Applet
appletThread.start(); //and start it
}
}
public void stop()
{
if(appletThread!=null)
{
appletThread.stop(); //stop the Thread
appletThread = null;
}
}
public void paint(Graphics g)
{
g.setColor(lineColor);
g.drawString("Times to go: " + times, 10, 40);
g.drawLine(x1,y1,x1,y2);
g.drawLine(x2,y1,x2,y2);
g.drawLine(x1,y1,x2,y2);
g.drawLine(x2,y1,x1,y2);
}
public void run()
{
while(times>=0)
{
x1 = Misc.rnd(this.size().width); //use our own GameLib method
x2 = Misc.rnd(this.size().width);
y1 = Misc.rnd(10, 20); //the other version of the rnd - method
y2 = Misc.rnd(this.size().height);
red = (float)Math.random();
green = (float)Math.random();
blue = (float)Math.random();
lineColor = new Color(red, green, blue); //create a new RGB color
repaint(); //force a call of the paint() method
try
{
appletThread.sleep(50); //makes this Applet (Thread) sleeping for 50 msec
}
catch(InterruptedException e) {}
times--;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?