Java绘制笛卡尔心形线

Java 实现笛卡尔心形线的绘画

利用心形线的参数方程描点即可,Java中没有点,可以用半径很小的圆代替,使用Thread.sleep方法,即可做到每画一次点休眠一次。代码也是比较简单,如下:

import java.awt.*;
import javax.swing.*;


class HeartLine extends JFrame
{
    
    
	public static void main(String[] args) 
	{
    
    
		HeartLine frame=new HeartLine("HeartLine");
		
		SwingUtilities.invokeLater(()->{
    
    
			frame.init();
			try
			{
    
    
				for(int i=1;i<=2000;i++)
				{
    
    
					frame.drawHeartLine(2000,i,50);
					Thread.sleep(3);
				}

			}
			catch (InterruptedException e)
			{
    
    
			}
			frame.drawHeartLine(2000,0,50);
			});
	}
	private JFrame frame;
	private int width;
	private int height;
	private Graphics graphics;
	private final double PI=Math.PI;
	public HeartLine(String title)
	{
    
     
		super(title);
		JLabel label=new JLabel("Descartes' heart line");
		getContentPane().add(label);
		setVisible(true);
		setSize(440,440);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBackground(Color.LIGHT_GRAY);
		setLayout(new FlowLayout());

	}
	void init()
	{
    
    
		width=getSize().width;
		height=getSize().height;
		graphics=this.getGraphics();
		Image img=new ImageIcon("winSmile.jpg").getImage();
		setIconImage(img);
	}
	void drawHeartLine(int n,int k,double r)
	{
    
    
	  if(k==n) return;
	   double x0=2*r*(Math.sin(k*2*PI/n)-0.5*Math.sin(k*4*PI/n))+220;
	   double y0=-2*r*(Math.cos(k*2*PI/n)-0.5*Math.cos(k*4*PI/n))+200;

       drawOval(x0,y0,1,1);
	
	}
	void drawOval(double x0,double y0,int width,int height)
	{
    
    
		graphics.drawOval((int)x0,(int)y0,width,height);
	}
}

拿上它给心仪的女生表白吧

猜你喜欢

转载自blog.csdn.net/m0_47202518/article/details/108109506
今日推荐