JAVA课程设计——飞机大战(个人)
基于Java SWing的游戏界面设计
团队博客
战机选择
private void backwardActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//图片地址数组下标切换
index++;
if (index > 3) {
index = 0;
}
//图片切换
new Thread(new Runnable() {
@Override
public void run() {
fighterPlane.setIcon(new ImageIcon(planesPath[index]));
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}).start();
}
private void forwardActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//图片地址数组下标切换
index--;
if (index < 0) {
index = 3;
}
//图片切换
new Thread(new Runnable() {
@Override
public void run() {
fighterPlane.setIcon(new ImageIcon(planesPath[index]));
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}).start();
}
游戏界面背景
//标签设置图片
txtUsername = new javax.swing.JLabel(usernameImage);
txtPassword = new javax.swing.JLabel(passwordImage);
userName = new javax.swing.JTextField();
password = new javax.swing.JPasswordField();
registered = new javax.swing.JButton(registeredImage);
login = new javax.swing.JButton(loginImage);
this.icon = Toolkit.getDefaultToolkit().getImage("images/背景/icon.png");
this.background = new ImageIcon("images/背景/img_bg_logo.jpg");
this.logo = new ImageIcon("images/背景/LOGO.png");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("飞机大战");
setPreferredSize(new java.awt.Dimension(512, 768));
//底层和顶层背景设置
JLabel label = new JLabel(this.background);
label.setBounds(0, 0, frameWidth, frameHeight);
JLabel label1 = new JLabel(this.logo);
label1.setBounds((frameWidth - logo.getIconWidth()) / 2 - 10, 100, logo.getIconWidth(), logo.getIconHeight());
imagePanel = (JPanel) this.getContentPane();
imagePanel.setOpaque(false);
imagePanel.setLayout(new FlowLayout());
getLayeredPane().setLayout(null);
getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));
getLayeredPane().add(label1, Integer.valueOf(Integer.MAX_VALUE));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setIconImage(this.icon);
registered.setBorderPainted(false);
registered.setContentAreaFilled(false);
registered.setMargin(new java.awt.Insets(0, 0, 0, 0));
//实现动态按钮效果
registered.setRolloverIcon(new ImageIcon("images/背景/注册2.png"));
registered.setPressedIcon(new ImageIcon("images/背景/注册3.png"));
registered.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
registeredActionPerformed(evt);
}
});
login.setBorderPainted(false);
login.setContentAreaFilled(false);
login.setMargin(new java.awt.Insets(0, 0, 0, 0));
//实现动态按钮效果
login.setRolloverIcon(new ImageIcon("images/背景/登录提交2.png"));
login.setPressedIcon(new ImageIcon("images/背景/登录提交3.png"));
login.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
loginActionPerformed(evt);
}
});
战机控制
public class FrameMouseMotionListener implements MouseMotionListener {
private BaseFrame baseFrame;
public BaseFrame getBaseFrame() {
return baseFrame;
}
public void setBaseFrame(BaseFrame baseFrame) {
this.baseFrame = baseFrame;
}
@Override
public void mouseDragged(MouseEvent e) {
this.baseFrame.getBackGroundPanel().getPlayer().setX(e.getX() - this.baseFrame.getBackGroundPanel().getPlayer().getWidth() / 2);
this.baseFrame.getBackGroundPanel().getPlayer().setY(e.getY() - this.baseFrame.getBackGroundPanel().getPlayer().getHeight() / 2);
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
游戏界面动态背景
super.paintComponent(g);
// 绘制背景图片
g.drawImage(this.bgImage, 0, top - bgImage.getHeight(this), this.bgImage.getWidth(this),
this.bgImage.getHeight(this), null);
g.drawImage(this.bgImage, 0, top, this.bgImage.getWidth(this), this.bgImage.getHeight(this), null);
time++;
if (time == 100000) {
time = 0;
}
if (time % 10 == 0) {
top++;
if (top >= this.bgImage.getHeight(this)) {
top = 0;
}
}
//绘制云朵
this.cloud.drawSelf(g);
public void drawSelf(Graphics g) {
g.drawImage(this.images[0], x + 200, top - heights[0], width, heights[0], null);
g.drawImage(this.images[1], x - 100, top - 700, width, heights[1], null);
g.drawImage(this.images[0], x + 150, top - 800, width, heights[0], null);
g.drawImage(this.images[1], x - 150, top + heights[1], width, heights[1], null);
g.drawImage(this.images[0], x + 150, top - 600, width, heights[0], null);
g.drawImage(this.images[1], x - 150, top, width, heights[1], null);
if (this.backGroundPanel.getTime() % 10 == 0) {
top++;
if (top >= 1500) {
top = -1000;
}
}
}
多线程实现动态效果
public class DrawableThread extends Thread {
private BackGroundPanel backGroundPanel;
public DrawableThread(BackGroundPanel backGroundPanel) {
this.backGroundPanel = backGroundPanel;
}
@Override
public void run() {
while (true) {
//重绘
this.backGroundPanel.repaint();
try {
sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
游戏暂停,游戏继续
public class FrameMouseListener implements MouseListener {
private BaseFrame baseFrame;
public FrameMouseListener(BaseFrame baseFrame) {
this.baseFrame = baseFrame;
}
public BaseFrame getBaseFrame() {
return baseFrame;
}
public void setBaseFrame(BaseFrame baseFrame) {
this.baseFrame = baseFrame;
}
@Override
public void mouseClicked(MouseEvent e) {
this.baseFrame.getBackGroundPanel().drawableThread = new DrawableThread(this.baseFrame.getBackGroundPanel());
this.baseFrame.getBackGroundPanel().drawableThread.start();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
this.baseFrame.getBackGroundPanel().drawableThread.threadStop();
}
}
用户信息的读写操作
该程序的所有信息都是通过excel文档进行存储,能够方便地写入和读取信息,不需要担心像txt文档存储格式的问题,所用到的是阿里巴巴开发的EasyExcel工具,它能够实现对excel文档快速,方便地进行一系列操作。
读
{
EasyExcel.read(fileName, User.class, new AnalysisEventListener<User>() {
@Override
public void invoke(User user, AnalysisContext analysisContext) {
users.add(user);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}).sheet(0).doRead();
}
写
EasyExcel.write(fileName, User.class).sheet(0).doWrite(users);