欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-29 19:58:40
本文实例讲述了java基于swing实现的弹球游戏代码。分享给大家供大家参考。 主要功能代码如下: 复制代码 代码如下: package game; import...

本文实例讲述了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程序设计有所帮助。