JAVA Timer实现动画(银河星空 心形图)

版权声明:版权声明:本文为博主原创文章,博客地址:http://blog.csdn.net/qq_37808895,未经博主允许不得转载。 https://blog.csdn.net/qq_37808895/article/details/88623208

不得不说学好数学的好处心形线(又称心脏线 : 在曼德博集合正中间的图形便是一个心脏线)
本程序用的是极坐标方程

水平方向: ρ=a(1-cosθ) 或 ρ=a(1+cosθ) (a>0)
垂直方向: ρ=a(1-sinθ) 或 ρ=a(1+sinθ) (a>0)

在这里插入图片描述

程序代码

实现原理同上篇博客Timer 中断 Canvas重画
带上音乐背景效果还不错,因为是大一时写的还未学到java的线程,只用了Timer来实现相同效果,画出多重心形线重叠的图案,带有不同颜色,程序比较简单凑合看吧


package animation;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


public class HeartJFrame extends JFrame {

	public boolean sign;
	private HeartCanvas canvas;
	File f;
	URI uri;
	URL url;
	public void Music(){
		try{
			this.f=new File("Faded.wav");
			uri=f.toURI();
			url=uri.toURL();//解析地址
			AudioClip aau;
			aau=Applet.newAudioClip(url);
			aau.loop();
			}catch(Exception e){e.printStackTrace();}
	}
	
	
	public HeartJFrame(){
		super("Heart");
		Dimension dim=getToolkit().getScreenSize();
	    this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2);
		setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.canvas=new HeartCanvas();
		this.getContentPane().add(this.canvas,"Center");
		canvas.setBackground(Color.black);
		
		this.setVisible(true);
		this.Music();
		
	}
	/*public void run(){
		try{
			this.f=new File("Faded.wav");
			uri=f.toURI();
			url=uri.toURL();//解析地址
			AudioClip aau;
			aau=Applet.newAudioClip(url);
			aau.loop();
			Thread.sleep(10);
			}catch(Exception e){e.printStackTrace();}
	}*/
	public static void main(String[] args) {
		new HeartJFrame();
	}
}
class HeartCanvas extends Canvas implements ActionListener{
	private Timer timer;
	private Color color;
	public HeartCanvas(){
		
		timer=new Timer(5000,this);
		timer.start();
	}
	public void paint(Graphics g){
		this.color=new Color(
				(new Double(Math.random() * 128)).intValue() + 128,
				(new Double(Math.random() * 128)).intValue() + 128,
				(new Double(Math.random() * 128)).intValue() + 128);
		int x0=this.getWidth()/2;
		int y0=this.getHeight()/2;
		g.setColor(this.color);
		
		for(int i=0;i<10000;i++){
			g.fillOval(new Random().nextInt(6000),new Random().nextInt(6000),5,5);
			}
		for(int j=70;j<240;j+=10){
			for(int i=0;i<1023;i++){
				double angle=i*Math.PI/512;
				double radius=j*(1+Math.sin(angle));
				int x=(int)Math.round(radius*Math.cos(angle)*2);
				int y=(int)Math.round(radius*Math.sin(angle));
				g.fillOval(x0+x, y0+y, 2, 2);
			}
		}
		
	}
	public void actionPerformed(ActionEvent ev){
		repaint();
		
	}
}

实现效果图

程序运行播放音乐,5000ms一帧改变心形线的颜色,音乐文件可随意选取
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_37808895/article/details/88623208
今日推荐