and the client:
public class Player{
private static final String ip = "localhost";
private static final int PORT = 2222;
private JFrame frame = new JFrame("Game!");
private final static int WIDTH = 1020;
private final static int HEIGHT = 600;
private GamePanel panel;
private static int id;
private Socket socket;
private ObjectInputStream in;
private ObjectOutputStream out;
public Player() throws IOException, ClassNotFoundException {
try{
socket = new Socket(ip, PORT);
out = new ObjectOutputStream(socket.getOutputStream());
out.flush();
in = new ObjectInputStream(socket.getInputStream());
id = in.readInt();
panel = (GamePanel)in.readObject();
//currentThread = (PlayerThread) in.readObject();
} catch (UnknownHostException e){
System.err.println("Don't know about host " + ip);
System.exit(1);
} catch (IOException e){
System.err.println("Couldn't get I/O for the connection to " + ip);
System.exit(1);
}
}
public synchronized void updatePanel(GamePanel p){
panel = p;
panel.active = true;
panel.P.playing = true;
panel.P.dropped = false;
frame.add(panel);
frame.repaint();
}
public void play() throws Exception{
try{
synchronized(this){
while(true){
String status = (String) in.readObject();
GamePanel p = (GamePanel) in.readObject();
if(status.equals("PLAY")){
updatePanel(p);
System.out.println("Set panel " + id + " to active.");
while(!panel.message.equals("NEXT")){
//wait for the player to make their move
}
System.out.println("Player " + panel.P.ID + " moved.");
panel.active = false;
panel.message = "";
out.writeObject("MOVED"); //set the opponent panel to "active"
out.flush();
}
else if(status.equals("WAIT")){
//in.readObject();
//panel.active = false;
out.writeObject("WAITING");
out.flush();
}
out.writeObject(panel);
out.flush();
}
}
}
catch(Exception e){
System.out.println("Out of while loop scope.");
System.exit(1);
}
finally{
System.err.println("Closing the socket");
socket.close();
}
}
public static void main(String[] args) throws Exception{
System.out.println("in main.");
while(true){
Player client = new Player();
client.createBoard();
client.play();
if(!client.rematch())
break;
}
}
private boolean rematch() {
int response = JOptionPane.showConfirmDialog(frame, "Would you like a rematch?",
" Again?", JOptionPane.YES_NO_OPTION);
//this.dispose();
frame.dispose();
return response == JOptionPane.YES_OPTION;
}
private void createBoard(){
frame.setTitle("! Player " + id);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage imgUrl = null;
ImageIcon imgIcon = null;
try {
imgUrl = ImageIO.read(_Applet.class.getResource("cards/card.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imgIcon = new ImageIcon(imgUrl);
Image img = imgIcon.getImage();
frame.setIconImage(img);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setResizable(false);
System.out.println("Deck pos in client: " + panel.D.deck_position);
if(panel.active)
System.out.println("Player " + id + " is first!");
}
public String toString(){
return id+"";
}
You can see I'm writing the panel and then attempting to updated the other players' panel, but I am definitely not doing it right...