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;
   
 }
 }
}


以下是一个使用Java实现粒子爱心特效的代码示例: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ParticleHeartEffect extends JPanel implements ActionListener { private Timer timer; private Particle[] particles; public ParticleHeartEffect() { particles = new Particle[1000]; for (int i = 0; i < particles.length; i++) { particles[i] = new Particle(); } timer = new Timer(10, this); timer.start(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.RED); for (Particle particle : particles) { g2d.fillOval((int) particle.x, (int) particle.y, 2, 2); } } @Override public void actionPerformed(ActionEvent e) { for (Particle particle : particles) { particle.update(); } repaint(); } private class Particle { private double x; private double y; private double vx; private double vy; public Particle() { x = ParticleHeartEffect.this.getWidth() / 2; y = ParticleHeartEffect.this.getHeight() / 2; double angle = Math.random() * 2 * Math.PI; double speed = Math.random() * 2 + 1; vx = Math.cos(angle) * speed; vy = Math.sin(angle) * speed; } public void update() { x += vx; y += vy; if (x < 0 || x > ParticleHeartEffect.this.getWidth() || y < 0 || y > ParticleHeartEffect.this.getHeight()) { x = ParticleHeartEffect.this.getWidth() / 2; y = ParticleHeartEffect.this.getHeight() / 2; double angle = Math.random() * 2 * Math.PI; double speed = Math.random() * 2 + 1; vx = Math.cos(angle) * speed; vy = Math.sin(angle) * speed; } } } public static void main(String[] args) { JFrame frame = new JFrame("Particle Heart Effect"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); frame.setLocationRelativeTo(null); ParticleHeartEffect particleHeartEffect = new ParticleHeartEffect(); frame.add(particleHeartEffect); frame.setVisible(true); } } ``` 这段代码使用Java的Swing库实现了一个粒子爱心特效。它创建了一个面板,并在面板上绘制了一些红色的小圆点,模拟了粒子的运动轨迹。通过定时器不断更新粒子的位置,实现了流动的爱心形状动画特效。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值