Up | Previous | Next |
Penguin
:
By far the most immediate explanation should be explaining
import java.awt.*; public class Penguin { int x, y; String playerID; public Penguin(int x, int y, int width, int height, String owner) { this.x = x; this.y = y; this.playerID = owner; } Client location; Image[] frames; public void placeIn(Client location) { this.location = location; frames = this.location.small; } public void perform(String action) { } }
frames
:
You note that changes now are totally parallel with whether the code will run over the network or not.
import java.awt.*; import javax.swing.*; import java.rmi.*; import java.rmi.server.*; import java.awt.event.*; import java.net.*; import java.net.*; public class Client extends JFrame implements ClientInterface, KeyListener { static Image small[]; static { String pictureURL = "http://www.cs.indiana.edu/classes/a348/CTED/moduleFour/lectures/iceblox/iceblox.gif"; MediaTracker tracker = new MediaTracker(null); Image collection; try { collection = Toolkit.getDefaultToolkit().getImage(new URL(pictureURL)); } catch (Exception e) { collection = Toolkit.getDefaultToolkit().getImage("iceblox.gif"); } }
Let's finish the
import java.awt.*; import javax.swing.*; import java.rmi.*; import java.rmi.server.*; import java.awt.event.*; import java.net.*; import java.awt.image.*; public class Client extends JFrame implements ClientInterface, KeyListener { static Image small[]; static { String pictureURL = "http://www.cs.indiana.edu/classes/a348/CTED/moduleFour/lectures/iceblox/iceblox.gif"; Label label = new Label(); MediaTracker tracker = new MediaTracker(label); Image collection; try { collection = Toolkit.getDefaultToolkit().getImage(new URL(pictureURL)); } catch (Exception e) { collection = Toolkit.getDefaultToolkit().getImage("iceblox.gif"); } tracker.addImage(collection, 0); try { tracker.waitForID(0); } catch (InterruptedException e) { } ImageProducer collectionProducer = collection.getSource(); int smalls = 48; small = new Image[smalls]; int k = 0, i = 0, j = 0; ImageFilter filter; while (k < smalls) { filter = new CropImageFilter(j * 30, i * 30, 30, 30); small[k] = label.createImage(new FilteredImageSource(collectionProducer, filter)); tracker.addImage(small[k], 1); k++; j++; if (j == 8) { j = 0; i++; } } try { tracker.waitForID(1); } catch (InterruptedException e) { } }
Penguin
:
So we know now how the
import java.awt.*; public class Penguin { int x, y; String playerID; public Penguin(int x, int y, int width, int height, String owner) { this.x = x; this.y = y; this.playerID = owner; } Client location; Image[] frames; public void placeIn(Client location) { this.location = location; frames = this.location.small; } public void perform(String action) { } int look = 2; public void draw(Graphics g, boolean self) { g.drawImage(frames[look], x, y, location); if (self) { g.drawRect(x, y, 30, 30); } } }
update
method needs to look:
Now we can compile, etc. then run the program.
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); this.repaint(); } else { players[player].perform(action); } }
Here's one more set of changes; first:
This brings back the
ServerInterface server; public void registerWith(ServerInterface server) throws RemoteException { this.server = server; this.id = server.register(this); System.out.println("I have registered, I have id: " + this.id); server.broadcast(this.id, this.x, this.y, "happy to be here"); }
main
in Client
to:
Same goes for 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); } catch (Exception e) { System.out.println("Error in client..." + e); e.printStackTrace(); } }
Setup
:
Now we can compile, etc.:
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); Client b = new Client(cols, rows, 2, 3); b.registerWith(server); Client c = new Client(cols, rows, 4, 5); c.registerWith(server); } }
When we run the
javac *.java rmic -classpath . Server rmic -classpath . Client java Setup
Setup
we obtain:
So now the clients show themselves, and the others.
Next we run the distributed program (these are the commands to be issued in separate windows):
The outcome is still the same.
java -Djava.security.policy=java.policy Server 18276 java Client 127.0.0.1 18276
Up | Previous | Next |