diff --git a/Client.java b/Client.java index 809f6fb..b69e41c 100755 --- a/Client.java +++ b/Client.java @@ -14,35 +14,136 @@ import javax.swing.*; import javax.*; import java.awt.*; import java.awt.geom.*; +import java.awt.event.*; +import java.awt.image.BufferedImage; +import javax.swing.border.*; +import java.net.URL; +import javax.imageio.ImageIO; +import java.awt.image.*; //import javax.swing.text.html.parser.ParserDelegator; -public class Client extends JFrame { +public class Client { + + private final JPanel gui = new JPanel(new BorderLayout(3, 3)); + private JButton[][] boardSquares = new JButton[8][8]; + private Image[] pieceImages = new Image[2]; + private JPanel boardPanel; + private static final String Columns = "ABCDEFGH"; + public static final int BLACK = 0, WHITE = 1; public Client() { - this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + initializeGui(); } - public void drawBoard(String b) { - board = b; - bp = new BoardPanel(); - board = TrimBoard(); - JFrame frame = new JFrame("Breakthrough"); - frame.getContentPane().add(BorderLayout.CENTER, bp); - frame.setSize(800, 600); - frame.setLocationRelativeTo(null); - frame.setVisible(true); - bp.DrawPieces(); - /* - Graphics test = getGraphics(); - test.clearRect(0, 0, 800, 600); - test.setColor(Color.black); - */ - //frame.getContentPane().add(test, BorderLayout.CENTER); - //this.paint(test); - //frame.repaint(); + public final void initializeGui() { + createImages(); + + gui.setBorder(new EmptyBorder(5,5,5,5)); + + boardPanel = new JPanel(new GridLayout(0, 9)) { + + /** + * Override the preferred size to return the largest it can, in + * a square shape. Must (must, must) be added to a GridBagLayout + * as the only component (it uses the parent as a guide to size) + * with no GridBagConstaint (so it is centered). + */ + @Override + public final Dimension getPreferredSize() { + Dimension d = super.getPreferredSize(); + Dimension prefSize = null; + Component c = getParent(); + if (c == null) { + prefSize = new Dimension( + (int)d.getWidth(),(int)d.getHeight()); + } else if (c!=null && + c.getWidth()>d.getWidth() && + c.getHeight()>d.getHeight()) { + prefSize = c.getSize(); + } else { + prefSize = d; + } + int w = (int) prefSize.getWidth(); + int h = (int) prefSize.getHeight(); + // the smaller of the two sizes + int s = (w>h ? h : w); + return new Dimension(s,s); + } + }; + boardPanel.setBorder(new CompoundBorder( + new EmptyBorder(8,8,8,8), + new LineBorder(Color.BLACK) + )); + // Set BG. + Color background = new Color(100,100,100); + Color boardbg = new Color(250, 200, 100); + boardPanel.setBackground(background); + JPanel boardConstrain = new JPanel(new GridBagLayout()); + boardConstrain.setBackground(background); + boardConstrain.add(boardPanel); + gui.add(boardConstrain); + + Insets buttonMargin = new Insets(0,0,0,0); + for(int r = 0; r < boardSquares.length; ++r) { + for(int c = 0; c < boardSquares.length; ++c) { + Action selectPiece = new AbstractAction("") { + @Override + public void actionPerformed(ActionEvent e) { + sendLocation(); + } + }; + JButton b = new JButton(); + b.setMargin(buttonMargin); + b.setAction(selectPiece); + + ImageIcon icon = new ImageIcon( new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB)); + b.setIcon(icon); + b.setBackground(boardbg); + boardSquares[r][c] = b; + } + } + + // fill the board. + boardPanel.add(new JLabel("")); + // fill the top row + for(int c = 0; c < 8; c++) { + boardPanel.add(new JLabel(Columns.substring(c, c + 1), SwingConstants.CENTER)); + } + // fill everything else + for(int r = 0; r < 8; ++r) { + for(int c = 0; c < 8; c++) { + switch(c) { + case 0: boardPanel.add(new JLabel("" + (9 - (r + 1)), SwingConstants.CENTER)); + + default: boardPanel.add(boardSquares[r][c]); + } + } + } } - public String TrimBoard() { + public final JComponent getGui() { + return gui; + } + + private final void createImages() { + try { + URL url1 = new URL("http://www.iconsdb.com/icons/preview/black/circle-xxl.png"); + BufferedImage bi = ImageIO.read(url1); + Image img = bi.getScaledInstance(64, 64, BufferedImage.SCALE_FAST); + pieceImages[0] = img; + + URL url2 = new URL("http://www.iconsdb.com/icons/preview/white/circle-xxl.png"); + bi = ImageIO.read(url2); + img = bi.getScaledInstance(64, 64, BufferedImage.SCALE_FAST); + pieceImages[1] = img; + } + catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } + + public String TrimBoard(String board) { String output = ""; for(int i = 0; i < board.length(); ++i) { if(board.charAt(i) == 'X') { @@ -59,62 +160,45 @@ public class Client extends JFrame { } return output; } - - class BoardPanel extends JPanel { - public void DrawPieces() { - Graphics g = getGraphics(); - g.clearRect(0, 0, 800, 600); - g.setColor(Color.black); - this.validate(); - //add("Center", new BoardCanvas()); - - //background.setRect(0.0, 0.0, 800.0, 600.0); - //back = background; - //back.setPaint(0, 0, 0); - //g.draw(background); - } - } - - class BoardCanvas extends Canvas { - public void paint(Graphics graphics) { - Graphics2D g = (Graphics2D) graphics; - - Shape pieces[] = new Shape[32]; - - int c = 0; - - Shape sh = new Ellipse2D.Double(0,0,100,100); - g.draw(sh); - - for(int x = 0; x < board.length(); ++x) { - if(board.charAt(x) == 'X') { - pieces[c] = new Ellipse2D.Double((((x%8) + 1)*10), ((x/8)*20), 10, 10); - c++; - } - - else if(board.charAt(x) == 'O') { - pieces[c] = new Ellipse2D.Double((((x%8) + 1)*10), ((x/8)*20), 10, 10); - c++; - } - } - - for(int s = 0; s < pieces.length; ++s) { - if(pieces[s] != null) { - g.draw(pieces[s]); - } - } - + + public final void updateBoard(String board) { + board = TrimBoard(board); + int count = 0; + ImageIcon icon = new ImageIcon( new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB)); + for(int r = 0; r < 8; ++r) { + for(int c = 0; c < 8; c++) { + if(board.charAt(count) == 'X') { + boardSquares[r][c].setIcon(new ImageIcon(pieceImages[0])); + } + else if(board.charAt(count) == 'O') { + boardSquares[r][c].setIcon(new ImageIcon(pieceImages[1])); + } + else { + boardSquares[r][c].setIcon(icon); + } + count++; + } + } + if(board.charAt(count) == 'X') { + boardSquares[7][7].setIcon(new ImageIcon(pieceImages[0])); + } + else if(board.charAt(count) == 'O') { + boardSquares[7][7].setIcon(new ImageIcon(pieceImages[1])); + } + else { + boardSquares[7][7].setIcon(icon); } } + public void sendLocation() { + } + public static void main (String[] args) { Scanner keyboard = new Scanner(System.in); String hostname = args[0]; int portnum = Integer.parseInt(args[1]); - Client window = new Client(); - try { Socket echoSocket = new Socket(hostname, portnum); PrintWriter output = new PrintWriter(echoSocket.getOutputStream(), true); @@ -139,6 +223,17 @@ public class Client extends JFrame { boolean end = false; int c = 0; + Client window = new Client(); + + JFrame frame = new JFrame("Breakthrough"); + frame.add(window.getGui()); + + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + frame.setLocationByPlatform(true); + frame.pack(); + frame.setMinimumSize(frame.getSize()); + frame.setVisible(true); + while(!end && (userInput != null)) { userInput = stdIn.readLine(); output.println(userInput); @@ -151,20 +246,15 @@ public class Client extends JFrame { } } System.out.println(String.valueOf(buffer).trim()); - window.drawBoard(String.valueOf(buffer).trim()); + window.updateBoard(String.valueOf(buffer).trim()); } output.close(); in.close(); stdIn.close(); echoSocket.close(); } - catch (IOException e){ System.err.println("IOException: " + e.getMessage()); } } - - String board; - - BoardPanel bp; } \ No newline at end of file