GUI is stable and can handle board changes. It's ugly, but it works.

This commit is contained in:
Alexander Huddleston 2015-10-29 19:49:05 -05:00
parent a5671cab39
commit e8d9c61f86

View file

@ -14,35 +14,136 @@ import javax.swing.*;
import javax.*; import javax.*;
import java.awt.*; import java.awt.*;
import java.awt.geom.*; 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; //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() { public Client() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initializeGui();
} }
public void drawBoard(String b) { public final void initializeGui() {
board = b; createImages();
bp = new BoardPanel();
board = TrimBoard(); gui.setBorder(new EmptyBorder(5,5,5,5));
JFrame frame = new JFrame("Breakthrough");
frame.getContentPane().add(BorderLayout.CENTER, bp); boardPanel = new JPanel(new GridLayout(0, 9)) {
frame.setSize(800, 600);
frame.setLocationRelativeTo(null); /**
frame.setVisible(true); * Override the preferred size to return the largest it can, in
bp.DrawPieces(); * a square shape. Must (must, must) be added to a GridBagLayout
/* * as the only component (it uses the parent as a guide to size)
Graphics test = getGraphics(); * with no GridBagConstaint (so it is centered).
test.clearRect(0, 0, 800, 600); */
test.setColor(Color.black); @Override
*/ public final Dimension getPreferredSize() {
//frame.getContentPane().add(test, BorderLayout.CENTER); Dimension d = super.getPreferredSize();
//this.paint(test); Dimension prefSize = null;
//frame.repaint(); 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 = ""; String output = "";
for(int i = 0; i < board.length(); ++i) { for(int i = 0; i < board.length(); ++i) {
if(board.charAt(i) == 'X') { if(board.charAt(i) == 'X') {
@ -59,62 +160,45 @@ public class Client extends JFrame {
} }
return output; return output;
} }
class BoardPanel extends JPanel { public final void updateBoard(String board) {
public void DrawPieces() { board = TrimBoard(board);
Graphics g = getGraphics(); int count = 0;
g.clearRect(0, 0, 800, 600); ImageIcon icon = new ImageIcon( new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));
g.setColor(Color.black); for(int r = 0; r < 8; ++r) {
this.validate(); for(int c = 0; c < 8; c++) {
//add("Center", new BoardCanvas()); if(board.charAt(count) == 'X') {
boardSquares[r][c].setIcon(new ImageIcon(pieceImages[0]));
//background.setRect(0.0, 0.0, 800.0, 600.0); }
//back = background; else if(board.charAt(count) == 'O') {
//back.setPaint(0, 0, 0); boardSquares[r][c].setIcon(new ImageIcon(pieceImages[1]));
//g.draw(background); }
} else {
} boardSquares[r][c].setIcon(icon);
}
class BoardCanvas extends Canvas { count++;
public void paint(Graphics graphics) { }
Graphics2D g = (Graphics2D) graphics; }
if(board.charAt(count) == 'X') {
Shape pieces[] = new Shape[32]; boardSquares[7][7].setIcon(new ImageIcon(pieceImages[0]));
}
int c = 0; else if(board.charAt(count) == 'O') {
boardSquares[7][7].setIcon(new ImageIcon(pieceImages[1]));
Shape sh = new Ellipse2D.Double(0,0,100,100); }
g.draw(sh); else {
boardSquares[7][7].setIcon(icon);
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 void sendLocation() {
}
public static void main (String[] args) { public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in); Scanner keyboard = new Scanner(System.in);
String hostname = args[0]; String hostname = args[0];
int portnum = Integer.parseInt(args[1]); int portnum = Integer.parseInt(args[1]);
Client window = new Client();
try { try {
Socket echoSocket = new Socket(hostname, portnum); Socket echoSocket = new Socket(hostname, portnum);
PrintWriter output = new PrintWriter(echoSocket.getOutputStream(), true); PrintWriter output = new PrintWriter(echoSocket.getOutputStream(), true);
@ -139,6 +223,17 @@ public class Client extends JFrame {
boolean end = false; boolean end = false;
int c = 0; 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)) { while(!end && (userInput != null)) {
userInput = stdIn.readLine(); userInput = stdIn.readLine();
output.println(userInput); output.println(userInput);
@ -151,20 +246,15 @@ public class Client extends JFrame {
} }
} }
System.out.println(String.valueOf(buffer).trim()); System.out.println(String.valueOf(buffer).trim());
window.drawBoard(String.valueOf(buffer).trim()); window.updateBoard(String.valueOf(buffer).trim());
} }
output.close(); output.close();
in.close(); in.close();
stdIn.close(); stdIn.close();
echoSocket.close(); echoSocket.close();
} }
catch (IOException e){ catch (IOException e){
System.err.println("IOException: " + e.getMessage()); System.err.println("IOException: " + e.getMessage());
} }
} }
String board;
BoardPanel bp;
} }