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

📄 pentominosapplet.java

📁 a java code of moving square game
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }        boardcanvas.repaint();    }        public void run() {        while (true) {          while (getMessage() != goMessage) {  // wait for game setup             if (getMessage() == randomMessage) {                setUpRandomBoard();                boardcanvas.stopCreating();  // tell board to ignore mouse clicks                setMessage(goMessage);                doDelay(1000);  // give user a chance to change selection             }             else if (getMessage() == clearMessage) {                clearBoard();                boardcanvas.startCreating();  // tell board to let user set up puzzle                comment.setText("Click four squares:");                setMessage(0);                doDelay(-1);  // wait forever (for control message from action() or boardcanvas.mouseDown())             }          }          // begin next game          synchronized(this) {             goButton.disable();             pauseButton.enable();             stepButton.disable();             comment.setText("Solving...");             message = 0;          }          for (int i=1; i<=12; i++)              used[i] = false;          numused = 0;          int square = 11;  // reprsents the upper left corner of the board          while (board[square] == -1)             square++;  // move past any filled squares, since Play(square) assumes the square is empty          aborted = false;          play(square);          if (!aborted) {             pauseButton.disable();             stepButton.disable();             goButton.disable();             comment.setText("No (further) solution found.");             doDelay(-1);          }       }    }        synchronized void doDelay(int milliseconds) {            // wait for specified time, or until a control message is sent using setMessage()            // is generated.  For an indefinite wait, milliseconds should be < 0       if (milliseconds < 0) {          try {             wait();          }          catch (InterruptedException e) {          }       }       else {          try {             wait(milliseconds);          }          catch (InterruptedException e) {          }       }    }        synchronized void setMessage(int message) {  // send control message to game thread       this.message = message;       if (message > 0)          notify();    }        synchronized int getMessage() {       return this.message;    }            synchronized public boolean action(Event evt, Object arg) {       if (evt.target == clearButton) {          pauseButton.disable();          goButton.disable();          stepButton.disable();          setMessage(clearMessage);          notify();          return true;       }       else if (evt.target == randomButton) {          pauseButton.disable();          goButton.disable();          stepButton.disable();          setMessage(randomMessage);          notify();          return true;       }       else if (evt.target == goButton) {          pauseButton.enable();          goButton.disable();          stepButton.disable();          setMessage(goMessage);          notify();          return true;       }       else if (evt.target == pauseButton) {          pauseButton.disable();          goButton.enable();          stepButton.enable();          setMessage(pauseMessage);          notify();          return true;       }       else if (evt.target == stepButton) {          setMessage(stepMessage);          notify();          return true;       }       else if (evt.target == speedChoice) {          int item = speedChoice.getSelectedIndex();          if (speed != item) {             speed = item;             delay = speedDelay[speed];          }          return true;       }       else          return super.action(evt,arg);    }        }   // end of class PentominosSolverclass PentominosBoardCanvas extends Panel {  // implement the visible 8-by-8 playing board    int[] board;  // The board data array, passed into the constructor and                  //    used throughout this class                      PentominosApplet owner;  // used only to set notification back to applet that                             // the user has finished setting up a board (used                             // in mouseDown()).    Color pieceColor[];  // Array of colors to be used in displaying pieces                         //   pieceColor[0] is the color of an empty square,                         //   pieceColor[1] is the color for piece number 1, etc.                             boolean creatingBoard;  // this is true when user is setting up a board    int clickCt;  // number of squares that have been blackened by the user; when                  // this value reaches four, it is time to start solving the puzzle        PentominosBoardCanvas(int[] theBoard, PentominosApplet theOwner) { // Constructor       board = theBoard;       owner = theOwner;       MakeColors();  // create and fill in pieceColor[] array    }    void MakeColors() {        pieceColor = new Color[13];        pieceColor[0] = Color.white;        pieceColor[1] = new Color(200,0,0);        pieceColor[2] = new Color(150,150,255);        pieceColor[3] = new Color(0,200,200);        pieceColor[4] = new Color(255,150,255);        pieceColor[5] = new Color(0,200,0);        pieceColor[6] = new Color(150,255,255);        pieceColor[7] = new Color(200,200,0);        pieceColor[8] = new Color(0,0,200);        pieceColor[9] = new Color(255,150,150);        pieceColor[10] = new Color(200,0,200);        pieceColor[11] = new Color(255,255,150);        pieceColor[12] = new Color(150,255,150);    }        // Note:  The following methods are synchronized because when the    // board is resized, the main applet thread might try to call paint    // while the puzzle-solving thread is trying to place squares on the    // Board.  (I'm not sure this should really happen because the puzzle    // thread is supposed to be running at a lower priority, but before    // I added the synchronization, the screen would get corrupted when    // I resized the board while the puzzle was in the process of being    // solved.)    public synchronized void paint(Graphics g) {  // redraw the whole board        int w = size().width;        w = 8 * (w/8) + 1;        int h = size().height;        h = 8 * (h/8) + 1;        Color oldColor = g.getColor();        g.setColor(Color.black);        for (int i = 0; i <= 8; i++) {           int x = i * ((w - 1) / 8);           g.drawLine(x,0,x,h-1);        }        for (int i = 0; i <= 8; i++) {           int y = i * ((h - 1) / 8);           g.drawLine(0,y,w-1,y);        }        for (int i = 1; i <= 8; i++) {           int y = (i-1) * ((h-1) / 8);           for (int j = 1; j <= 8; j++) {               int x = (j-1) * ((w-1) / 8);               if (board[10*i + j] == -1)                   g.setColor(Color.black);               else                   g.setColor(pieceColor[board[10*i + j]]);               g.fillRect(x+1, y+1, (w-1) / 8 - 1, (h-1) / 8 - 1);           }        }        g.setColor(oldColor);    }    synchronized void putSquare(Graphics g, int name, int square) {  // "name" gives the piece number       int w = size().width;       w = 8 * (w/8) + 1;       int h = size().height;       h = 8 * (h/8) + 1;       int x = ((square % 10)-1) * ((w-1)) / 8;       int y = ((square / 10)-1) * ((h-1)) / 8;       g.setColor(pieceColor[name]);       g.fillRect(x+1, y+1, (w-1)/8 - 1, (h-1)/8 - 1);       g.setColor(Color.black);       board[square] = name;    }    synchronized void playPiece(int[] pieceData, int startSquare) {       Graphics g = getGraphics();       putSquare(g,pieceData[0],startSquare);       for (int i = 1; i < 5; i++)          putSquare(g,pieceData[0],startSquare+pieceData[i]);       g.dispose();    }        synchronized void clearSquare(Graphics g, int square) {        int w = size().width;        w = 8 * (w/8) + 1;        int h = size().height;        h = 8 * (h/8) + 1;       int x = ((square % 10)-1) * ((w-1)) / 8;       int y = ((square / 10)-1) * ((h-1)) / 8;       g.setColor(pieceColor[0]);       g.fillRect(x+1, y+1, (w-1)/8 - 1, (h-1)/8 - 1);       g.setColor(Color.black);       board[square] = 0;    }        synchronized void removePiece(int[] pieceData, int startSquare) {       Graphics g = getGraphics();       clearSquare(g,startSquare);       for (int i = 1; i < 5; i++)          clearSquare(g,startSquare+pieceData[i]);       g.dispose();    }    synchronized void blackenSquare(int square) {        int w = size().width;        w = 8 * (w/8) + 1;        int h = size().height;        h = 8 * (h/8) + 1;       int x = ((square % 10)-1) * ((w-1)) / 8;       int y = ((square / 10)-1) * ((h-1)) / 8;       Graphics g = getGraphics();       g.fillRect(x, y, (w-1)/8, (h-1)/8);       g.dispose();       board[square] = -1;    }        public void startCreating() {       clickCt = 0;       creatingBoard = true;    }        public void stopCreating() {       creatingBoard = false;    }        public boolean mouseDown(Event evt, int x, int y) {       if (creatingBoard) {          int w = size().width / 8;          int h = size().height / 8;          int col = x / w;          int row = y / h;          if (col < 0 || col > 7 || row < 0 || row > 7)             return true;          int square = 10*(row+1) + (col+1);          if (board[square] == 0) {             blackenSquare(square);             clickCt++;          }          else {             Graphics g = getGraphics();             clearSquare(g,square);             g.dispose();             clickCt--;          }          if (clickCt == 4) {             creatingBoard = false;             owner.setMessage(PentominosApplet.goMessage);          }       }       return true;    }        public Dimension minimumSize() {        return new Dimension(160,160);    }    public Dimension preferredSize() {        return minimumSize();    }}

⌨️ 快捷键说明

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