⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 firetree.java

📁 JAVA2游戏程序设计源码 可以帮助初学者或者只想改改程序就能用的人
💻 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 + -