用java写一个爱心代码,不断的闪烁。
时间: 2023-08-31 12:40:31 浏览: 65
### 回答1:
您好,这是一份 Java 代码来实现爱心图形的不断闪烁效果:
```
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Heart extends JPanel {
private int x = -50;
private int y = 50;
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
int[] xPoints = {x + 50, x + 15, x + 85, x + 50};
int[] yPoints = {y + 50, y + 100, y + 100, y + 50};
g.fillPolygon(xPoints, yPoints, 4);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
x++;
if (x > getWidth()) {
x = -50;
}
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Heart());
frame.setVisible(true);
}
}
```
代码的主要思路是继承 JPanel,重写 paint() 方法,在里面绘制爱心图形,然后使用 sleep() 方法让图形间隔一段时间重新绘制。
希望这份代码对您有所帮助。
### 回答2:
可以使用Java的图形界面库Swing来实现一个爱心不断闪烁的效果。
首先,创建一个继承自JFrame的类,作为界面容器。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.GeneralPath;
public class HeartFrame extends JFrame {
private final int WIDTH = 600; // 界面宽度
private final int HEIGHT = 400; // 界面高度
private Timer timer; // 定时器控制爱心的闪烁
private boolean isRed = true; // 控制当前显示的颜色
private int alpha = 255; // 控制当前显示的透明度
public HeartFrame() {
initUI();
initTimer();
}
private void initUI() {
setTitle("闪烁的爱心");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 使用匿名类重写JFrame的paint方法来绘制爱心
getContentPane().add(new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 设置爱心的颜色和透明度
if (isRed) {
g2d.setColor(new Color(255, 0, 0, alpha));
} else {
g2d.setColor(new Color(255, 255, 255, alpha));
}
// 绘制爱心
GeneralPath path = new GeneralPath();
path.moveTo(300, 200);
path.curveTo(400, 100, 500, 200, 300, 400);
path.curveTo(100, 200, 200, 100, 300, 200);
g2d.fill(path);
}
});
setVisible(true);
}
private void initTimer() {
timer = new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 控制爱心的闪烁效果
if (isRed) {
alpha -= 51;
if (alpha <= 0) {
isRed = false;
}
} else {
alpha += 51;
if (alpha >= 255) {
isRed = true;
}
}
// 重新绘制界面
repaint();
}
});
timer.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new HeartFrame();
}
});
}
}
```
以上代码中,通过重写JFrame的paint方法来绘制爱心形状,通过设置爱心的颜色和透明度来控制爱心的闪烁效果。使用定时器Timer来不断改变爱心的透明度,从而实现闪烁效果。运行该代码,就能看到一个不断闪烁的爱心界面。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)