public
anonymous / Board.java
Created

  • Download Gist
Board.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
package pl.mc.battleships.view;
 
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
 
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
 
import pl.mc.battleships.common.DataPack;
import pl.mc.battleships.common.ShipType;
import pl.mc.battleships.common.events.GameEvent;
import pl.mc.battleships.controller.Controller;
 
/**
* @author mc
* View class (MVC pattern implementation) - responsible for creating
* the user interface and interacting with player during the gameplay.
*/
public class View {
private BattleshipsFrame frame;
private Connection controllerConnection;
 
/** Implementation of a Singleton pattern. */
private static View instance = null;
/** @return View class instance. */
public static synchronized View getInstance() {
if(instance == null) instance = new View();
return instance;
}
/** View class constructor. */
private View() {}
/** Method for showing main window of the game. */
public void showFrame() {
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
frame = new BattleshipsFrame();
}
});
}
/** Method responsible for creating the server */
void createServer() {
final View view = this;
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
//create needed objects and initialize the controller
BlockingQueue<GameEvent> eventQueue = new LinkedBlockingQueue<GameEvent>();
controllerConnection = new LocalConnection(eventQueue);
frame.changeStatus("Waiting for another player...");
Controller.getInstance(eventQueue, view);
//controller.run();
}
});
}
/** Method responsible for connecting to server */
void connectToServer(final String ipAddress) {
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
//nothing to see here yet, move along
controllerConnection = new RemoteConnection(ipAddress);
frame.changeStatus("Connecting to " + ipAddress + "...");
}
});
}
/** Method responsible for redrawing the displayed boards */
public void refreshView(final DataPack data) {
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
frame.refreshBoards(data);
}
});
}
/** Method for validating inputed ip address */
public final static boolean ipAddressIsValid(final String ipAddress) {
String[] parts = ipAddress.split("\\.");
if(parts.length != 4) return false;
for(String s : parts) {
int i = Integer.parseInt(s);
if ((i < 0) || (i > 255)) return false;
}
return true;
}
/** Method for sending information to View containing
* count and types of the ships that must be placed on the board */
public void placeShips(final LinkedList<ShipType> ships) {
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
for(@SuppressWarnings("unused") ShipType ship : ships) {
//TODO
}
}
});
}
/**
* @author mc
* Class representing main window of a game.
*/
class BattleshipsFrame extends JFrame {
private static final long serialVersionUID = 1L;
private final Board playerBoard, opponentBoard;
private final JButton startNewGameButton;
private final JLabel statusLabel;
private View view;
/** Main window constructor. */
BattleshipsFrame() {
//setting window parameters
super("Battleships");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//acquiring upper View class reference
view = View.getInstance();
//setting layout and look
setLayout(new BorderLayout());
JPanel bottom = new JPanel();
bottom.setLayout(new BorderLayout());
add(BorderLayout.SOUTH, bottom);
JPanel boards = new JPanel();
boards.setLayout(new GridLayout(1, 2));
add(BorderLayout.CENTER, boards);
//creating and displaying boards
playerBoard = new Board();
opponentBoard = new Board();
PlayerBoardListener mouseListener = new PlayerBoardListener();
this.addMouseListener(mouseListener);
playerBoard.addMouseListener(mouseListener);
opponentBoard.addMouseListener(new OpponentBoardListener());
boards.add(playerBoard);
boards.add(opponentBoard);
//creating and displaying other widgets
statusLabel = new JLabel("Click 'New Game' button to continue");
bottom.add(BorderLayout.LINE_START, statusLabel);
startNewGameButton = new JButton("New Game");
bottom.add(BorderLayout.LINE_END, startNewGameButton);
startNewGameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createNewGameDialog();
}
});
//configuring look
setResizable(false);
setSize(810, 445);
setVisible(true);
}
/** Method responsible for creating new game dialog */
private void createNewGameDialog() {
//creating and setting up dialog window
final JDialog dialog = new JDialog(BattleshipsFrame.this, "New Game", true);
dialog.setLayout(new GridLayout(4,1));
dialog.setSize(400, 150);
//creating and setting up radio buttons
final JRadioButton hostButton = new JRadioButton("Host network game on this computer");
final JRadioButton joinButton = new JRadioButton("Join another game");
hostButton.setSelected(true);
final ButtonGroup bg = new ButtonGroup();
bg.add(hostButton);
bg.add(joinButton);
dialog.add(hostButton);
dialog.add(joinButton);
//creating join game panel
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,2));
final JLabel ipLabel = new JLabel("Server IP Address");
final JTextComponent ipTextField = new JTextField();
ipLabel.setEnabled(false);
ipTextField.setEnabled(false);
panel.add(ipLabel);
panel.add(ipTextField);
dialog.add(panel);
//creating OK button
panel = new JPanel();
final JButton okButton = new JButton("OK");
panel.add(okButton);
dialog.add(panel);
okButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
if(hostButton.isSelected()) {
view.createServer();
dialog.dispose();
} else {
if(View.ipAddressIsValid(ipTextField.getText())) {
view.connectToServer(ipTextField.getText());
dialog.dispose();
}
}
}
});
//making RadioButtons activate/deactivate ip address input
hostButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
ipLabel.setEnabled(false);
ipTextField.setEnabled(false);
}
});
joinButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
ipLabel.setEnabled(true);
ipTextField.setEnabled(true);
}
});
dialog.setVisible(true);
}
/** Method responsible for changing the statusLabel text */
public void changeStatus(final String newStatus) {
statusLabel.setText(newStatus);
}
/** Method responsible for refreshing the boards */
public void refreshBoards(final DataPack data) {
playerBoard.refreshBoard(data.playerShips, data.playerShots);
opponentBoard.refreshBoard(data.opponentShips, data.opponentShots);
}
/** Listener responsible for handling mouse events on player board */
public class PlayerBoardListener extends MouseAdapter {
@Override public void mousePressed(MouseEvent event) {
try {
if(event.getButton() == MouseEvent.BUTTON1 && controllerConnection != null)
controllerConnection.sendShipPlacedEvent(event.getX()/40, event.getY()/40, ShipType.EMPTY);
} catch(Exception e) {
e.printStackTrace();
}
}
@Override public void mouseMoved(MouseEvent event) {
//TODO
System.out.println("Mouse movement detected! Actual mouse position is: " + event.getX()+ "," + event.getY() + ".");
}
@Override public void mouseDragged(MouseEvent event) {
//TODO
System.out.println("Mouse movement detected! Actual mouse position is: " + event.getX()+ "," + event.getY() + ".");
}
}
/** Listener responsible for handling mouse events on opponent board */
private class OpponentBoardListener extends MouseAdapter {
@Override public void mousePressed(MouseEvent event) {
try {
if(event.getButton() == MouseEvent.BUTTON1 && controllerConnection != null)
controllerConnection.sendShotEvent(event.getX()/40, event.getY()/40);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.