Java画心形代码

psb (1)

效果图↑ 代码如下:

import java.awt.Graphics;

import javax.swing.JFrame;


public class Heart extends JFrame{
	public static void main(String[] args) {
		new Heart();
	}
	public Heart(){
		this.setLayout(null);
		this.setBounds(0, 0, 400, 400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	public void paint(Graphics g) {
		super.paint(g);
		draw(g);
	}
	public void draw(Graphics g)	{
	    float m,n,x,y;
	    float i;
	    int dx,dy;
	    for (i=0;i<=200;i=(float) (i+0.01)){
	        m=i;
	        n=(float) (-50*(((sin(i)*sqrt(abs(cos(i))))/(sin(i)+1.4))-2*sin(i)+2));
	        x=n*cos(m)+200;
	        y=n*sin(m)+150;
	        dx=(int) x;
	        dy=(int) y;
	        g.drawLine(dx,dy,dx,dy);
	    }
	}
	private float sin(float num){
		return (float) Math.sin(num);
	}
	private float cos(float num){
		return (float) Math.cos(num);
	}
	private float abs(float num){
		return Math.abs(num);
	}
	private float sqrt(float num){
		return (float) Math.sqrt(num);
	}
}