Java 代码绘制爱心图形 - 简单易懂的实现
以下是使用 Java 语言绘制爱心图形的代码:
import java.awt.*;
import javax.swing.*;
public class HeartShape extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(3));
g2d.setColor(Color.RED);
int x = getWidth() / 2;
int y = getHeight() / 2;
int radius = 50;
int[] xPoints = {x - radius, x, x + radius, x};
int[] yPoints = {y, y - radius, y, y + radius};
g2d.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
g2d.fillPolygon(xPoints, yPoints, 4);
}
public static void main(String[] args) {
JFrame frame = new JFrame('Heart Shape');
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.add(new HeartShape());
frame.setVisible(true);
}
}
运行后会出现一个红色的爱心图形,如下所示:

原文地址: https://www.cveoy.top/t/topic/mLD8 著作权归作者所有。请勿转载和采集!