java图形用户界面心形线图形设计
时间: 2023-10-11 14:20:59 浏览: 92
以下是使用Java Swing实现心形线图形的代码示例:
```java
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HeartShape extends JPanel {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int centerX = width / 2;
int centerY = height / 2;
int radius = Math.min(centerX, centerY) - 10;
g.translate(centerX, centerY);
g.scale(1, -1);
int x, y;
double angle = 0;
double angleStep = Math.PI / 50;
g.drawLine(-radius, 0, radius, 0);
g.drawLine(0, -radius, 0, radius);
for (int i = 0; i < 200; i++) {
angle += angleStep;
x = (int) Math.round(radius * Math.sin(angle) * Math.sin(angle) * Math.sin(angle));
y = (int) Math.round(-radius * (13 * Math.cos(angle) - 5 * Math.cos(2 * angle) - 2 * Math.cos(3 * angle) - Math.cos(4 * angle)));
g.drawLine(x, y, x, y);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Heart Shape");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.add(new HeartShape());
frame.setVisible(true);
}
}
```
该程序创建了一个JFrame窗口,并在其中绘制了一个心形线图形。运行程序后,将显示一个窗口,其中包含绘制的图形。
阅读全文