Today I have nothing to do, so I will teach you a little case to coax girls. We need to create a heart pattern, truncate the created string according to the position and length of the heart pattern and output it at the desired position, and finally present it on the screen with full love. Without further ado, just go to the source code to see the effect~
package ddd; import java.awt.*; import javax.swing.JFrame; public class Cardioid extends JFrame { //Get screen size 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");//Set the window title this.setBackground(Color.BLACK); this.setLocation((WINDOW_WIDTH-WIDTH)/2,(WINDOW_HEIGHT-HEIGHT)/2);//Set the window position this.setSize(WIDTH, HEIGHT);//Set the window size this.setLayout(getLayout());//Set the window layout this.setVisible(true);//Set the window to see this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);//Set the window default closing method} public void paint(Graphics g){ double x,y,r;//Horizontal and vertical coordinates and radius 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);//Generate image} } public static void main(String[] args) { new Cardioid(); } }The effect is as follows:
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.