欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java粒子特效喷薄的彩球

程序员文章站 2022-05-28 14:30:10
...

喷薄的彩球其实只是最初的一个粒子特效原型,在我的代码中其实有添加了背景音乐,然后我修改了初始彩球位置,还有彩球的大小,然后加了一个葫芦娃拿着葫芦的图片Java粒子特效喷薄的彩球

然后大家应该都能想到,我彩球变成了他葫芦里喷出来的东西,效果如下:

Java粒子特效喷薄的彩球

背景音乐我本来想加一个葫芦娃的原声音乐,但是Java只支持无损音质(就是wav这类的),但我没找到,所以添加了个姚贝娜的迷恋当背景音乐(总之画风很奇怪,因为那是我原来就下载好的),总之大家学会了如何添加音乐,想加啥还不是自己说了算,下面就是背景音乐的添加方法:

 

try{
        	URL cb;
        	File f=new File("E:\\音乐\\迷恋.wav");
        	cb=f.toURL();
        	AudioClip ac;
        	ac=Applet.newAudioClip(cb);
        	ac.play();
        	ac.loop();//循环播放
        }catch (MalformedURLException e) {  
            e.printStackTrace();  
        }  

好了,前面都是小插曲,只是让你的作品更加生动,现在回归主题,重点是如何把彩球画出来,还要呈现喷薄的状态,当然为了绚丽夺目,还得有不断变化的每个小球的颜色,

 

首先最基本的画一个小球,调用Graphics g;   g.fillOval();  然后使他的颜色变化,可以使用RGB来变化,int r=0;r+=300;Color c=new Color(r);g.setColor(c);很简单吧。当然,为了是画面不要闪烁,画背景图片的时候最好还是采用双缓冲机制。然后利用循环结构不断地重复画小球,颜色不断变化,自己调节数据,改变初始位置,就可以做到上图的效果了,当然至于怎样喷薄有多种算法,大家可以自行开发,我只是用了其中一个,下面为完整代码,仅供大家参考,希望大家可以做出更多更有趣的粒子特效

 

import javax.swing.JFrame;


public class MainUI extends JFrame{

	private MyPanel panel;
	public MainUI(){
		panel=new MyPanel();
		this.getContentPane().add(panel);//初始化一个容器,用来在容器上添加一些控件
		
		this.setSize(1300, 1000);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new MainUI();

	}

}
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class MyPanel extends JPanel implements Runnable{

	final int MAX=1000;
	Particle p[];
	int myWidth,myHeight,XCenter,YCenter=400;
	BufferedImage im;
	Graphics g;
	Thread pThread;
	Image image;
	
	public MyPanel(){
		this.setBackground(Color.BLACK);
		myWidth=1300;
		myHeight=1000;
		p=new Particle[MAX];
		for(int i=0;i<MAX;i++){
			p[i]=new Particle();
		}
		im=new BufferedImage(myWidth,myHeight,BufferedImage.TYPE_INT_RGB);
		image=new ImageIcon("C:\\Users\\Alienware\\Pictures\\Saved Pictures\\葫芦娃.jpg").getImage();
		/*try {  
		    im = ImageIO.read(new File("C:\\Users\\Alienware\\Pictures\\Saved Pictures\\葫芦娃.jpg"));  
		}catch(Exception e) {  
		    e.printStackTrace();  
		}  */
//		img=im.getImage();
		g=im.getGraphics();
		pThread=new Thread(this);
		pThread.start();
	}
	
	 
	
	protected void paintComponent(Graphics g){
		super.paintComponent(g);//整个面板用背景色重画一遍,起到清屏的作用 
		g.drawImage(im,0,0,this.getWidth(),this.getHeight(),null);
	}

	public void run() {
		try{
        	URL cb;
        	File f=new File("E:\\音乐\\迷恋.wav");
        	cb=f.toURL();
        	AudioClip ac;
        	ac=Applet.newAudioClip(cb);
        	ac.play();
        	ac.loop();//循环播放
        }catch (MalformedURLException e) {  
            e.printStackTrace();  
        }  
		int i,t=0;
		int r=0;
		while(true){
			g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),null);
//			g.clearRect(0, 0, myWidth, myHeight);
			for(i=0;i<MAX;i++){
				g.fillOval((int)p[i].X+720, (int)p[i].Y+180, 3+i/10, 3+i/10);
				p[i].X=p[i].X+p[i].Vx;
				r+=300;
				Color c=new Color(r);
				g.setColor(c);
				if(p[i].X>20){
					 p[i].Y+=p[i].Vy*p[i].time/2000;  
			         p[i].Vy=(int)9.8*p[i].time;  
			         p[i].time++; 
				}
				if(p[i].Y>myHeight){
					p[i].rest();
				}
			}
			
			repaint();
			try{  
		          Thread.sleep(50);  
		       }catch(InterruptedException e){}  
			}
			
	}
	
}
import java.awt.Color;


public class Particle {

	double X,Y;
	double Vx,Vy;
	int time;
	public Color color;
	public Particle(){
		rest();
	}
	public void rest(){
		 X=(int)(Math.random()*-80);
		 Y=(int)(Math.random()*5+10);  
		 Vx=Math.random()*8+1.0;    
		 Vy=0;  
		 time=0; 
	}
}


Java粒子特效喷薄的彩球