JAVA课程设计——飞机大战(个人)

JAVA课程设计——飞机大战(个人)

基于Java SWing的游戏界面设计

团队博客

战机选择

JAVA课程设计——飞机大战(个人)

 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();


    }

游戏界面背景

JAVA课程设计——飞机大战(个人)

        //标签设置图片
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) {

}
}

游戏界面动态背景

JAVA课程设计——飞机大战(个人)

        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();
}
}
}
}

游戏暂停,游戏继续

JAVA课程设计——飞机大战(个人)

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);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/114438.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号