使用java编写动态爱心程序
时间: 2023-02-24 22:56:03 浏览: 958
可以使用Java编写动态爱心程序,下面是一个简单的代码示例:
```
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LoveHeart extends JPanel {
public void paint(Graphics g) {
super.paint(g);
int x1 = 150, y1 = 150;
int x2 = 50, y2 = 50;
g.setColor(Color.RED);
for (int i = 0; i <= 90; i++) {
x1 = x1 + 2;
y1 = (int) (y1 - (Math.sin(Math.toRadians(i)) * 2));
x2 = x2 + 2;
y2 = (int) (y2 + (Math.sin(Math.toRadians(i)) * 2));
g.drawLine(x1, y1, x2, y2);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new LoveHeart());
frame.setVisible(true);
}
}
```
这个代码使用了Java的图形绘制API,绘制了一个动态的爱心。
阅读全文