雪花漂漂 站点:爱心种子小博士 关键字:雪花漂漂
|
雪花漂漂 程序运行图:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.Random;
public class TestSnow extends JFrame{
AnimationCanvas canvas;
JButton startButton,stopButton;
public TestSnow(){
super("Snow");
Container container=getContentPane();
canvas=new AnimationCanvas();
container.add(canvas);
startButton=new JButton("Start");
startButton.addActionListener(new ButtonListener());
stopButton=new JButton("Stop");
stopButton.addActionListener(new ButtonListener());
JPanel panel=new JPanel();
panel.add(startButton);panel.add(stopButton);
container.add(BorderLayout.SOUTH,panel);
addWindowListener(new WindowEventHandler());
setSize(600,500);
show();
}
class WindowEventHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JButton temp=(JButton)e.getSource();
if(temp.equals(startButton)){
canvas.start();
}else if(temp.equals(stopButton)){
canvas.stop();
}
}
}
public static void main(String args[]){
new TestSnow();
}
}
class AnimationCanvas extends JPanel implements Runnable
{
Thread mainThread ;
Image image;
BufferedImage bi;
Random rand ;
int snows, wind, threadSleep, snowSize ;
int[] snowX, snowY ;
// long stopTime = 0;
Dimension dim ;
MediaTracker mt ;
AnimationCanvas()
{
rand = new Random();
setSize(600,450);
dim=getSize();
snows =150;
snowSize =3 ;
threadSleep =80;
snowX = new int[snows];
snowY = new int[snows];
for(int i = 0; i < snows; i++)
{
snowX[i] = rand.nextInt() % (dim.width / 2)
+ dim.width / 2;
snowY[i] = rand.nextInt() % (dim.height / 2)
+ dim.height / 2;
}
image=getToolkit().getImage("022.jpg");
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try
{
mt.waitForID(0);
}
catch(InterruptedException _ex) { return; }
}
public void start()
{
if(mainThread == null)
{
mainThread = new Thread(this);
mainThread.start();
}
}
public void stop()
{
if(mainThread!=null)
mainThread.interrupt();
mainThread = null;
}
public void run()
{
while(mainThread != null)
{
try
{
Thread.sleep(threadSleep);
}
catch(InterruptedException _ex) { return ; }
repaint();
}
}
public void drawBackSnow(Graphics2D g)
{
g.setColor(Color.white);
for(int i = 0; i < snows; i++)
{
g.fillOval(snowX[i], snowY[i], snowSize, snowSize);
snowX[i] += rand.nextInt() % 2 + wind;
snowY[i] += (rand.nextInt() % 6 + 5) / 5 + 1;
if(snowX[i] >= dim.width) snowX[i] = 0;
if(snowX[i] < 0) snowX[i] = dim.width - 1;
if(snowY[i] >= dim.height || snowY[i] < 0)
{
snowX[i] = Math.abs(rand.nextInt() % dim.width);
snowY[i] = 0;
}
}
wind = rand.nextInt() % 5 - 2 ;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
Dimension d=getSize();
bi=new BufferedImage(d.width,d.height,BufferedImage.TYPE_INT_ARGB);
Graphics2D big=bi.createGraphics();
big.setColor(Color.black);
big.fillRect(0, 0, d.width,d.height);
big.drawImage(image, 0, 0, this);
drawBackSnow(big);
g2d.drawImage(bi, 0, 0, null);
big.dispose();
}
}
|
|
|
|