Java implements Chinese Valentine's Day confession artifact

王林
Release: 2020-08-25 16:20:57
forward
3273 people have browsed it

Java implements Chinese Valentine's Day confession artifact

First we need to create a heart-shaped pattern, and then truncate the created string according to the position and length of the heart-shaped pattern and output it at the required position. Finally, we can A lot of love appears on the screen.

(Recommended tutorial: java introductory tutorial)

Implementation code:

package ddd;
 
import java.awt.*;
 
import javax.swing.JFrame;
 
public class Cardioid extends JFrame {
 
	//获取屏幕大小
	private static final int WIDTH = 500;
	private static final int HEIGHT = 500;   
 
	private static int WINDOW_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
	private static int WINDOW_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
	
	public Cardioid(){
		super("I love you");//设置窗口标题
		this.setBackground(Color.BLACK);
		this.setLocation((WINDOW_WIDTH-WIDTH)/2,(WINDOW_HEIGHT-HEIGHT)/2);//设置窗口位置
		this.setSize(WIDTH, HEIGHT);//设置窗口大小
		this.setLayout(getLayout());//设置窗口布局
		this.setVisible(true);//设置窗口可见
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);//设置窗口默认关闭方式
	}
	
	public void paint(Graphics g){
		double x,y,r;//横纵坐标以及半径
		Image image = this.createImage(WIDTH, HEIGHT);
		Graphics pic = image.getGraphics();
		for (int i = -2; i < 90; i++) {
			for (int j = -2; j < 90; j++) {
				r=Math.PI/45+Math.PI/45*i*(1-Math.sin(Math.PI/45*j))*18;
				x=r*Math.cos(Math.PI/45*j)*Math.sin(Math.PI/45*i)+WIDTH/2;
				y=-r*Math.sin(Math.PI/45*j)+HEIGHT/3;
				pic.setColor(Color.MAGENTA);
				pic.fillOval((int)x, (int)y, 2, 2);
			}
			g.drawImage(image,0,0,this);//生成图片
		}
	}
	
	
	public static void main(String[] args) {
		new Cardioid();
	}
}
Copy after login

(Recommended learning video: java course)

Achievement effect:

Java implements Chinese Valentines Day confession artifact

The above is the detailed content of Java implements Chinese Valentine's Day confession artifact. For more information, please follow other related articles on the PHP Chinese website!

source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!