服务器之家

服务器之家 > 正文

Java基于swing实现的弹球游戏代码

时间:2019-12-04 12:39     来源/作者:shichen2014

本文实例讲述了Java基于swing实现的弹球游戏代码。分享给大家供大家参考。

主要功能代码如下:

 

复制代码代码如下:


package Game;

 

import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;

/**
 * 游戏界面类
 * 
 * @author Administrator
 * 
 */
public class Game extends javax.swing.JFrame {
        private static final long serialVersionUID = 1L;
        private static Game UI;
        private Graphics g;
        private Listener ml;
        public javax.swing.JLabel text_field;
        public javax.swing.JProgressBar pBar;
        private String command;
        public ArrayList<MyThread> list = new ArrayList<MyThread>();
        private javax.swing.JToggleButton button;

        public static void main(String args[]) {
                UI = new Game();
                UI.initUI();
        }

        /**
         * 初始化窗体的方法
         */
        public void initUI() {
                // 设置标题
                this.setTitle("是高手就坚持10s");
                // 设置大小
                this.setSize(610, 635);
                // 设置关闭是退出程序
                this.setDefaultCloseOperation(3);
                // 设置打开窗体时在屏幕中间显示
                this.setLocationRelativeTo(null);
                // 设置窗体的流式布局管理器
                this.setLayout(new java.awt.FlowLayout());
                // 设置窗体背景颜色
                this.getContentPane().setBackground(java.awt.Color.black);
                // 创建文本标签对象
                text_field = new javax.swing.JLabel();
                javax.swing.JLabel lable = new javax.swing.JLabel("时  间  :");
                // 设置文本标签前景颜色
                lable.setForeground(java.awt.Color.red);
                text_field.setForeground(java.awt.Color.red);

                // 创建进度条对象
                pBar = new javax.swing.JProgressBar(0, 330);
                // 创建按钮对象
                button = new javax.swing.JToggleButton();
                button.setMargin(new Insets(0, 0, 0, 0));
                button.setIcon(new javax.swing.ImageIcon("images/Pause.gif"));
                button.setActionCommand("暂停");
                // 通过匿名内部类来创建动作监听器
                java.awt.event.ActionListener button_listener = new java.awt.event.ActionListener() {

                        public void actionPerformed(ActionEvent e) {
                                String com = e.getActionCommand();

                                if (com.equals("暂停")) {
                                        button.setMargin(new Insets(0, 0, 0, 0));
                                        button.setIcon(new javax.swing.ImageIcon("images/start.gif"));
                                        button.setActionCommand("继续");
                                        for (int i = 0; i < list.size(); i++) {
                                                list.get(i).PauseThread();

                                        }

                                }
                                if (com.equals("继续")) {

                                        button.setMargin(new Insets(0, 0, 0, 0));
                                        button.setIcon(new javax.swing.ImageIcon("images/Pause.gif"));
                                        button.setActionCommand("暂停");

                                        for (int i = 0; i < list.size(); i++) {
                                                list.get(i).ContinueThread();
                                        }

                                }

                        }

                };
                button.addActionListener(button_listener);

                this.add(button);
                this.add(lable);
                this.add(pBar);
                this.add(text_field);
                // 的到菜单条
                JMenuBar bar = creatMenuBar();
                // 为窗体设置菜单条
                this.setJMenuBar(bar);
                // 设置窗体的可见性
                this.setVisible(true);
        }

