Class Window

java.lang.Object
battleship.view.AbstractView
battleship.view.Window
All Implemented Interfaces:
View

public class Window
extends AbstractView
Window view, instanced if argument 2 equals to "nogui"
See Also:
View, AbstractView
  • Nested Class Summary

    Nested Classes
    Modifier and Type Class Description
    (package private) class  Window.Draw
    Panel where we paint the board
  • Field Summary

    Fields
    Modifier and Type Field Description
    (package private) javax.swing.JFrame frame  
    int height
    grids height, do no represent frame size
    private WindowKeyboardListener keyboardComponent  
    private WindowMouseListener mouseComponent  
    (package private) java.lang.String upperSubTitle  
    (package private) java.lang.String upperTitle  
    int width
    grids width, do no represent frame size

    Fields inherited from class battleship.view.AbstractView

    game

    Fields inherited from interface battleship.view.View

    shipsSize
  • Constructor Summary

    Constructors
    Constructor Description
    Window​(Game game)  
  • Method Summary

    Modifier and Type Method Description
    Pair<java.lang.Integer,​java.lang.Integer> chooseMove​(Player player)
    ask player to choose a position in its opponent grid
    void displayBoard()
    refresh windows to display updated content
    void displayWinner​(Player winner)
    open a dialog to display the winner and exit the program when window is closed
    protected java.lang.String getKeyInput()
    ask player for keyboard input and return result
    protected java.lang.String keyboardInput()
    ask Game.currentPlayer for keyboard input
    protected Pair<java.lang.Integer,​java.lang.Integer> mouseInput​(Player player)
    ask player for mouse input
    void openDialog​(java.lang.String message)
    Open a window with message as content and with a "OK" button
    void openDialog​(java.lang.String message, boolean exitOnClose)
    Open a window with message as content and with a "OK" button
    void setShips​(Player player)
    Ask player to set position of its ships
    protected void setUpperText​(java.lang.String s)
    Display a text above the grid

    Methods inherited from class battleship.view.AbstractView

    getDirectionFromChar, toString, toString

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • frame

      final javax.swing.JFrame frame
    • height

      public final int height
      grids height, do no represent frame size
      See Also:
      Constant Field Values
    • width

      public final int width
      grids width, do no represent frame size
      See Also:
      Constant Field Values
    • mouseComponent

      private final WindowMouseListener mouseComponent
    • keyboardComponent

      private final WindowKeyboardListener keyboardComponent
    • upperTitle

      java.lang.String upperTitle
    • upperSubTitle

      java.lang.String upperSubTitle
  • Constructor Details

    • Window

      public Window​(Game game)
  • Method Details

    • getKeyInput

      protected java.lang.String getKeyInput() throws java.lang.InterruptedException
      Description copied from class: AbstractView
      ask player for keyboard input and return result
      Specified by:
      getKeyInput in class AbstractView
      Returns:
      String given by player
      Throws:
      java.lang.InterruptedException - see getKeyInput()
    • setUpperText

      protected void setUpperText​(java.lang.String s)
      Display a text above the grid
      Specified by:
      setUpperText in class AbstractView
      Parameters:
      s - text to display
      See Also:
      setUpperText(String), Terminal.setUpperText(String)
    • setShips

      public void setShips​(Player player) throws java.lang.InterruptedException
      Ask player to set position of its ships
      Specified by:
      setShips in interface View
      Overrides:
      setShips in class AbstractView
      Parameters:
      player - player we ask
      Throws:
      java.lang.InterruptedException - see mouseInput(Player) and AbstractView.getDirectionFromChar()
    • keyboardInput

      protected java.lang.String keyboardInput() throws java.lang.InterruptedException
      ask Game.currentPlayer for keyboard input
      Specified by:
      keyboardInput in class AbstractView
      Returns:
      String given by player
      Throws:
      java.lang.InterruptedException - throw if this Thread is interrupted while sleeping
      See Also:
      keyboardInput(), Terminal.keyboardInput(), Terminal.keyboardInputInteger()
    • mouseInput

      protected Pair<java.lang.Integer,​java.lang.Integer> mouseInput​(Player player) throws java.lang.InterruptedException
      ask player for mouse input
      Specified by:
      mouseInput in class AbstractView
      Parameters:
      player - Game.currentPlayer
      Returns:
      coordinate of player opponent grid
      Throws:
      java.lang.InterruptedException - throw if this Thread is interrupted while sleeping
      See Also:
      mouseInput(Player)
    • openDialog

      public void openDialog​(java.lang.String message, boolean exitOnClose)
      Open a window with message as content and with a "OK" button
      Parameters:
      message - message to display
      exitOnClose - true if when user close this window, the program exit, false otherwise
    • openDialog

      public void openDialog​(java.lang.String message)
      Open a window with message as content and with a "OK" button
      Parameters:
      message - message to display
      See Also:
      openDialog(String, boolean)
    • displayBoard

      public void displayBoard()
      refresh windows to display updated content
    • chooseMove

      public Pair<java.lang.Integer,​java.lang.Integer> chooseMove​(Player player) throws java.lang.InterruptedException
      ask player to choose a position in its opponent grid
      Specified by:
      chooseMove in interface View
      Overrides:
      chooseMove in class AbstractView
      Parameters:
      player - Game.currentPlayer
      Returns:
      a couple (Pair containing the x and y coordinate (left side store Y and right side X)
      Throws:
      java.lang.InterruptedException - see mouseInput(Player)
    • displayWinner

      public void displayWinner​(Player winner)
      open a dialog to display the winner and exit the program when window is closed
      Parameters:
      winner - the winner of the game.
      See Also:
      displayWinner(Player), Terminal.displayWinner(Player)