Home  >  Article  >  Java  >  Share methods and examples of drawing hearts in Java code

Share methods and examples of drawing hearts in Java code

王林
王林Original
2024-02-20 12:24:06806browse

Share methods and examples of drawing hearts in Java code

Love drawing skills and examples sharing in Java code

Love, as a special graphic, is often used to express love and warm emotions. In Java programming, we can use some simple techniques to draw beautiful love patterns. This article will share some tips and examples of love drawing, hoping to be helpful to Java programmers.

First of all, we need to understand the basic shape and principle of love. A heart usually consists of two overlapping arcs with the same starting and ending points. We can achieve the heart effect by drawing two arcs of the same size and adjusting their position, rotation angle and size.

Next, let’s look at some specific code examples.

Example 1: Draw a simple heart

import java.awt.Color;
import java.awt.Graphics;
import import java.awt.Graphics2D;
import import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class HeartDrawingExample extends JPanel {
  
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    int width = getWidth();
    int height = getHeight();

    g2d.setColor(Color.RED);
    g2d.fillArc(width/2 - width/4, height/2 - height/4, width/2, height/2, 0, 180);
    g2d.fillArc(width/2 - width/4, height/2 - height/4, width/2, height/2, 180, 180);
    g2d.fillRect(width/2 - width/8, height/2, width/4, height/2);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("Heart Drawing Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new HeartDrawingExample());
    frame.setSize(400, 400);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

The above code demonstrates how to use Java's Swing library to create a simple heart drawing instance. First, we created a JPanel subclass HeartDrawingExample and overridden the paintComponent method to draw hearts. In the drawing method, we use the Graphics2D object to draw graphics and set anti-aliasing rendering parameters to achieve better drawing effects. Then, we set the color to red and used the fillArc method to draw two arcs and a rectangle, ultimately forming a heart effect. Finally, we created a JFrame object, added HeartDrawingExample to it, and set the size and position of the window, finally displaying the heart pattern we drew.

Example 2: Draw a dynamic heartbeat heart

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import import java.awt.Graphics2D;
import import java.awt.RenderingHints;
import import java.awt.event.ActionEvent;
import import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class DynamicHeartbeatHeartExample extends JPanel implements ActionListener {
  
  private int width = 200;
  private int height = 200;
  private int scale = 10;
  private int angle = 0;

  public DynamicHeartbeatHeartExample() {
    Timer timer = new Timer(100, this);
    timer.start();
  }
  
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    int size = scale * (width/2);

    g2d.setColor(Color.RED);
    g2d.fillArc(getWidth()/2 - size/2, getHeight()/2 - size/2, size, size, angle, 180 - angle);
    g2d.fillArc(getWidth()/2 - size/2, getHeight()/2 - size/2, size, size, 180 + angle, 180 - angle);
    g2d.fillRect(getWidth()/2 - size/4, getHeight()/2, size/2, size);
  }

  @Override
  public Dimension getPreferredSize() {
    return new Dimension(width*scale, height*scale);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (scale > 2) {
      scale--;
    } else {
      scale = 10;
    }
    angle = (angle + 10) % 360;
    repaint();
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("Dynamic Heartbeat Heart Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new DynamicHeartbeatHeartExample());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

The above code demonstrates how to create a dynamic heartbeat heart drawing instance. We used a Timer to regularly update the size and angle of the heart, creating a dynamic beating effect. In the drawing method, we draw the heart according to the current size and angle, forming a beating heartbeat rhythm. Finally, we define a main method to create and display this dynamic heart window.

Through the above two examples, we can see that using the Java programming language can flexibly draw beautiful love patterns. By adjusting the parameters and methods in the code, you can also achieve more styles of love effects. I hope these examples can provide some help and ideas for Java programmers when drawing love patterns. May the warmth and emotion of love be conveyed and expressed in the world of programming.

The above is the detailed content of Share methods and examples of drawing hearts in Java code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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 admin@php.cn