📄 firetree.java
字号:
// 程序:烟火树粒子
// 范例文件:FireTree.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class FireTree extends Applet implements Runnable
{
final int Max = 1000;
ftparticle p[];
int AppletWidth,AppletHeight,XCenter,YCenter;
Image OffScreen;
Graphics drawOffScreen;
Thread pThread;
public void init()
{
setBackground(Color.black);
p = new ftparticle[Max];
AppletWidth = getSize().width;
AppletHeight = getSize().height;
XCenter = AppletWidth/2;
YCenter = 2*AppletHeight/3;
for(int i=0; i<Max; i++)
p[i] = new ftparticle(XCenter,YCenter);
OffScreen = createImage(AppletWidth,AppletHeight);
drawOffScreen = OffScreen.getGraphics();
}
public void start()
{
pThread = new Thread(this);
pThread.start();
}
public void stop()
{
pThread = null;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(OffScreen,0,0,this);
}
public void run()
{
int i;
while(true)
{
drawOffScreen.clearRect(0,0,AppletWidth,AppletHeight);
drawOffScreen.setColor(Color.white);
drawOffScreen.fillRect(XCenter,YCenter,5,AppletHeight/3);
for(i=0; i<Max; i++)
{
if(p[i].Y > AppletHeight)
p[i].reset(XCenter,YCenter);
drawOffScreen.setColor(p[i].color);
drawOffScreen.fillOval((int)p[i].X,(int)p[i].Y,3,3);
p[i].X += p[i].Vx;
p[i].Y += p[i].Vy;
p[i].Vy += 9.8*p[i].time/5;
p[i].time++;
}
repaint();
try {
Thread.sleep(200);
}
catch (InterruptedException e) { }
}
}
}
class ftparticle
{
boolean state;
double X,Y;
double Vx,Vy;
int time;
Color color;
public ftparticle(int x,int y)
{
X = x;
Y = y;
reset(x,y);
}
public void reset(int x, int y)
{
X = x;
Y = y;
Vx = (int)(Math.random()*10 - Math.random()*10);
Vy = (int)(-Math.random()*20 - 1);
time = 0;
color = new Color((int)(Math.random()*255),
(int) (Math.random()*255),
(int) (Math.random()*255));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -