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(); } }
(Recommended learning video: java course)
Achievement effect:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
