用java编写一个红色的心型的图案
以下是用Java编写的红色心型图案代码:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HeartShape extends JPanel {
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
int[] x = { 250, 200, 200, 250, 300, 300 };
int[] y = { 150, 200, 225, 250, 225, 200 };
g.fillPolygon(x, y, 6);
g.fillArc(175, 100, 150, 150, 0, -180);
g.fillArc(275, 100, 150, 150, 0, -180);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Heart Shape");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
HeartShape panel = new HeartShape();
frame.add(panel);
frame.setVisible(true);
}
}
运行程序后,将显示一个红色的心型图案。其中,我们使用了 fillPolygon() 方法来绘制心形的上半部分,使用了 fillArc() 方法来绘制心形的下半部分。通过调整 x 和 y 数组中的值,可以改变心形的大小和形状
原文地址: https://www.cveoy.top/t/topic/envS 著作权归作者所有。请勿转载和采集!