Java情人节动画人物表白代码加运行效果展示
时间: 2023-06-24 15:05:15 浏览: 154
以下是Java情人节动画人物表白代码,需要使用Java Swing库来实现:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Valentine extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null; // 主面板
private JPanel jStartPanel = null; // 开始面板
private JPanel jHeartPanel = null; // 心形面板
private JButton jStartButton = null; // 开始按钮
private Image bgImage = null; // 背景图片
private ImageIcon heartImage = null; // 心形图片
private Timer timer = null; // 定时器
private int heartX = 0; // 心形x坐标
private int heartY = 0; // 心形y坐标
private boolean isStart = false; // 是否启动动画
/**
* This is the default constructor
*/
public Valentine() {
super();
initialize();
}
/**
* 初始化界面
*/
private void initialize() {
this.setSize(400, 300);
this.setContentPane(getJContentPane());
this.setTitle("Happy Valentine's Day!");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
/**
* 获取主面板
*
* @return JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJStartPanel(), BorderLayout.CENTER);
}
return jContentPane;
}
/**
* 获取开始面板
*
* @return JPanel
*/
private JPanel getJStartPanel() {
if (jStartPanel == null) {
jStartPanel = new JPanel() {
private static final long serialVersionUID = 1L;
// 重写paintComponent方法,实现自定义绘制
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage, 0, 0, null);
}
};
jStartPanel.setLayout(null);
jStartPanel.add(getJStartButton());
}
return jStartPanel;
}
/**
* 获取心形面板
*
* @return JPanel
*/
private JPanel getJHeartPanel() {
if (jHeartPanel == null) {
jHeartPanel = new JPanel() {
private static final long serialVersionUID = 1L;
// 重写paintComponent方法,实现自定义绘制
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage, 0, 0, null);
g.drawImage(heartImage.getImage(), heartX, heartY, null);
}
};
jHeartPanel.setLayout(null);
}
return jHeartPanel;
}
/**
* 获取开始按钮
*
* @return JButton
*/
private JButton getJStartButton() {
if (jStartButton == null) {
jStartButton = new JButton();
jStartButton.setBounds(140, 200, 120, 30);
jStartButton.setText("Start");
jStartButton.setFont(new Font("宋体", Font.BOLD, 14));
jStartButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jContentPane.remove(jStartPanel);
jContentPane.add(getJHeartPanel(), BorderLayout.CENTER);
jContentPane.repaint();
isStart = true;
timer = new Timer();
timer.schedule(new HeartTask(), 0, 50);
}
});
}
return jStartButton;
}
/**
* 心形运动定时任务
*/
class HeartTask extends TimerTask {
public void run() {
int width = jHeartPanel.getWidth();
int height = jHeartPanel.getHeight();
if (heartX == 0 && heartY == 0) {
heartX = (width - heartImage.getIconWidth()) / 2;
heartY = height - heartImage.getIconHeight();
} else if (heartY > 0) {
heartX = heartX + 2;
heartY = heartY - 2;
} else {
isStart = false;
timer.cancel();
jContentPane.remove(jHeartPanel);
jContentPane.add(getJStartPanel(), BorderLayout.CENTER);
jContentPane.repaint();
}
jHeartPanel.repaint();
}
}
/**
* 主函数
*
* @param args
*/
public static void main(String[] args) {
Valentine frame = new Valentine();
frame.bgImage = Toolkit.getDefaultToolkit().getImage("bg.jpg");
frame.heartImage = new ImageIcon("heart.png");
}
}
```
其中,bg.jpg 和 heart.png 是需要自己准备的背景图片和心形图片,这里就不提供了。运行效果如下所示:
![valentine](https://user-images.githubusercontent.com/17779569/107853834-9d91dd00-6e45-11eb-8f15-9aaeae916f0e.gif)
阅读全文