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

📄 mazefog2.java

📁 Tim duong di trong me cung mot thuat toan kha hay
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    mainFrame = new Frame("Maze Fog Game - by: Keith Fenske");    mainFrame.addWindowListener(new MazeFog2Window());    mainFrame.setLayout(new BorderLayout(5, 5));    mainFrame.setLocation(new Point(50, 50)); // top-left corner of app window    mainFrame.setSize(700, 500);  // initial size of application window    appletPanel = new MazeFog2(); // create instance of target applet    mainFrame.add(appletPanel, BorderLayout.CENTER); // give applet full frame    mainFrame.validate();         // do the application window layout    mainFrame.setVisible(true);   // show the application window    /* Initialize the applet after the layout and sizes of the main frame have    been determined, since the applet computes how many rows and columns will    fit into the allocated size of <boardCanvas>. */    appletPanel.init();           // initialize applet with correct sizes    appletPanel.boardCanvas.repaint(); // force game board to appear (redraw)  } // end of main() method// ------------------------------------------------------------------------- ///*  actionPerformed() method  This method is called when the user clicks on a control button at the top of  the applet window (not on the game board).*/  public void actionPerformed(ActionEvent event)  {    Object source = event.getSource(); // where the event came from    if (source == biggerButton)      doBiggerButton();           // "Bigger" button for bigger board squares    else if (source == newgameButton)      doNewgameButton();          // "New Game" button to start a new game    else if (source == showmeButton)      doShowmeButton();           // "Show Me" button to see the solution    else if (source == smallerButton)      doSmallerButton();          // "Smaller" button for smaller board squares    else    {      System.out.println(        "error in actionPerformed(): ActionEvent not recognized: " + event);    }  } // end of actionPerformed() method/*  boardMouseMoved() method  This method is called by our dummy Canvas class (MazeFog2Board) to process  mouse movement across the game board, in the context of the MazeFog2 class.  While there is an active game, we try to copy the mouse's position to the  user's position on the game board.  boardMouseMoved() is a more complicated version of keyPressed() because the  arrow keys only move in whole board positions (rows and columns).  Since both  input methods are available to the user, we must leave the user's position in  a form that is valid for keyPressed().  For example, if the mouse moves more  than halfway to the next board square, then we must set the row and column to  the new square, with negative pixel offsets back towards the old square.*/  public void boardMouseMoved(MouseEvent event)  {    int deltaX;                   // mouse horizontal (x) distance    int deltaY;                   // mouse vertical (y) distance    Graphics gr;                  // graphics context for <boardCanvas>    int halfway;                  // halfway distance to next square in pixels    int hz;                       // temporary number of horizontal pixels    int mouseNewX;                // new x coordinate of mouse    int mouseNewY;                // new y coordinate of mouse    int userNewX;                 // new x coordinate of user's position    int userNewY;                 // new y coordinate of user's position    int userOldX;                 // old x coordinate of user's position    int userOldY;                 // old y coordinate of user's position    int vt;                       // temporary number of vertical pixels    if (gameState == GameACTIVE)  // ignore mouse unless there is an active game    {      halfway = boardGridSize / 2; // any offset bigger moves to next square      mouseNewX = event.getX();   // get mouse x coordinate (horizontal)      mouseNewY = event.getY();   // get mouse y coordinate (vertical)      /* Allocate a graphics context for <boardCanvas> and remember the exact      coordinates of the user's current position, so that we can shift the      display pixels later.  (Erasing and redrawing the user's board symbol      every time the mouse moves one pixel would cause a lot of flicker.) */      gr = boardCanvas.getGraphics(); // get graphics context      userOldX = (userCol * boardGridSize) + boardBorderSize + boardLineWidth        + canvasBorder + userColOffset;      userOldY = (userRow * boardGridSize) + boardBorderSize + boardLineWidth        + canvasBorder + userRowOffset;      /* In which direction did the mouse move?  Displacements may be negative,      zero, or positive.  Assume that the mouse is pointing at the center of      where the user would like to move.  Note that the coordinates (userOldX,      userOldY) represent the top-left corner of the bounding box for the      user's board symbol (the circle), not the center of the board symbol. */      deltaX = mouseNewX - userOldX - (boardSymbolSize / 2); // horizontal      deltaY = mouseNewY - userOldY - (boardSymbolSize / 2); // vertical      /* We are given two endpoints of a straight line: the user's current      position and the desired new position pointed to by the mouse.  Depending      upon the speed of the mouse and how busy the system is, the mouse      movement may be very large and complex but the information available to      us is only linear.  Curved paths are lost if we aren't called often      enough.  As a result, we only have to be accurate for short distances and      straight lines.  Iteratively process the larger of the horizontal and      vertical movements.  We allow the mouse movement to be quite sloppy. */      while ((deltaX != 0) || (deltaY != 0))      {        if (Math.abs(deltaX) > Math.abs(deltaY)) // more horizontal?        {          /* The mouse movement is larger in the horizontal (x) directon. */          if (deltaX < 0)          {            /* We are trying to move left. */            if (userColOffset > 0) // do we have a right offset pending?            {              hz = Math.min(Math.abs(deltaX), Math.abs(userColOffset));                                  // select smaller segment (absolute value)              deltaX += hz;       // reduce horizontal distance to move              userColOffset -= hz; // reduce right offset pending            }            else if ((boardLeft[userRow][userCol] != LineEMPTY) // wall?            || (boardDistance[userRow][userCol - 1] <= 0)) // stop at exit            {              deltaX = 0;         // can't go left, so cancel left movement            }            else            {              /* We can go left.  Limit ourself to a segment that is less than              or equal to the size of a board square.  Remember, this              processing is iterative: the remaining distance will be done              later. */              hz = Math.abs(userColOffset + deltaX); // total desired              hz = Math.min(hz, boardGridSize); // smaller segment              deltaX += hz - Math.abs(userColOffset); // reduce remaining              if (hz < halfway)   // still on same board square?                userColOffset = - hz; // yes              else              {                /* We have gone past the halfway point, so switch to the next                board square. */                userCol --;       // go one column to the left                userColOffset = boardGridSize - hz; // now a right offset              }            }          }          else if (deltaX > 0)          {            /* We are trying to move right. */            if (userColOffset < 0) // do we have a left offset pending?            {              hz = Math.min(Math.abs(deltaX), Math.abs(userColOffset));                                  // select smaller segment (absolute value)              deltaX -= hz;       // reduce horizontal distance to move              userColOffset += hz; // reduce left offset pending            }            else if ((boardLeft[userRow][userCol + 1] != LineEMPTY) // wall?            || (boardDistance[userRow][userCol + 1] <= 0)) // stop at exit            {              deltaX = 0;         // can't go right, so cancel right movement            }            else            {              /* We can go right. */              hz = Math.abs(userColOffset + deltaX); // total desired              hz = Math.min(hz, boardGridSize); // smaller segment              deltaX -= hz - Math.abs(userColOffset); // reduce remaining              if (hz < halfway)   // still on same board square?                userColOffset = hz; // yes              else              {                /* We have gone past the halfway point, so switch to the next                board square. */                userCol ++;       // go one column to the right                userColOffset = - (boardGridSize - hz); // now a left offset              }            }          }          else          {            /* Amazing.  <deltaX> is zero, but somehow it's still greater than            the absolute value of <deltaY>, which is not zero! */            System.out.println("error in boardMouseMoved(): <deltaX> is zero but greater than absolute of <deltaY>");          }        }        else        {          /* The mouse movement is larger in the vertical (y) direction. */          if (deltaY < 0)          {            /* We are trying to move up. */            if (userRowOffset > 0) // do we have a down offset pending?            {              vt = Math.min(Math.abs(deltaY), Math.abs(userRowOffset));                                  // select smaller segment (absolute value)              deltaY += vt;       // reduce vertical distance to move              userRowOffset -= vt; // reduce down offset pending            }            else if ((boardTop[userRow][userCol] != LineEMPTY) // wall?            || (boardDistance[userRow - 1][userCol] <= 0)) // stop at exit            {              deltaY = 0;         // can't go up, so cancel up movement            }            else            {              /* We can go up. */              vt = Math.abs(userRowOffset + deltaY); // total desired              vt = Math.min(vt, boardGridSize); // smaller segment              deltaY += vt - Math.abs(userRowOffset); // reduce remaining              if (vt < halfway)   // still on same board square?                userRowOffset = - vt; // yes              else              {                /* We have gone past the halfway point, so switch to the next                board square. */                userRow --;       // go one column up                userRowOffset = boardGridSize - vt; // now a down offset              }            }          }          else if (deltaY > 0)          {            /* We are trying to move down. */            if (userRowOffset < 0) // do we have an up offset pending?            {              vt = Math.min(Math.abs(deltaY), Math.abs(userRowOffset));                                  // select smaller segment (absolute value)              deltaY -= vt;       // reduce vertical distance to move              userRowOffset += vt; // reduce up offset pending            }            else if ((boardTop[userRow + 1][userCol] != LineEMPTY) // wall?            || (boardDistance[userRow + 1][userCol] <= 0)) // stop at exit            {              deltaY = 0;         // can't go down, so cancel down movement            }            else            {              /* We can go down. */              vt = Math.abs(userRowOffset + deltaY); // total desired              vt = Math.min(vt, boardGridSize); // smaller segment              deltaY -= vt - Math.abs(userRowOffset); // reduce remaining              if (vt < halfway)   // still on same board square?                userRowOffset = vt; // yes              else              {                /* We have gone past the halfway point, so switch to the next                board square. */                userRow ++;       // go one column down                userRowOffset = - (boardGridSize - vt); // now an up offset              }            }          }          else          {            /* We can only get here if the while loop fails and both <deltaX>            and <deltaY> are zero. */            System.out.println("error in boardMouseMoved(): both <deltaX> and <deltaY> are zero");          }        }      }      /* Cancel any pixel offsets that would bump us into a wall.  The most      obvious cases are being beside a wall (line).  We must also allow for the      corners at intersections. */      if (userColOffset < 0)      {        /* There is a pixel offset trying to move left.  There may also be a        vertical offset (not yet tested). */        if (boardLeft[userRow][userCol] != LineEMPTY)        {          /* There is a wall to the left of us.  No left movement allowed. */          userColOffset = 0;      // cancel horizontal pixel offset        }        else if (userRowOffset < 0)        {          /* Left is empty.  There is a pixel offset trying to move up. */          if (boardTop[userRow][userCol] != LineEMPTY) // can we go up?            userRowOffset = 0;    // cancel vertical pixel offset          else if ((Math.abs(userColOffset) > boardBorderSize)          || (Math.abs(userRowOffset) > boardBorderSize))          {            /* We round the corners a bit, but once the pixel offsets get too

⌨️ 快捷键说明

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