1. Implementation principle
We will use mathematical equations to draw the shape of the love heart. There are many mathematical equations for love curves. Here we use a simple cardiac parametric equation:
- x = 16 * sin³θ
- y = 13 * cosθ - 5 * cos2θ - 2 * cos3θ - cos4θ
where θ is an angle parameter, with a range of between 0 and 2π.
2. Complete code implementation
import .*; import .*; public class HeartShape extends JFrame { public HeartShape() { setTitle("520 Love"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); add(new HeartPanel()); } public static void main(String[] args) { (() -> { new HeartShape().setVisible(true); }); } } class HeartPanel extends JPanel { @Override protected void paintComponent(Graphics g) { (g); Graphics2D g2d = (Graphics2D) g; // Set up anti-aliasing (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Set background color setBackground(); (); // Draw love int width = getWidth(); int height = getHeight(); // Zoom and pan parameters to center the love double scale = (width, height) / 25.0; int translateX = width / 2; int translateY = height / 2; // Draw a love curve int points = 1000; double[] xPoints = new double[points]; double[] yPoints = new double[points]; for (int i = 0; i < points; i++) { double t = 2 * * i / points; // Cardiac parameter equation double x = 16 * ((t), 3); double y = 13 * (t) - 5 * (2 * t) - 2 * (3 * t) - (4 * t); xPoints[i] = x * scale + translateX; yPoints[i] = -y * scale + translateY; // Invert the Y axis } // Fill in love int[] xIntPoints = new int[points]; int[] yIntPoints = new int[points]; for (int i = 0; i < points; i++) { xIntPoints[i] = (int) xPoints[i]; yIntPoints[i] = (int) yPoints[i]; } // Use polygon fill (xIntPoints, yIntPoints, points); // Add "520" text (); (new Font("Microsoft Yahe", , 48)); String text = "520"; FontMetrics fm = (); int textWidth = (text); int textHeight = (); (text, translateX - textWidth / 2, translateY + textHeight / 2); } }
3. Code analysis
kind: Inherited from JFrame, it is the main window of the program.
- Set the window title "520 Love"
- Set the window size to 800x600
- Add HeartPanel as content panel
kind: Inherited from JPanel, responsible for drawing love graphics.
-
paintComponent
Implement drawing logic in the method - Use Graphics2D to draw, turn on anti-aliasing
- Set black background and red heart colors
- Calculate the point coordinates of the heart shape using the heart parameter equation
- Scale and translate the coordinates so that love can be displayed in the center
- Filling love with polygons
- Add "520" white text in the center of love
4. Operation effect
After running the program, you will see a window with a black background, with a red love pattern in the center and a white "520" in the center of the love.
V. Expansion and improvement
- Animation effect: You can add animations to make your love beat
- Color changes: Can make love color gradually change or flash
- Custom text: You can modify the code and let the user enter custom text
- 3D effect: You can use Java 3D library to create 3D love
6. Summary
Through this tutorial, we learned how to draw a mathematical love graph using Java and add "520" text to it. This technique can be applied to variousGraphic interfaceIn programs, such as games, animations or romantic confession programs.
This is the article about drawing a 520 love pattern sample code using Java. For more related content on drawing love pattern in Java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!