⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boxworld.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * $Id: BoxWorld.java,v 1.2 2004/02/28 09:20:53 epr Exp $
 * Box world
 * Author: Levente S醤tha
 * 2001 October-November
 * 2002 July
 *   - source code reorganized
 *   - run to mouse feature added
 */
package org.jnode.test.gui;

import java.awt.CheckboxMenuItem;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.Point;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Hashtable;
import java.util.Stack;
import java.util.Vector;

public class BoxWorld extends Panel implements WindowListener, KeyListener,
        MouseListener, MouseMotionListener, ActionListener, ItemListener {

    //***************** WORLD DATA ****************************
    private int[][] vecs = {
            { 81, -766241563, -831219482, -696967897, -863854201, -896130840,
                    -661281463, -828168022, -793433846, -758894261, 1073742156,
                    10, -829120249, 20, -795533048, 1073742122, 19, -862609146,
                    1073742123, 2382},
            //1
            { 105, -834465628, -900427516, -765290203, -630843034, -697000793,
                    -863854169, -729505560, -862543512, -861494039, -860444342,
                    -659182261, -826068565, -725307124, 50, -865856283,
                    -796648249, -728455927, -794580631, -660231894, -759943957,
                    12, -831252250, 11, -661249657, 1582},
            //2
            { 97, -867003259, -766241563, -731768507, -965338778, -831316857,
                    -964289273, -829315704, -962189975, -693917558, -860705653,
                    -759943957, -2147111605, 58, -832366426, -764110586,
                    -930767545, -763092792, -795728536, -760993463, 1073742154,
                    20, -729571161, 1073742121, 19, -895277911, 1073742026,
                    1350},
            //3
            { 73, -732654331, -798779035, -697066138, -695983896, -795630200,
                    -660362871, -825873173, -725307124, -2147077780, 26,
                    -730523354, -694901432, -2147112630, 20, -728456921,
                    -2147144406, 19, -759943926, -2147111605, 2110},
            //4
            { 89, -767291164, -732719804, -832170651, -831316602, -663413497,
                    -796712537, -862543544, -861493911, -827118166, -726356725,
                    -625595029, 50, -731637467, -697000602, -662331096,
                    -661314263, -727406326, 1073742218, 12, -693885658, 11,
                    -828136216, 2094},
            //5
            { 169, -801927997, -701166301, -935227005, -666693468, -766241691,
                    -966388411, -664561498, -898523738, -964256441, -628776792,
                    -694999959, -928929367, -626677462, -927487542, -793531189,
                    -725470741, -925388436, -825019219, -724257523, -623495827,
                    -2146978355, 130, -767291164, -933916348, -699198299,
                    -798812026, -698017498, -797762425, -863886969, -729505560,
                    -795728504, -895081143, -794580790, -894031542, -659182261,
                    -859655765, -758894356, -590958196, 20, -796615449,
                    -2147179287, 19, -796549915, -2147177303, 3678},
            //6
            { 105, -766241563, -665479867, -832366171, -864642746, -863593145,
                    -762108504, -928602807, -727504726, -927553142, -893242997,
                    -792481588, -691719892, 1073742220, 58, -698050298,
                    -764142361, -796517017, -829021880, -827972247, -726292182,
                    1073742187, 20, -695951128, -2147210999, 19, -827118421,
                    -2147177205, 3126},
            //7
            { 89, -732654331, -631892635, -798779194, -864903770, -629826233,
                    -595418681, -594107223, -828168022, -727406326, -692769366,
                    -2147046005, 42, -698017498, -663478906, -662331096,
                    -661314295, -2147079830, 20, -729538265, -2147113687, 19,
                    -830267161, -2147244855, 3398},
            //8
            { 129, -799828795, -699067099, -598305403, -731604794, -596435514,
                    -729505592, -862510744, -594139895, -794613590, -559470198,
                    -892883797, -891834004, -791431987, -690670291, -589908595,
                    1073742317, 82, -698083098, -831088250, -697000697,
                    -796484217, -627858040, -860444438, -692802293, -859361909,
                    -758894356, -590991028, 20, -628907705, -625660693, 19,
                    -694868695, -693819094, 3430},
            //9
            { 73, -699067099, -598305403, -831055578, -596337401, -828956440,
                    -827906647, -727406326, -692769366, -2147046005, 34,
                    -664430266, -762929754, -693885528, 1073742218, 20,
                    -663380665, -2147179192, 19, -728358552, -2147080855, 2118},
            //10
            { 105, -865953658, -698017562, -597255802, -831382425, -964223673,
                    -763092760, -963174072, -928929335, -593057494, -860705653,
                    -759943957, -659182261, 1073742251, 58, -697099097,
                    -930701945, -628743992, -695032695, -861754967, -727439126,
                    1073742218, 20, -862609240, 1073742217, 19, -762043159,
                    1073742153, 2902},
            //11
            { 73, -732654331, -865725083, -664528666, -629793593, -862543672,
                    -627825431, -626644726, -726356725, -2147078805, 42,
                    -831186650, -830104313, -761913080, -727406199, 1073742186,
                    20, -728424153, 1073742185, 19, -695951033, 1073742153,
                    2870},
            //12
            { 81, -800878396, -700116700, -665643644, -664495930, -863626041,
                    -661445240, -660297526, -793531189, -692769493, 1073742219,
                    34, -830104345, -695918328, -828005143, -2147112694, 28,
                    -764142299, -795565753, -2147146455, 27, -732687131,
                    -798779035, -2147116730, 2646},
            //20
            { 113, -868052860, -767291164, -666529468, -967764572, -999910267,
                    -765224762, -998860474, -997810809, -830267288, -662363864,
                    -962190231, -861755254, -760993558, -660231862, 66,
                    -833416027, -699067099, -933127803, -664430426, -764175257,
                    -929718136, -829217623, -694868695, 20, -697131770,
                    -2147213144, 19, -797762299, -2147211961, 1086},
            //30
            { 225, -937326526, -635238238, -534279742, -835515325, -734753533,
                    -633991837, -968814077, -900622844, -699067163, -531196539,
                    -698017690, -563701370, -864969210, -562783001, -763256760,
                    -862902808, -995646039, -794580790, -660264662, -559470166,
                    -927880117, -726455125, -591187509, -589974323, -656033586,
                    -856213074, -755745553, -2147107505, 170, -600667005,
                    -868085277, -767291164, -666529468, -933785148, -766241595,
                    -932735547, -798779226, -797795034, -596239033, -830267256,
                    -662331064, -963173976, -829250423, -728455927, -594139799,
                    -895342486, -759943957, -625595029, -624578228,
                    -2147043955, 36, -865693532, -697164346, -796745305,
                    -2147077782, 35, -758894356, -757844755, -790382259,
                    -2147141330, 798},
            //40

            //ISABELLA :X
            { 137, -833416027, -732654331, -631892635, -531130939, -530278234,
                    -529228633, -763191160, -929554104, -661281463, -928471543,
                    -893867862, -859655637, -758894356, -523783732, -724257236,
                    -623495827, 1073742285, 138, -798779194, -664430266,
                    -563668570, -797729593, -663380665, -562618969, -695983928,
                    -595156600, -795630104, -594107095, -794613271, -693819094,
                    -593057398, -860345878, -759943957, -659182261, -524833365,
                    20, -828168984, 1073742283, 19, -658132660, 1073742252,
                    1358},};

    //************** DIMENSIONS ********************
    private static final int CELL_SIZE = 20;

    private static final int X_SIZE = 20;

    private static final int Y_SIZE = 20;

    private static final int BW_WIDTH = X_SIZE * CELL_SIZE;

    private static final int BW_HEIGHT = Y_SIZE * CELL_SIZE;

    //*************** MODE CODES *******************
    private static final int START_MODE = 0;

    private static final int PLAY_MODE = 1;

    private static final int END_MODE = 2;

    private static final int HELP_MODE = 3;

    //*********** MOVE CODES ****************
    private static final int LEFT_MOVE = 1;

    private static final int RIGHT_MOVE = LEFT_MOVE + 1;

    private static final int UP_MOVE = RIGHT_MOVE + 1;

    private static final int DOWN_MOVE = UP_MOVE + 1;

    private static final int PUSH_BASE = DOWN_MOVE;

    private static final int LEFT_PUSH = PUSH_BASE + 1;

    private static final int RIGHT_PUSH = LEFT_PUSH + 1;

    private static final int UP_PUSH = RIGHT_PUSH + 1;

    private static final int DOWN_PUSH = UP_PUSH + 1;

    private static final int DRAW_BASE = DOWN_PUSH;

    private static final int LEFT_DRAW = DRAW_BASE + 1;

    private static final int RIGHT_DRAW = LEFT_DRAW + 1;

    private static final int UP_DRAW = RIGHT_DRAW + 1;

    private static final int DOWN_DRAW = UP_DRAW + 1;

    //**************** STATE CODE *****************
    private static final int NULL_ST = 0;

    private static final int WALL_ST = 1;

    private static final int BACK_ST = 2;

    private static final int GOAL_ST = 3;

    private static final int BOX_ST = 4;

    private static final int FIT_ST = 5;

    private static final int MAN_ST = 6;

    private static final int MAN_GOAL_ST = 7;

    private int gameMode = PLAY_MODE;

    //current world ID
    private int worldId = 0;

    //current world data
    private int[] vec = vecs[ worldId];

    //current world
    private int[][] state = new int[ X_SIZE][ Y_SIZE];

    //goals list
    private int[] goals;

    //suspends the current movement at an obstacle
    private boolean repeat = true;

    //end state
    //private boolean endState = false;

    //controlls the speed of animation
    private int SLEEP = 50;

    //current and previous position of the man
    private int x, y, xo, yo;

    //popup menu
    private PopupMenu menu;

    private CheckboxMenuItem beep;

    //**************** APPLET METHODS *************************
    // init applet
    public void init() {

        setBackground(Color.lightGray);

        x = xo = 10;
        y = yo = 10;
        //loading the first wold in the world table
        loadWorld();

        //registering listeners
        addKeyListener(this);
        addMouseListener(this);
        addMouseMotionListener(this);

        requestFocus();

        //building the popup menu
        menu = new PopupMenu();
        MenuItem start = new MenuItem("Start");
        start.addActionListener(this);
        start.setActionCommand("start");
        menu.add(start);
        MenuItem next = new MenuItem("Next");
        next.addActionListener(this);
        next.setActionCommand("next");
        menu.add(next);
        MenuItem previous = new MenuItem("Previous");
        previous.addActionListener(this);
        previous.setActionCommand("previous");
        menu.add(previous);

        menu.addSeparator();

        MenuItem undo = new MenuItem("Undo");
        undo.addActionListener(this);
        undo.setActionCommand("undo");
        menu.add(undo);
        MenuItem redo = new MenuItem("Redo");
        redo.addActionListener(this);
        redo.setActionCommand("redo");
        menu.add(redo);

        menu.addSeparator();

        beep = new CheckboxMenuItem("Beep", beepOn);
        beep.addItemListener(this);
        beep.setActionCommand("beep");
        menu.add(beep);

        //add(menu);
    }

    //start applet
    public void start() {
        requestFocus();
    }

    private void loadWorld() {
        int sh, s, n, xx, yy, ind = 0;
        //int len = vec.length;
        Vector gv = new Vector();
        for (int i = 0; i < 4; i++) {
            s = 0x00000007 & vec[ ind];
            n = vec[ ind] >> 3;
            ind++;
            for (int j = 0; j < n; j++, ind++) {
                sh = 0x00000003 & (vec[ ind] >> 30);
                //int m = 0xFFFFFFFF;
                //int ssh = 0;
                int v = vec[ ind];
                for (int k = 0; k < sh; k++) {
                    xx = 0x0000001F & v;
                    v = v >> 5;
                    yy = 0x0000001F & v;
                    v = v >> 5;
                    if (xx < X_SIZE && yy < Y_SIZE) {
                        if (s == GOAL_ST) {
                            gv.addElement(new Integer(xx | (yy << 16)));
                        }
                        if (s == BOX_ST && state[ xx][ yy] == GOAL_ST
                                || s == GOAL_ST && state[ xx][ yy] == BOX_ST) {
                            state[ xx][ yy] = FIT_ST;
                        } else if (s == MAN_ST && state[ xx][ yy] == GOAL_ST
                                || s == GOAL_ST && state[ xx][ yy] == MAN_ST) {
                            state[ xx][ yy] = MAN_GOAL_ST;
                        } else {
                            state[ xx][ yy] = s;
                        }
                    }
                }
            }
        }
        int goal_cnt = gv.size();
        goals = new int[ goal_cnt];
        for (int i = 0; i < goal_cnt; i++) {
            goals[ i] = ((Integer) gv.elementAt(i)).intValue();
        }
        int v = vec[ ind] >> 3;
        xx = 0x0000001F & v;
        v = v >> 5;
        yy = 0x0000001F & v;
        state[ xx][ yy] = MAN_ST;
        x = xx;
        y = yy;
        //endState = false;
    }

    //************************************************************************************
    //******************************* EVENT HANDLING
    // *************************************
    //************************************************************************************

    //----------------------------------KEY
    // EVENTS----------------------------------------
    public void keyPressed(KeyEvent e) {
        if (gameMode == START_MODE) {
            controlWorld(KeyEvent.VK_S);
        } else if (gameMode == END_MODE) {
            controlWorld(KeyEvent.VK_N);
        } else {
            int key = e.getKeyCode();
            int code = key == KeyEvent.VK_LEFT ? LEFT_MOVE
                    : key == KeyEvent.VK_RIGHT ? RIGHT_MOVE
                            : key == KeyEvent.VK_UP ? UP_MOVE
                                    : key == KeyEvent.VK_DOWN ? DOWN_MOVE : -1;
            if (code > 0) {
                if (gameMode == PLAY_MODE) {
                    changeState(code);
                }
            } else {
                controlWorld(key);
            }
        }
    }

    //----------------------------------MOUSE
    // EVENTS---------------------------------------
    public void mousePressed(MouseEvent e) {
        if (gameMode != PLAY_MODE) {
            changeMode();
        } else {
            if (e.isPopupTrigger()) {
                menu.show(this, e.getX(), e.getY());
                return;
            }

            if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == 0) { return; }

            moveTo(e.getX(), e.getY());
        }
    }

    public void mouseClicked(MouseEvent e) { //changeMode() ;
    }

    public void mouseDragged(MouseEvent e) {
        /*
         * if(e.isPopupTrigger()){ menu.show(this,e.getX(),e.getY()); return; }
         * 
         * if((e.getModifiers()& e.BUTTON1_MASK) == 0)
         */
        moveTo2(e.getX(), e.getY());
    }

    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
            menu.show(this, e.getX(), e.getY());
        }
    }

    //----------------------------------ACTION
    // EVENTS-------------------------------------
    //FROM MENU
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if ("start".equals(command)) {
            startWorld();
        } else if ("next".equals(command)) {
            nextWorld();
        } else if ("previous".equals(command)) {
            previousWorld();
        } else if ("undo".equals(command)) {
            undo();
        } else if ("redo".equals(command)) {
            redo();
        }
    }

    //----------------------------------ITEM
    // EVENTS----------------------------------------
    //CHECKBOX MENUITEM
    public void itemStateChanged(ItemEvent e) {
        toggleBeep();
    }

    //*************************************************************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -