public void close() { System.err.println("Closing connection with client"); out.close(); in.close(); try { server.close(); } catch (IOException ioe) { System.out.println("IOException on socket listen: " + ioe); ioe.printStackTrace(); } }
public void run() { System.err.println("Accepted connection from client"); String s; JSONObject json; String command = null; ServerCommand sc = null; while ((s = in.readLine()) != null) { System.out.println("Got command: " + s); try { json = (JSONObject) JSONSerializer.toJSON(s); command = json.getString("command"); if (json != null && command != null) { sc = this.jAuctionServer.serverCommandFactory.getCommand(command, this); JSONObject data = json.getJSONObject("data"); if (sc != null) { if (sc.parseJson(data)) { Thread t = new Thread(sc); t.start(); } else { badRequest(); } json = null; command = null; sc = null; } } } catch (net.sf.json.JSONException e) { badRequest(); System.err.println("bad request: " + s); json = null; command = null; sc = null; } } close(); }
/** * This method creates the content pane that will be a part of the frame, including the JLabels, * JPanels and the JButtons INPUT (parameters): none OUTOUT (return): the main JPanel object that * includes everything */ public JPanel createContentPane() { JPanel totalGUI = new JPanel(); // the main panel totalGUI.setBackground(Color.BLACK); totalGUI.setLayout(null); printInstructions(); startNum = In.getInt(); if (startNum == 1) startGame = true; else startGame = false; while (!startGame) { System.out.println("Please enter 1 to start the game"); startNum = In.getInt(); if (startNum == 1) startGame = true; else startGame = false; } if (startGame) { titlePanel = new JPanel(); // a panel where the title will be displayed titlePanel.setLayout(null); titlePanel.setLocation(0, 0); titlePanel.setSize(1600, 150); Color titlePanelColor = new Color(238, 130, 238); titlePanel.setBackground(titlePanelColor); totalGUI.add(titlePanel); // this panel is added to the main panel gamePanel = new JPanel(); // game panel (where the cards will be displayed) gamePanel.setLayout(null); gamePanel.setLocation(0, 150); gamePanel.setSize(950, 650); gamePanel.setBackground(Color.BLACK); totalGUI.add(gamePanel); newGameButton = new JButton("Start"); // a button to start the game newGameButton.setLocation(800, 155); newGameButton.setSize(120, 30); newGameButton.addActionListener(this); gamePanel.add(newGameButton); // this button is added to the game panel endGameButton = new JButton("End Game"); // a button to end the game endGameButton.setLocation(800, 195); endGameButton.setSize(120, 30); endGameButton.addActionListener(this); gamePanel.add(endGameButton); hitButton = new JButton("Hit! (Player)"); // a hit button for the player hitButton.setLocation(800, 235); hitButton.setSize(120, 30); hitButton.addActionListener(this); gamePanel.add(hitButton); hitButton2 = new JButton("Hit! (Dealer)"); // a hit button for the dealer hitButton2.setLocation(800, 275); hitButton2.setSize(120, 30); hitButton2.addActionListener(this); gamePanel.add(hitButton2); standButton = new JButton("Stand (Player)"); // a stand button for the player standButton.setLocation(800, 315); standButton.setSize(120, 30); standButton.addActionListener(this); gamePanel.add(standButton); standButton2 = new JButton("Stand (Dealer)"); // a stand button for the dealer standButton2.setLocation(800, 355); standButton2.setSize(120, 30); standButton2.addActionListener(this); gamePanel.add(standButton2); playerTitle = new JLabel( "Player"); // label that displays the word "Player" on the screen playerTitle.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); Color playerTitleColor = new Color(0, 250, 154); playerTitle.setForeground(playerTitleColor); playerTitle.setLocation(550, 150); playerTitle.setSize(100, 30); gamePanel.add(playerTitle); dealerTitle = new JLabel("Dealer"); // label that displays the word "Dealer" on the screen dealerTitle.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); Color dealerTitleColor = new Color(0, 250, 154); dealerTitle.setForeground(dealerTitleColor); dealerTitle.setLocation(550, 350); dealerTitle.setSize(100, 30); gamePanel.add(dealerTitle); scoreLabel = new JLabel(""); // label that displays the score scoreLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 30)); Color scoreLabelColor = new Color(255, 20, 147); scoreLabel.setForeground(scoreLabelColor); scoreLabel.setLocation(200, 130); scoreLabel.setSize(400, 300); gamePanel.add(scoreLabel); turnLabel = new JLabel(""); // label that informs the users whose turn it is turnLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); Color turnLabelColor = new Color(255, 127, 80); turnLabel.setForeground(turnLabelColor); turnLabel.setLocation(250, 200); turnLabel.setSize(300, 100); gamePanel.add(turnLabel); score1 = new JLabel(""); score1.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); Color score1Color = new Color(255, 127, 80); score1.setForeground(score1Color); score1.setLocation(660, 135); score1.setSize(100, 60); gamePanel.add(score1); score2 = new JLabel(""); score2.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); Color score2Color = new Color(255, 127, 80); score2.setForeground(score2Color); score2.setLocation(660, 335); score2.setSize(100, 60); gamePanel.add(score2); ImageIcon titleIcon1 = new ImageIcon("BlackJack.jpg"); titlePics = new JLabel(titleIcon1); // an image for decor titlePics.setLocation(0, 0); titlePics.setSize(150, 150); titlePanel.add(titlePics); // image added to title label welcomeLabel = new JLabel("Welcome To Casino Royale!"); welcomeLabel.setFont(new Font("Monotype Corsiva", Font.BOLD, 40)); Color titleColor = new Color(0, 255, 255); // title welcomeLabel.setForeground(titleColor); welcomeLabel.setLocation(245, 0); welcomeLabel.setSize(700, 100); titlePanel.add(welcomeLabel); commandLabel = new JLabel("Please press \"Start\" to start a game of Black Jack!"); commandLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 16)); Color commandColor = new Color(0, 0, 0); commandLabel.setForeground(commandColor); // instruction to user on how to start the game commandLabel.setLocation(250, 101); commandLabel.setSize(700, 44); titlePanel.add(commandLabel); ImageIcon titleIcon3 = new ImageIcon("Swirl.jpg"); titlePics = new JLabel(titleIcon3); // an image for decor titlePics.setLocation(783, 0); titlePics.setSize(150, 150); titlePanel.add(titlePics); totalGUI.setOpaque(true); return totalGUI; // return the final JPanel } return totalGUI; }