Up Previous Next

The server needs to make sure everybody has all the information:
  int size = -1; 
  public void broadcast(int id, int x, int y, String action) throws RemoteException {
    if (action.equals("happy to be here")) {
      for (int i = 0; i <= size; i++) {
        ClientInterface one = clients[i]; 
        int xi = one.getX(), yi = one.getY(); 
        for (int j = 0; j <= size; j++) { 
          clients[j].update(i, xi, yi, action); 
        }          
      }
    } else { 
      for (int i = 0; i <= size; i++) {
        clients[i].update(id, x, y, action); 
      } 
    } 
  } 
For this the client has to change its interface:
import java.rmi.*;

public interface ClientInterface extends Remote{
  public void update(int player, int x, int y, String action) throws RemoteException;  
  public int getX() throws RemoteException;  
  public int getY() throws RemoteException;  
} 
The implementation is immediate:
  int x, y;
  public int getX() { return this.x; } 
  public int getY() { return this.y; } 
  public Client(int columns, int rows, int x, int y) {
    this.x = x * cellWidth; 
    this.y = y * cellHeight; 
    ...
The Penguin class changes too:
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; 
    location.repaint(); 
  } 
  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.setColor(Color.red); 
      g.drawRect(x-1, y-1, 31, 31); 
    }
  }  
} 
Needless to say the update method no longer has to worry about repainting:
  public void update(int player, int x, int y, 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); 
    } 
  }
So when we run the program:
We now see that the avatar owned by a frame/client has a red rectangular frame.


Up Previous Next