コード例 #1
0
 /**
  * If statements to see if any labels are hoovered over. If so, the image is changed. If left, the
  * image is changed.
  */
 public void mouseMoved(MouseEvent e) {
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != NewgameText) {
     NewgameText.setIcon(new ImageIcon("img/New game text.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == NewgameText) {
     NewgameText.setIcon(new ImageIcon("img/newgameholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != InstructionsSmallText) {
     InstructionsSmallText.setIcon(new ImageIcon("img/instructionstext.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == InstructionsSmallText) {
     InstructionsSmallText.setIcon(new ImageIcon("img/Instructionsholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != HighscoreText) {
     HighscoreText.setIcon(new ImageIcon("img/highscoretext.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == HighscoreText) {
     HighscoreText.setIcon(new ImageIcon("img/highscoreholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != ExitGameText) {
     ExitGameText.setIcon(new ImageIcon("img/exit game.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == ExitGameText) {
     ExitGameText.setIcon(new ImageIcon("img/exit game holdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != back) {
     back.setIcon(new ImageIcon("img/back.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == back) {
     back.setIcon(new ImageIcon("img/backholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != PlayerVsComp) {
     PlayerVsComp.setIcon(new ImageIcon("img/playvscomptext.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == PlayerVsComp) {
     PlayerVsComp.setIcon(new ImageIcon("img/playvscompholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != PlayerVsPlayer) {
     PlayerVsPlayer.setIcon(new ImageIcon("img/playervsplayertext.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == PlayerVsPlayer) {
     PlayerVsPlayer.setIcon(new ImageIcon("img/playervsplayertextholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != TwoPlayerVsTwoPlayer) {
     TwoPlayerVsTwoPlayer.setIcon(new ImageIcon("img/2playervs2playertext.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == TwoPlayerVsTwoPlayer) {
     TwoPlayerVsTwoPlayer.setIcon(new ImageIcon("img/2playervs2playertextholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != Easy) {
     Easy.setIcon(new ImageIcon("img/easytext.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == Easy) {
     Easy.setIcon(new ImageIcon("img/easytextholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != Medium) {
     Medium.setIcon(new ImageIcon("img/mediumtext.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == Medium) {
     Medium.setIcon(new ImageIcon("img/mediumtextholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != Hard) {
     Hard.setIcon(new ImageIcon("img/hardtext.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == Hard) {
     Hard.setIcon(new ImageIcon("img/hardtextholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != Online) {
     Online.setIcon(new ImageIcon("img/onlinetext.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == Online) {
     Online.setIcon(new ImageIcon("img/onlinetextholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != Lan) {
     Lan.setIcon(new ImageIcon("img/lantext.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == Lan) {
     Lan.setIcon(new ImageIcon("img/lantextholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != Login2) {
     Login2.setIcon(new ImageIcon("img/login.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == Login2) {
     Login2.setIcon(new ImageIcon("img/loginholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != Create) {
     Create.setIcon(new ImageIcon("img/create.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == Create) {
     Create.setIcon(new ImageIcon("img/createholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != Join) {
     Join.setIcon(new ImageIcon("img/join.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == Join) {
     Join.setIcon(new ImageIcon("img/joinholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != Create2) {
     Create2.setIcon(new ImageIcon("img/create.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == Create2) {
     Create2.setIcon(new ImageIcon("img/createholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != Start) {
     Start.setIcon(new ImageIcon("img/starttext.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == Start) {
     Start.setIcon(new ImageIcon("img/starttextholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != Cancel) {
     Cancel.setIcon(new ImageIcon("img/cancel.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == Cancel) {
     Cancel.setIcon(new ImageIcon("img/cancelholdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != returnToMain) {
     returnToMain.setIcon(new ImageIcon("img/return to main menu.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == returnToMain) {
     returnToMain.setIcon(new ImageIcon("img/return to main menu holdover.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) != mainMenu) {
     mainMenu.setIcon(new ImageIcon("img/mainmenu.png"));
   }
   if (layeredPane.findComponentAt(e.getX(), e.getY()) == mainMenu) {
     mainMenu.setIcon(new ImageIcon("img/mainmenuholdover.png"));
   }
   displayCurrentScreen();
 }
コード例 #2
0
  // If statements checking if any, and which label was clicked. Takes the
  // player another screen.
  public void mouseReleased(MouseEvent e) {
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == NewgameText) {
      currentScreen = 2;
      if (dBg) System.out.println("New game screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != NewgameText) {
      NewgameText.setIcon(new ImageIcon("img/New game text.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == InstructionsSmallText) {
      currentScreen = 3;
      if (dBg) System.out.println("Instructions screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != InstructionsSmallText) {
      InstructionsSmallText.setIcon(new ImageIcon("img/instructionstext.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == HighscoreText) {
      currentScreen = 4;
      if (dBg) System.out.println("Highscore screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != HighscoreText) {
      HighscoreText.setIcon(new ImageIcon("img/highscoretext.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == ExitGameText) {
      currentScreen = 5;
      if (dBg) System.out.println("Game exits after 5seconds delay, and credit screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != ExitGameText) {
      ExitGameText.setIcon(new ImageIcon("img/exit game.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == PlayerVsComp) {
      currentScreen = 6;
      if (dBg) System.out.println("Difficulty screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != PlayerVsComp) {
      PlayerVsComp.setIcon(new ImageIcon("img/playvscomptext.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == PlayerVsPlayer) {
      currentScreen = 7;
      if (dBg) System.out.println("Network Mode screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != PlayerVsPlayer) {
      PlayerVsPlayer.setIcon(new ImageIcon("img/playervsplayertext.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == TwoPlayerVsTwoPlayer) {
      currentScreen = 7;
      if (dBg) System.out.println("Network Mode screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != TwoPlayerVsTwoPlayer) {
      TwoPlayerVsTwoPlayer.setIcon(new ImageIcon("img/2playervs2playertext.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == Easy) {
      if (dBg) System.out.println("game screen displayed");
      currentScreen = 11;
      localGame = new LocalGame(this, 0);
      board1Label.setGame(localGame);
      board2Label.setGame(localGame);
      board1Label.setClickable(false);
      board2Label.setClickable(true);
    }

    if (layeredPane.findComponentAt(e.getX(), e.getY()) != Easy) {
      Easy.setIcon(new ImageIcon("img/easytext.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == Medium) {
      currentScreen = 11;
      localGame = new LocalGame(this, 1);
      board1Label.setGame(localGame);
      board2Label.setGame(localGame);
      board1Label.setClickable(false);
      board2Label.setClickable(true);
      if (dBg) System.out.println("game screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != Medium) {
      Medium.setIcon(new ImageIcon("img/mediumtext.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == Hard) {
      localGame = new LocalGame(this, 2);
      board1Label.setGame(localGame);
      board2Label.setGame(localGame);
      board1Label.setClickable(false);
      board2Label.setClickable(true);
      currentScreen = 11;
      if (dBg) System.out.println("game screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != Hard) {
      Hard.setIcon(new ImageIcon("img/hardtext.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == Online) {
      currentScreen = 8;
      // connect to the database
      loginCreate.setText("connecting...");
      try {
        database = new Database("localhost", "battleship", "root", "rtpassword", "");
        loginCreate.setText("Connected. Input your data.");

      } catch (Exception ex) {
        System.out.println("Unable to connect.");
      }
      if (dBg) System.out.println("Login screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != Online) {
      Online.setIcon(new ImageIcon("img/onlinetext.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == Lan) {
      currentScreen = 8;
      if (dBg) System.out.println("Login screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != Lan) {
      Lan.setIcon(new ImageIcon("img/lantext.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == Login2) {
      char[] passwordSequence = password.getPassword();
      String userNameString = username.getText();
      System.out.println("\n" + userNameString);
      for (int i = 0; i < passwordSequence.length; i++) System.out.print(passwordSequence[i]);
      String check = database.checkUser(username.getText(), new String(password.getPassword()));
      if (check == "OK") {
        currentScreen = 9;
        if (dBg) System.out.println("Server screen displayed");
      } else {
        loginCreate.setText(check);
      }
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != Login2) {
      Login2.setIcon(new ImageIcon("img/login.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == Create) {
      char[] newPasswordSequence = newPassword.getPassword();
      char[] verPassSequence = verifyPassword.getPassword();
      String newUserNameString = newUsername.getText();

      currentScreen = 9;
      if (dBg) System.out.println("Server screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != Create) {
      Create.setIcon(new ImageIcon("img/create.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == Join) {
      String ipString = ip.getText();
      currentScreen = 10;
      if (dBg) System.out.println("Lobby screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != Join) {
      Join.setIcon(new ImageIcon("img/join.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == Create2) {
      String serverNameString = serverName.getText();
      currentScreen = 10;
      if (dBg) System.out.println("Lobby screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != Create2) {
      Create2.setIcon(new ImageIcon("img/create.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == Start) {
      currentScreen = 11;
      if (dBg) System.out.println("Game screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != Start) {
      Start.setIcon(new ImageIcon("img/starttext.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == Cancel) {
      currentScreen = 9;
      if (dBg) System.out.println("Server screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != Cancel) {
      Cancel.setIcon(new ImageIcon("img/cancel.png"));
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) == returnToMain) {
      emptyArrays();
      currentScreen = 1;
      if (dBg) System.out.println("start screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != returnToMain) {
      returnToMain.setIcon(new ImageIcon("img/return to main menu.png"));
    }

    if (layeredPane.findComponentAt(e.getX(), e.getY()) == mainMenu) {
      currentScreen = 1;
      if (dBg) System.out.println("start screen displayed");
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != mainMenu) {
      mainMenu.setIcon(new ImageIcon("img/mainmenu.png"));
    }

    if (layeredPane.findComponentAt(e.getX(), e.getY()) == back) {
      switch (currentScreen) {
        case 2:
          currentScreen = 1;
          if (dBg) System.out.println("start screen displayed");
          break;
        case 3:
          currentScreen = 1;
          if (dBg) System.out.println("start screen displayed");
          break;
        case 4:
          currentScreen = 1;
          if (dBg) System.out.println("start screen displayed");
          break;
        case 6:
          currentScreen = 2;
          if (dBg) System.out.println("New Game screen displayed");
          break;
        case 7:
          currentScreen = 2;
          if (dBg) System.out.println("start screen displayed");
          break;
        case 8:
          currentScreen = 7;
          if (dBg) System.out.println("Network mode screen displayed");
          break;
        case 9:
          currentScreen = 8;
          if (dBg) System.out.println("Login screen displayed");
          break;
      }
    }
    if (layeredPane.findComponentAt(e.getX(), e.getY()) != back) {
      back.setIcon(new ImageIcon("img/back.png"));
    }
    displayCurrentScreen();
  }