/* *Class GameArea * *Internetprogrammering 1 - Course **/ import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.net.*; import java.io.*; /**Class GameArea - which is a subclass to JPanel - is the *central controlling class that make things happend when *buttons in class MagnificentBall(GUI) are pressed. *Furthermore, it updates the ball and player positons. * *@author Martin Carlsson * *@see javax.swing.JPanel * **@see MagnificentBall */ public class GameArea extends JPanel { private boolean isActive = false, isPlaying = false, pausPressed = false; private boolean connected = false, gameOver = false, hardMode = false; public Thread receiveActivity, ballActivity, playerOneActivity, playerTwoActivity; private int xMax, yMax; private MagnificentBall gui; private Ball ball; private Player p1, p2; private Sender sender; private InetAddress remoteAdr; private Receiver receiver; /**Constructs a game area *@param gui a reference back to the graphical user interface */ public GameArea(MagnificentBall gui) { setBackground(Color.black); this.gui = gui; addKeyListener(kl); } /** *Used for sending the status of what is being sent to the Sender class *@param s describes what is being sent *@see Sender */ public void sendStatus(String s) { if(sender != null && connected) sender.sendAway(p1.getXpos() + xMax - p1.getWidth(), p1.getYpos(), p2.getLife(), p2.getPoints(), p1.getLife(), p1.getPoints(), s); } /** *Creates Player, Sender, Ball and Receiver instances. *Starts the process of receiving and sending data. *@param localPort the local port to listen for incoming traffic *@param remoteAdr address to connect to *@param remotePort port to send to *@param xMax game area horizontal size *@param yMax game area vertical size *@see Player *@see Sender *@see Ball *@see Receiver *@see #disconnect() */ public void connect(int localPort, InetAddress remoteAdr, int remotePort, int xMax, int yMax) { this.xMax = xMax; this.yMax = yMax; p1 = new Player(this, 0); // Player 1 created p2 = new Player(this, xMax - p1.getWidth()); // Player 2 created playerOneActivity = new Thread(p1); // Thread for player 1 playerTwoActivity = new Thread(p2); // Thread for player 2 p1.setLooping(true); p2.setLooping(true); playerOneActivity.start(); //Thread for player 1 is started playerTwoActivity.start(); //hread for player 2 is started sender = new Sender(remoteAdr, remotePort, gui, this); // Sender instance is created ball = new Ball(gui, this, p1, p2, sender); // Ball instance is created ballActivity = new Thread(ball); // Ball Thread created receiver = new Receiver(this, gui, localPort, p1, p2, ball); // Receiver instance created receiveActivity = new Thread(receiver); // Receiver Thread created receiveActivity.start(); // Receive Thread started sendStatus("activ"); // sends request to be in "active mode" isPlaying = false; // the game is not started yet, just connected } // end connect() /** *Stops the threads created and disconnects all socket connections *@see #connect(int localPort, InetAddress remoteAdr, int remotePort, int xMax, int yMax) */ public void disconnect(){ if(connected) { if(sender != null) { try{ sendStatus("quit"); // send quit status sender.closeSendSocket(); sender = null; } catch(NullPointerException e) { System.err.println(e); } } if(receiver != null) { receiver.setLooping(false); receiver.closeReceiveSocket(); receiver = null; } if(ball != null) { ball.setLooping(false); ball = null; } if(p1 != null) { p1.setLooping(false); p1 = null; } if(p2 != null) { p2.setLooping(false); p2 = null; } gui.setConnectButton(true); // button enabled again gui.setTitle(gui.getGameTitle() + "--DISCONNECTED"); connected = false; isPlaying = false; isActive = false; gui.setAllButtons(true); // all buttons enabled again gui.setHardButton(true); } } //end disconnect() /** *Updates the game area, ie the players and the ball positons. *@param g Graphics object */ public void paintComponent(Graphics g) { super.paintComponent(g); if(ball != null) { g.setColor(ball.getColor()); g.fillOval(ball.getXPos() - ball.getRadie(), ball.getYPos() - ball.getRadie(), ball.getWidth(), ball.getHeight()); } if(p1 != null) { g.setColor(p1.getColor()); g.fillRect(p1.getXpos(), p1.getYpos(), p1.getWidth(), p1.getHeight()); } if(p2 != null && receiver != null) { g.setColor(p2.getColor()); g.fillRect(p2.getXpos(), p2.getYpos(), p2.getWidth(), p2.getHeight()); } } //end paintComponent() /** *Sets the hard or easy mode. *@param hardMode true or false */ public void setHardMode(boolean hardMode) { this.hardMode = hardMode; } /** *Starts the game. */ public void startGame() { if(isActive && !gameOver && connected){ ball.setLooping(true); // restarts the thread if(!ballActivity.isAlive()) ballActivity.start(); sendStatus("resumed"); setBackground(Color.black); isPlaying = true; gui.setAllButtons(true); } } /** *Game Over sequence. */ public void setGameOver() { if(isActive) { sendStatus("gameOver"); // sends game over status ball.setLooping(false); // stops the ball } gui.setTitle(gui.getGameTitle() +" GAME OVER"); setBackground(Color.darkGray); gameOver = true; } /** *Skapar nytt spel. */ public void newGame() { if(isActive && connected){ gameOver = false; stopGame(); sendStatus("newGame"); // sends info on new game pausPressed = false; reset(); // resets to initial values startGame(); // starts the game } } /** *Stops the game. */ public void stopGame() { if(isActive && !gameOver && connected) { ball.setLooping(false); sendStatus("stoped"); // sends stoped info } } /** *Resets to initial values. */ public void reset() { // Player 1, i.e. the player one self is controlling p1.setLife(10); p1.setLostStatus(false); p1.setPoints(0); p1.setYpos(yMax / 2 - p1.getHeight() / 2); p1.setColor(Color.magenta); gui.setPlayerOnePoints(String.valueOf(p1.getPoints())); gui.setPlayerOneLife(String.valueOf(p1.getLife())); // Player 2, i.e. the opponent p2.setLife(10); p2.setLostStatus(false); p2.setPoints(0); p2.setYpos(yMax / 2 - p2.getHeight() / 2); p2.setColor(Color.magenta); gui.setPlayerTwoPoints(String.valueOf(p2.getPoints())); gui.setPlayerTwoLife(String.valueOf(p2.getLife())); if(hardMode && isActive) { // initial speed slightly changed ball.setSpeed(ball.getInitSpeed() + 2); ball.setXStep(ball.getInitSpeed() + 2); ball.setYStep(ball.getInitSpeed() + 2); ball.setFrequency(95); // with what frequency(how fast) the ball should loop Player.setBonusLimit(15); // higher bonus limit is set } else if (isActive) { ball.setSpeed(ball.getInitSpeed()); ball.setXStep(ball.getInitSpeed()); ball.setYStep(ball.getInitSpeed()); ball.setFrequency(100); Player.setBonusLimit(10); } if(isActive) // ball is positioned against Player 1(your avatar) ball.setXPos(ball.getRadie() + p1.getWidth() + 1); else // ball is positioned against Player 2(your oppenent's avatar) ball.setXPos(xMax - ball.getRadie() - p2.getWidth() - 1); ball.setYPos(yMax / 2); // position the ball vertically centered setBackground(Color.black); // background color set to black gui.setPlayerLifeColor("reset"); // resets the life status color to it't initial value if(isActive) { gui.setTitle(gui.getGameTitle() + "--Connected to " + gui.getHost() + "--Active Mode"); gui.setPausButton(false); gui.setContinueButton(false); } else { gui.setTitle(gui.getGameTitle() + "--Connected to " + gui.getHost() + "--Passive Mode"); gui.setAllButtons(false); gui.setHardButton(false); } gui.setConnectButton(false); } // end reset() // Listens to up- and -downarrow buttons KeyListener kl = new KeyAdapter() { public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_UP && p1 != null) p1.setYpos(Math.max(0, p1.getYpos() - p1.getStep())); else if(e.getKeyCode() == KeyEvent.VK_DOWN && p1 != null) p1.setYpos(Math.min(yMax - p1.getHeight(), p1.getYpos() + p1.getStep())); sendStatus("player"); repaint(); } }; // end kl adapter /** *Returns the game area horizontal size *@return yMax game area horizontal size */ public int getYmax() { return yMax; } /** *Returnerar spelplanens storlek i x-led *@return spelplanens storlek i x-led */ public int getXMax() { return xMax; } /** *Anropar repaint() */ public void paintGameComponents() { repaint(); } /** *Returns game over status *@return game over status, true or false */ public boolean gameOver() { return gameOver; } /** *Sets paus status to true or false *@param p true or false */ public void setPausPressed(boolean p) { pausPressed = p; } /** *Returns paus pressed status *@return returns true or false */ public boolean pausPressed() { return pausPressed; } /** *Sets connected to true or false @param s true or false */ public void setConnected(boolean s) { connected = s; } /** *Returns connect status *@return true or false */ public boolean connected() { return connected; } /** *Returns info on active status *@return player status, active or not, true or false *@see #setActive(boolean b) */ public boolean activePlayer() { return isActive; } /** *Sets player to active or passive *@param b true or false *@see #activePlayer() */ public void setActive(boolean b) { isActive=b; } /** *Returns playing status *@return currently playing, true or false */ public boolean playing() { return isPlaying; } } // End class GameArea