FileDocCategorySizeDatePackage
GameBoard.javaAPI DocExample3646Sat Jul 09 15:00:56 BST 2005None

GameBoard

public class GameBoard extends JFrame implements ResolveListener, Runnable

Fields Summary
private TicTacToe
tictactoe
private String
name
private String
host
private int
port
SocketChannel
channel
Constructors Summary
public GameBoard(TicTacToe t, String n, SocketChannel c)

		super(n);
		tictactoe = t;
		name = n;
		channel = c;
		tictactoe.activeGames.put(n, this);
		getContentPane().setLayout(new GridLayout(3,3,6,6));
		getContentPane().setBackground(Color.BLACK);
		for (int i = 0; i<9; i++) getContentPane().add(new SquareGUI(this, i));
		setSize(200,200);
		setVisible(true);
		if (channel != null) new Thread(this).start();
		
Methods Summary
public voidoperationFailed(DNSSDService service, int errorCode)

		System.out.println("Bonjour operation failed " + errorCode);
		System.exit(-1);
		
public voidrun()

		try
			{
			while (true)
				{
				ByteBuffer buffer = ByteBuffer.allocate(4);
				channel.read(buffer);
				int n = buffer.getInt(0);
				if (n >= 0 && n < 9)
					{
					try { SwingUtilities.invokeAndWait(new SquareMarker(n)); }
					catch (Exception e) { e.printStackTrace(); }
					}
				}
			}
		catch (Exception e) { } // Connection reset by peer!
		
public voidserviceResolved(DNSSDService resolver, int flags, int ifIndex, java.lang.String fullName, java.lang.String theHost, int thePort, TXTRecord txtRecord)

		host = theHost;
		port = thePort;
		ByteBuffer buffer = ByteBuffer.allocate(4 + 128);
		CharBuffer charBuffer = buffer.asCharBuffer();
		buffer.putInt(0, tictactoe.myName.length());
		charBuffer.position(2);
		charBuffer.put(tictactoe.myName);
		try
			{
			InetSocketAddress socketAddress = new InetSocketAddress(host, port);
			channel = SocketChannel.open(socketAddress);
			channel.write(buffer);
			new Thread(this).start();
			}
		catch (Exception e) { e.printStackTrace(); }
		
		resolver.stop();