java实现粒子效果(瀑布)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class k extends JFrame {
 
 private MyPanel panel ;
 public k(){
  panel = new MyPanel();
  this.getContentPane().add(panel);
  
  this.setSize(400,400);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setVisible(true);
 }
 public static void main(String[] args) {
  new k();
 }
 
 class MyPanel extends JPanel implements Runnable{
  final int Max=1000;
  Wparticle p[]; 
  int AppletWidth,AppletHeight,XCenter,YCenter=200; 
  BufferedImage OffScreen ;//=new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_BGR);
  
  Graphics drawOffScreen;
  Thread pThread;
  public MyPanel(){
   
   setBackground(Color.black); 
      AppletWidth=400;//getSize().width;
      AppletHeight=400;//getSize().height;
      p=new Wparticle[Max];
      for(int i=0;i<Max;i++)
       p[i]=new Wparticle();  
      //OffScreen=createImage(AppletWidth,AppletHeight);
      OffScreen =new BufferedImage(AppletWidth,AppletHeight,BufferedImage.TYPE_INT_BGR);
      drawOffScreen=OffScreen.getGraphics();
      pThread = new Thread(this);
      pThread.start();
  }
  @Override
  protected void paintComponent(Graphics g) {
   super.paintComponent(g);  
   g.drawImage(OffScreen,0,0,this);
   
  } 
  
  @Override
  public void run() {
   // TODO Auto-generated method stub
    boolean reset=false;
      int i,t=0;
      while(true){
       drawOffScreen.clearRect(0, 0, AppletWidth, AppletHeight);
       drawOffScreen.setColor(Color.white);
       drawOffScreen.drawLine(0,15,10,15);     
       for(i=0;i<Max;i++){     
        drawOffScreen.fillOval((int)p[i].X,(int)p[i].Y,3,3);
     
        p[i].X=p[i].X+p[i].Vx;
        if(p[i].X>10){
      
         p[i].Y+=p[i].Vy*p[i].time/1000;
         p[i].Vy=(int)9.8*p[i].time;
         p[i].time++;
        }
    
        if(p[i].Y>AppletHeight){
       
         p[i].reset();
        }
       }
    
       repaint();
      
       try{
        
        Thread.sleep(100);
       }catch(InterruptedException e){}
      }
   }
  }
  
 class Wparticle{
 double X,Y;
 double Vx,Vy; 
 int time;
 public Wparticle(){
    reset();
 }
 public void reset(){
    X=(int)(Math.random()*-40); 
    Y=(int)(Math.random()*5+10);
    Vx=Math.random()*3+1.0;  
    Vy=0;
    time=0;
   
 }
 }
}


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
粒子群算法是一种优化算法,可以用于解决许多问题,例如函数优化、组合优化和机器学习等。下面是Java实现粒子群算法的步骤: ```java // 定义粒子类 class Particle { double[] position; // 粒子位置 double[] velocity; // 粒子速度 double[] pBest; // 个体最优位置 double fitness; // 适应度值 public Particle(int dim) { position = new double[dim]; velocity = new double[dim]; pBest = new double[dim]; fitness = Double.MAX_VALUE; } } // 定义粒子群类 class PSO { int swarmSize; // 粒子群大小 int maxIter; // 最大迭代次数 double w; // 惯性权重 double c1, c2; // 学习因子 double[] gBest; // 全局最优位置 double gBestFitness; // 全局最优适应度值 Particle[] swarm; // 粒子群 public PSO(int swarmSize, int maxIter, double w, double c1, double c2) { this.swarmSize = swarmSize; this.maxIter = maxIter; this.w = w; this.c1 = c1; this.c2 = c2; swarm = new Particle[swarmSize]; gBest = new double[swarm[0].position.length]; gBestFitness = Double.MAX_VALUE; for (int i = 0; i < swarmSize; i++) { swarm[i] = new Particle(gBest.length); for (int j = 0; j < gBest.length; j++) { swarm[i].position[j] = Math.random() * 10; // 初始化粒子位置 swarm[i].velocity[j] = Math.random() * 2; // 初始化粒子速度 } } } // 更新粒子位置和速度 public void update() { for (int i = 0; i < swarmSize; i++) { Particle p = swarm[i]; for (int j = 0; j < p.position.length; j++) { // 更新速度 p.velocity[j] = w * p.velocity[j] + c1 * Math.random() * (p.pBest[j] - p.position[j]) + c2 * Math.random() * (gBest[j] - p.position[j]); // 更新位置 p.position[j] += p.velocity[j]; } // 更新个体最优位置 double fitness = evaluate(p.position); if (fitness < p.fitness) { p.fitness = fitness; System.arraycopy(p.position, 0, p.pBest, 0, p.position.length); } // 更新全局最优位置 if (fitness < gBestFitness) { gBestFitness = fitness; System.arraycopy(p.position, 0, gBest, 0, p.position.length); } } } // 计算适应度值 public double evaluate(double[] x) { // TODO: 根据具体问题实现适应度函数 return 0; } // 运行粒子群算法 public void run() { for (int i = 0; i < maxIter; i++) { update(); } } } // 使用粒子群算法解决函数优化问题的例子 public class Main { public static void main(String[] args) { PSO pso = new PSO(30, 100, 0.6, 1.5, 1.5); pso.run(); System.out.println("最优解:" + Arrays.toString(pso.gBest)); System.out.println("最优适应度值:" + pso.gBestFitness); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值