Up | Previous | Next |
See how the
int x, y; public Client(int columns, int rows, int x, int y) { this.x = x * cellWidth; this.y = y * cellHeight; this.setTitle("iceblox: client frame"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(columns * cellWidth + cellWidth / 2, (1 + rows) * cellHeight + cellHeight / 2); getContentPane().add(new Surface(columns, rows), "Center"); addKeyListener(this); this.setVisible(true); }
main
in Client
needs to accomodate this change, a bit later, below. Each client should keep track of where the other clients are:
So there is a class,
Penguin[] players = new Penguin[100]; public void update(int player, String action) { System.out.println("Player " + this.id + " updated by player " + player + " with action: " + action); players[player].perform(action); }
Penguin
:
The question then is, how does a player first gets placed in the frame (as a Penguin):
import java.awt.*; public class Penguin { public Penguin(int x, int y, int width, int height, String owner) { } }
This means
Penguin[] players = new Penguin[100]; public void update(int player, String action) { System.out.println("Player " + this.id + " updated by player " + player + " with action: " + action); if (players[player] == null) { players[player] = new Penguin(x, y, 30, 30, "(" + this.id + ", " + player + ")"); players[player].placeIn(this); } else { players[player].perform(action); } }
Penguin
must keep up:
The
import java.awt.*; public class Penguin { public Penguin(int x, int y, int width, int height, String owner) { } public void placeIn(Client location) { } public void perform(String action) { } }
main
in Client
initiates the sequence of initializations:
Notice that the
public static void main(String[] args) { try { ServerInterface far = (ServerInterface)Naming.lookup("rmi://" + args[0] + ":" + args[1] + "/Dirac"); int cols = 10, rows = 10; Client here = new Client(cols, rows, (int)(Math.random() * cols), (int)(Math.random() * rows)); UnicastRemoteObject.exportObject(here); here.registerWith(far); far.broadcast(here.id, here.x, here.y, "happy to be here"); } catch (Exception e) { System.out.println("Error in client..." + e); e.printStackTrace(); }
Server
's broadcast
method changes a bit:
This change needs to propagate to the server's business card:
public void broadcast(int id, int x, int y, String action) throws RemoteException { for (int i = 0; i <= size; i++) { clients[i].update(id, action); } }
And, eventually, the use of
import java.rmi.*; public interface ServerInterface extends Remote { public int register(ClientInterface client) throws RemoteException; public void broadcast(int name, int x, int y, String action) throws RemoteException; }
broadcast
in the client needs to be updated:
The last change we need to make:
public void keyPressed(KeyEvent e) { int val = e.getKeyCode(); try { if (val == 38) server.broadcast(id, x, y, "up"); else if (val == 39) server.broadcast(id, x, y, "right"); else if (val == 40) server.broadcast(id, x, y, "down"); else if (val == 37) server.broadcast(id, x, y, "left"); } catch (Exception exception) { System.out.println("Glitch: " + exception); } }
At this point we can compile:
import java.rmi.*; class Setup { public static void main(String[] args) throws RemoteException{ Server server = new Server(); int cols = 10, rows = 10; Client a = new Client(cols, rows, 0, 1); a.registerWith(server); server.broadcast(a.id, a.x, a.y, "happy to be here"); Client b = new Client(cols, rows, 2, 3); b.registerWith(server); server.broadcast(b.id, b.x, b.y, "happy to be here"); Client c = new Client(cols, rows, 4, 5); c.registerWith(server); server.broadcast(c.id, c.x, c.y, "happy to be here"); } }
The changes are all consistent, compilation proceeds without error or warning.
javac *.java
What do we do next? (There's more than one thing we can do now, so which do we choose first?)
Up | Previous | Next |