Up Previous Next

Now we have a (basic) background, we need to act an avatar.

However, before we do that we need to be ready to control it.

import java.awt.*; 
import javax.swing.*; 
import java.rmi.*; 
import java.rmi.server.*; 

import java.awt.event.*; 

public class Client extends JFrame implements ClientInterface, KeyListener { 

  public Client(int columns, int rows) {
    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); 
  }  

  public void keyReleased(KeyEvent e) { } 

  public void keyTyped(KeyEvent e) { }

  public void keyPressed(KeyEvent e) {
    int val = e.getKeyCode(); 
    try {
      if (val == 38) {
        System.out.println("up"); 
      } else if (val == 39) {
        System.out.println("right"); 
      } else if (val == 40) { 
        System.out.println("down"); 
      } else if (val == 37) {
        System.out.println("left"); 
      } else {
        System.out.println("something else"); 
      } 
    } catch (Exception exception) {
      System.out.println("Glitch: " + exception); 
    }  
  } 
The changes are limited to the part above. The code is split, but whole on this page.

  int cellWidth = 30, cellHeight = 30; 
  class Surface extends JPanel {
    int columns, rows; 
    Surface(int columns, int rows) { 
      this.columns = columns; 
      this.rows = rows; 
    }
    int fontSize = 10; // in pixels 
    Font digitsFont = new Font("Serif", Font.PLAIN, fontSize); 
    public void paintComponent(Graphics g) {
      super.paintComponent(g); 
      ((Graphics2D)g).setFont(digitsFont); 
      g.setColor(Color.black); 
      g.fillRect(0, 0, columns * cellWidth, rows * cellHeight); 
      g.setColor(Color.gray); 
      for (int i = 0; i <= rows; i++) 
        g.drawLine(0, i * cellHeight, columns * cellWidth, i * cellHeight); 
	for (int i = 0; i < columns; i++) { 
        g.drawLine(i * cellWidth, 0, i * cellWidth, rows * cellHeight); 
        g.drawRect(0, 0, cellWidth * columns, cellHeight * rows); 
        g.setColor(Color.gray);
      } 
      for (int j = 0; j < columns; j = j + 1) 
        for (int i = 0; i < rows; i++)
          g.drawString(i + ", " + j, j * cellWidth + 2, i * cellHeight + fontSize);	    
    }  
  }
 
  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); 
  } 
  int id = -1; 
  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); 
        UnicastRemoteObject.exportObject(here); 
        here.registerWith(far); 
    } catch (Exception e) {
        System.out.println("Error in client..." + e); 
        e.printStackTrace();
    }
  } 
}
Let's make sure these keys propagate all the way to the other clients.

One solution would be to send them to the server and have the server relay them to all the clients.


Up Previous Next