        /**
         * 创建菜单条的方法
         * 
         * @return
         */
        public JMenuBar creatMenuBar() {
                // 创建菜单条对象
                JMenuBar bar = new JMenuBar();
                // 创建菜单对象
                JMenu menu_menu = new JMenu("菜单");
                JMenu difficulty_menu = new JMenu("难度");
                JMenu help_menu = new JMenu("帮助");
                // 创建菜单选项对象
                JMenuItem star_item = new JMenuItem("开始");
                JMenuItem exit_item = new JMenuItem("退出");
                JMenuItem help_item = new JMenuItem("游戏说明");
                JMenuItem about_item = new JMenuItem("关于");
                // 创建单选选项
                JRadioButtonMenuItem easy_item = new JRadioButtonMenuItem(
                                "简单");
                JRadioButtonMenuItem middle_item = new JRadioButtonMenuItem(
                                "中等");
                JRadioButtonMenuItem hard_item = new JRadioButtonMenuItem(
                                "困难");
                // 创建一个按钮组
                ButtonGroup group = new ButtonGroup();
                // 将单选按钮添加到按钮组中
                group.add(easy_item);
                group.add(middle_item);
                group.add(hard_item);
                // 将单选按钮添加到菜单中
                difficulty_menu.add(easy_item);
                difficulty_menu.add(middle_item);
                difficulty_menu.add(hard_item);
                // 通过匿名内部类来创建动作监听器
                ActionListener listener = new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                command = e.getActionCommand();
                                // 如果选择开始,则创建线程对象
                                if (command.equals("开始") && list.size() == 0) {
                                        creatBall(20, 1);

                                }
                                // 如果选择退出按钮,则退出程序
                                if (command.equals("退出")) {
                                        System.exit(0);
                                }

                                // 如果选择简单按钮
                                if (command.equals("简单") && list.size() == 0) {

                                        creatBall(20, 1);
                                }

                                // 如果选择中等按钮
                                if (command.equals("中等") && list.size() == 0) {
                                        creatBall(50, 2);

                                }
                                if (command.equals("困难") && list.size() == 0) {
                                        creatBall(40, 2);
                                }
                                if (command.equals("游戏说明")) {
                                        JOptionPane.showMessageDialog(null,
                                                        "移动鼠标,用挡板接球,如果接不住,就算输了……\n游戏可以选择难度,包括简单、中等、困难");
                                }
                                if (command.equals("关于")) {
                                        JOptionPane
     .showMessageDialog(null,
      "这是一个用Java编写的小游戏……\n制作人:沈冠军\n时间:2010年8月\n版权所有,翻版必究!");
                                }
                        }
                };
                // 添加动作监听器
                star_item.addActionListener(listener);
                exit_item.addActionListener(listener);
                easy_item.addActionListener(listener);
                middle_item.addActionListener(listener);
                hard_item.addActionListener(listener);
                help_item.addActionListener(listener);
                about_item.addActionListener(listener);

                // 将菜单选项对象添加到菜单对象上
                menu_menu.add(star_item);
                menu_menu.add(exit_item);
                help_menu.add(help_item);
                help_menu.add(about_item);
                // 将菜单对象添加到菜单条上
                bar.add(menu_menu);
                bar.add(difficulty_menu);
                bar.add(help_menu);
                // 返回菜单条对象
                return bar;
        }

        /**
         * 创建线程对象的方法
         * 
         * @param speed
         *            :速度
         * 
         */
        public void creatBall(int speed, int num) {
                Random ran = new Random();

                if (ml == null) {
                        g = UI.getGraphics();
                        ml = new Listener(g);
                        UI.addMouseListener(ml);
                        UI.addMouseMotionListener(ml);

                }
                for (int i = 0; i < num; i++) {
                        int x = ran.nextInt(600) + 10;
                        int y = ran.nextInt(300) + 100;
                        MyThread th = new MyThread(g, ml, UI, x, y, speed);
                        list.add(th);
                        th.start();
                }

        }

        /**
         * 得到命令的方法
         */
        public String getCommand() {
                return command;
        }

}

 

 

复制代码代码如下:


package Game;

 

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;    
/** 
 * 鼠标监听器的类 
 *  
 * @author Administrator 
 *  
 */  
public class Listener extends MouseAdapter {  
    private Graphics g;  
    private int x = 5, y = 620;  
    private int width = 100, height = 10;  
  
    public Listener(Graphics g) {  
        this.g = g;  
    }  
  
    public void mouseMoved(MouseEvent e) {  
        // 设置画布对象颜色  
        synchronized (this.g) {  
            g.setColor(Color.black);  
            g.fillRect(x, y, width, height);  
            x = e.getX();  
            g.setColor(java.awt.Color.green);  
            g.fillRect(x, y, width, height);  
        }  
    }  
  
    /** 
     * 得到x的方法 
     *  
     * @return:x 
     */  
    public int getX() {  
        return x;  
    }  
  
}  

 

运行效果如下图所示:

Java基于swing实现的弹球游戏代码

希望本文所述对大家的Java程序设计有所帮助。

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部