?? thunder.java
字號:
import java.applet.*;
import java.awt.*;
public class Thunder extends Applet implements Runnable
{
private Thread thread = null;
private boolean no_thunder = true;
private boolean thunder = true;
private int[] light;
private int[] b1;
private int[] b2;
private Color whiteSky = new Color(0,0,65), yellowSky = new Color(144,40,40);
private Image buffer, image;
private String delay = "3";
public void init()
{
image = getImage(getCodeBase(),"City.gif");
light = new int[getSize().height];
b1 = new int[getSize().height];
b2 = new int[getSize().height];
buffer = this.createImage(getSize().width, getSize().height);
}
public void paint(Graphics g)
{
int i, thr;
if (no_thunder)
{
g.setColor(Color.black);
g.fillRect(0 , 0 , getSize().width, getSize().height);
g.drawImage(image,0,0,this);
}
else
{
if(thunder)
g.setColor(whiteSky);
else
g.setColor(yellowSky);
g.fillRect(0 , 0 , getSize().width, getSize().height);
thr = (int) (0.8F * getSize().height);
for (i = 1; i < getSize().height; i++)
{
if (i < thr)
{
g.setColor(Color.darkGray);
g.drawRect(light[i]-4, i, 3, 1);
g.drawRect(light[i]+2, i, 3, 1);
g.setColor(Color.gray);
g.drawRect(light[i]-1, i, 1, 1);
g.drawRect(light[i]+1, i, 1, 1);
}
if(thunder)
{
g.setColor(Color.white);
}
else
g.setColor(Color.yellow);
g.drawLine(light[i], i, light[i-1], i-1);
if (b1[i] >= 0)
{
g.drawLine(b1[i], i, b1[i-1], i-1);
}
if (b2[i] >= 0)
{
g.drawLine(b2[i], i, b2[i-1], i-1);
}
}
g.drawImage(image,0,0,this);
thunder = !thunder;
}
}
void drawBuffer()
{
Graphics g;
g = buffer.getGraphics();
paint(g);
}
public void start()
{
if (thread == null)
{
thread = new Thread(this);
thread.start();
}
}
public void stop()
{
if (thread != null)
{
thread.stop();
thread = null;
}
}
void createThunder()
{
int i;
int bs1, bs2;
int be1, be2;
light[0] = (int) (Math.random() * getSize().width);
b1[0] = light[0];
b2[0] = light[0];
bs1 = (int) (Math.random() * getSize().height) + 1;
bs2 = (int) (Math.random() * getSize().height) + 1;
be1 = bs1 + (int) (0.5 * Math.random() * getSize().height) + 1;
be2 = bs2 + (int) (0.5 * Math.random() * getSize().height) + 1;
for (i = 1; i<getSize().height; i++)
{ light[i] = light[i-1] + ((Math.random() >0.5)?1:-1);
b1[i] = light[i];
b2[i] = light[i];
}
for (i = bs1; i<getSize().height; i++)
{
b1[i] = b1[i-1] + ((Math.random() >0.5)?2:-2);
}
for (i = bs2; i<getSize().height; i++)
{
b2[i] = b2[i-1] + ((Math.random() >0.5)?2:-2);
}
for (i = be1; i<getSize().height; i++)
{
b1[i] = -1;
}
for (i = be2; i<getSize().height; i++)
{
b2[i] = -1;
}
}
public void run()
{
Graphics g;
while (true)
{
try
{
drawBuffer();
g = this.getGraphics();
g.drawImage(buffer, 0, 0, this);
Thread.sleep((int) (Integer.parseInt(delay) * 1000 * Math.random()));
no_thunder = false;
createThunder();
drawBuffer();
g = this.getGraphics();
g.drawImage(buffer, 0, 0, this);
Thread.sleep(1000);
no_thunder = true;
}
catch (InterruptedException e)
{
stop();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -