Figure 1. Commands to Enter at a Shell (ksh) Command Prompt to Set Up and Run the JDK Demos

>export PATH=$PATH:/usr/lpp/Java/bin:.
>cd/usr/lpp/Java/demo
>export DISPLAY=mydisplay:0
>CompileDemos
>RunDemos

Back to paper


Figure 2.1. Five Steps to Creating an Applet

Back to paper


Figure 2.2. Five Steps to Creating an Applet

Back to paper


Figure 3. Commands for Copying Images from the Animator Demo

cd $HOME/java
mkdir images
cp /usr/lpp/Java/demo/Animator/images/Duke/*images
mv images/T10.gif images/T0.gi
Back to paper


Figure 4.1. Modified Version of helloWorld Implementing the Runnable Interface


import java.awt.*;
import java.applet.*;

public class helloWorld extends Applet implements Runnable {
   Thread animationThread=null;
   protected int cur = 0, delay=150;
   protected Image frame[] = new Image[10];

   public void init() {
      resize (150,150);
      for (int i = 0; i<10; i++)
         frame[i] = getImage(getDocumentBase(),
                        "images/T"+i+".gif");
   }
    
   public void start() {
      animationThread = new Thread (this);
      animationThread.start (); 
   }

   public void run () {
      while  (true) {
          for (cur=1;  cur<10;  cur++) {
             repaint();
             try {   //  slow down the image changes
                Thread.sleep(delay);
             } catch  (InterruptedException e) {}
          }
          cur=0;
          repaint();
      }
   }
1
   public void paint (Graphics g) {
      g.drawString(new String("Hello World!"),  70,50);
      g.drawImage(frame[cur], 25, 55, this);
   }
   public void stop() {
      if (animationThread != null &&
                  animationThread.isAlive())
          animationThread.stop();
   }

}

Back to paper


Figure 4.2. Modified Version of helloWorld Implementing the Runnable Interface


import java.awt.*;
import java.applet.*;

public class helloWorld extends Applet implements Runnable {
   Thread animationThread=null;
   protected int cur = 0, delay=150;
   protected Image frame[] = new Image[10];

   public void init() {
      resize (150,150);
      for (int i = 0; i<10; i++)
         frame[i] = getImage(getDocumentBase(),
                        "images/T"+i+".gif");
   }
    
   public void start() {
      animationThread = new Thread (this);
      animationThread.start (); 
   }

   public void run () {
      while  (true) {
          for (cur=1;  cur<10;  cur++) {
             repaint();
             try {   //  slow down the image changes
                Thread.sleep(delay);
             } catch  (InterruptedException e) {}
          }
          cur=0;
          repaint();
      }
   }
1
   public void paint (Graphics g) {
      g.drawString(new String("Hello World!"),  70,50);
      g.drawImage(frame[cur], 25, 55, this);
   }
   public void stop() {
      if (animationThread != null &&
                  animationThread.isAlive())
          animationThread.stop();
   }

}

Back to paper


Figure 5. Update() Message to Insert into the New HelloWorld Class

public void update (Graphics g) {
       g.setColor (getBackground());
       g.fillRect (25,55,60,80);
       g.setColor (getForeground());
       paint (g);
}

Back to paper


Figure 6. Alternate init() Method


public void init() {
   int i;
   resize(150,150);
   MediaTracker tracker = new MediaTracker(this);
   for (i=o; i< i++) {
      frame[i] = getImage(getDocumentBase(),
         "images/T" + i + ".gif");
      tracker.addImage(frame[i],i);
   }
   showStatus("loading images");
   try {
      tracker.waitForAll();
   } catch (InnterruptedException e) {}
   showStatus("Images loaded");
}

Back to paper


Figure 7.1. First Set of Changes to helloWorld.java for Double-Buffering


public void update(Graphics g) {
   paint(g);
}
public void paint(Graphics g) {
   bufGraphic.setColor(getBackground());
   bufGraphic.fillRect(0,0,150,150);
   bufGraphic.setColor(getForeground());
   bufGraphic.drawString(new String("Hello World!"),
         70,50);
   bufGraphic.drawImage(frame[cur], 25, 55, this);
   g.drawImage(bufImage, 0,0, this);
}

Back to paper


Figure 7.2. Second Set of Changes to helloWorld.java for Double-Buffering

protected Image bufImage;
protected Image Graphics bufGraphics;

public void init() {
   bufImage = createImage(150,150);
   bufGraphic = bufImage.getGraphics();

Back to paper


Figure 8. Final WhiteBoard Applet

Back to paper


Figure 9. Shape and wbPanel Classes in the wb.java File

import java.awt.*;
import java.util.Vector;

class Shape extends Object {
   public int x1, y1, x2, y2;
   public Color  c;

   public Shape(int x1, int y1, int x2,
               int y2, Color c) {
      this.x1 = x1;
      this.y1 = y1;
      this.x2 = x2;
      this.y2 = y2
      this.c = c;
   }
}

class wbPanel extends Panel {
   int  x1,y1, x2,y2, x1,y1;
1 public static Vector shapes;

   public wbPanel() {
      setBackground(Color.white);
      setFont(new Font("TimesRoman",
            Font.BOLD,16));
2    shapes = new Vector();
   }
   public boolean handleEvent(Event e) {
      switch  (e.id)  {
        case Event.MOUSE_DOWN:
          x1 = e.x;
          y1 = e.y;
          x2 = -1;
          return true;
        case Event.MOUSE_UP:
          Shape s = new Shape(x1,y1.e.x,
               e.y,getForeground());
3      shapes.addElement(s);
          x2 = x1 = -1;
          repaint();
          return true;
        default
          return false;
      }
   }
   public void paint(Graphics g) {
4    int nShapes = shapes.size();
      g.setColor(getForeground());
      g.setPaintMode();
      for (int i=0; i < nShapes; i++) {
5       Shape p = (Shape)shapes.elementAt(i);
         g.setColor((Color)p.c);
         g.drawLine(p.x1, p.y1, p.x2, p.y2);
      } 
   }
}

Back to paper


Figure 10. The whiteBoard.java File, Including the whiteBoard and wbControlsClasses

import java.awt.*;
import java.applet.*;
import wbPanel;

public class whiteBoard extends Applet {
   Label wbTitle  =  new Label ("White Board",
                      Label.CENTER);

   public void init()  {
     setLayout(new BorderLayout());
     wbPanel wp = new wbPanel();
     wbTitle.setFont(new Font("TimesRoman",
             Font.BOLD,18));
     add ("North", wbTitle);
     add ("Center", wp);

1
     add("South",new wbControls(wp));
   }
}

class wbControls extends Panel {
   wbPanel traget;

   public wbControls(wbPanel target) {
     this.target = target;
     Choice ink = new Choice();
     ink.adItem("Black");
     ink.addItem("Blue");
     ink.addItem("Red");
     add(ink);
    }
   public boolean action(Event e, Object  arg) {
     if (e.target instanceof  Choice) {
         String choice = (String)arg;
         if (choice.equals("Red"))  {
            target.setForeground(Color.red);
         } else if (choice.equals("Blue")) {
            target.setForeground(Color.blue);
         } else if (choice.equals("Black")) {
            target.setForeground(Color.black);
         }
     }
   return true;
   }
}


Back to paper


Figure 11. The New wb Class

public abstract class wb {
   private static wbPanel whiteBoard;
   public static Vector shapes;

   public static synchronized void setWb (wbPanel wp) {
       shapes = new Vector ();
       whiteBoard = wp;
   }

   public static synchronized void clearWb () {
       shapes = new Vector ();
       whiteBoard.repaint();
   }

   public static synchronized void addShape(Shape s) {
       shape.addElement(s);
   }
}

Back to paper


Figure 12. The handleEvent Method

public boolean handleEvent (Event evt) {
   switch(evt.id) {
     case Event.ACTION_EVENT:
         if (("RESTART").equals(evt.arg)) {
           if (animationThread.isAlive())
             animationThread.stop();
           animationThread = new Thread(this);
           animationThread.start();
           wb.clearWb();
           return true;
         }  else return super.handleEvent(evt);
     default:
         return super.handleEvent(evt);
   }
}

Back to paper


Figure 13. The both.html File

<HTML>
<HEAD>
<TITLE>Distributed White Board</TITLE>
</HEAD>
<FRAMESET ROWS="30%, 70%">
<FRAME SRC="hello.html" NAME="hello">
<FRAME SRC="wboard.html" NAME="wboard">
</FRAMESET>
</HTML>

Back to paper


Figure 14. A Simple Flow, in Which a Server Listens for and Connects to Incoming Clients
Back to paper


Figure. 15. How Clients Interface to Each Other Through the Server

Back to paper


Figure 16. Source Code to Be Inserted into the wbServer.java File

import java.io.*;
import java.net.*;
import serverSockThread;

public class wbServer {
   static ServerSocket listener;
   static Socket c1, c2;

   public static void main(String args[]) {
      DataInputStream in;
      DataOutputStream out;
      int port = 8888;
1
      try {
         listener = new ServerSocket(port);
      } catch (IOException e) {
         System.out.println("ServerSocker failed");
         return;
      }
      while (true) {
         try {    // get a pair of client
           c1 = listener.accept();
           c2 = listener.accept();
      } catch (IOException e) {
           System.out.println("Accept failed");
           return;
      }
         serverSockThread t1 = new serverSockThread(c1,c2);
         t1.start();
         serverSockThread t2 = new serverSockThread(c1,c2);
         t2.start();
      }
   }
}

Back to paper


Figure 17. Source Code to Be Inserted into the ServerSockThread.java File

import java.awt.Color.*;
import java.io.*;
import java.net.Socket;

public class serverSockThread extends Thread {
   DataInputStream in;
   DataOutputStream out;
   Socket sIn, sOut;

    serverSockThread(Socket In, Socket Out) {
       sIn = In;
       sOut = Out;
       try {
          in = new DataInputStream(In.getInputStream());
          out = new DataOutputStream(Out.getOutputStream());
       }  catch (IOException e) {
          System.out.println(Socket Stream error");
          return;
       }
   }
   public void cleanupSock() {
       try {
          sIn.close();
       } catch (IOException e) {
       } finally {
         try }
           sOut.close();
         } catch (IOException e) {
         } finally {
                  stop();
         }
   }
}
public void run(); {
     int i;
     while (true)   {
            try {
              i = in.readInt();
              out.writeInt(i);
          } catch (Throwable e) {
              cleanupSock();
              return;
          }
     }
}  
        
Back to paper


Figure 18. Java Code for the sockThread.java Source File

import java.awt.Color;
import java.io.*;

public class sockThread extends Thread {
   wbPanel wp;
   DataInputStream in;
   sockThread(wbPanel wp, DataInputStream in) {
      this.wp = wp;
      this.in = in;
   }
   public void run() {
      int x1,y1,x2,y2,c;
      while (wb.sockAvail) {
         try {
            x1 = in.readInt();
            y1 = in.readInt();
            x2 = in.readInt();
            y2 = in.readInt();
            c = in.readInt();
         } catch (Throwable e) {
            wb.sockClose();
            return;
         }
         wb.addshape(new Shape(x1,y1,x2,y2,
                  new Color(c)),false);
         wb.repaint();
      }
   }
}

Back to paper


Figure 19. New whiteBoard Class to Replace the whiteBoard Class Defined in whiteBoard.java

import java.net.*;

public class whiteBoard extends Applet {
   sockThread sockthread=null;
   Label wbTitle = new Label("White Board",Label.CENTER);

   public void init() {
      Socket sock;
      setLayout(new BorderLayout());
      wbPanel wp = new wpPanel();
      wbTitle.setFont(new Font("TimesRoman",Font.BOLD,18));
      add("North", wbTitle);
      add("Center", wp);
      add("South",new wbControls(wp));
      try {
           sock = new Socket((getCodeBase()).getHost(),8888);
      } catch (Throwable e) {
           return;
      }
      wb.setWb(wp,sock);
      sockthread = new sockThread(wp,wb.in);
      sockthread.start();
    }
   public void destroy() {
      wb.sockClose();
      if (sockthread != null && sockthread.isAlive())
           sockthread.stop();
      System.exit(0);
   }
}


Back to paper


Figure 20. New wb Class to Replace the wb.java
import java.io.*;
import java.net.*;
public abstract class wb {
  private static wbPanel whiteBoard;
  public static Vector shapes;
  public static Socket socket;
  public static DataInputStream in;
  public static DataOutputStream out;
  public static boolean sockAvail;

  public static synchronized void setWb(wbPanel wp, Socket s) {
       shapes = new Vector();
       whiteBoard = wp;
       socket = s;
       sockAvail = true;
       try {
          in = new DataInputStream(socket.getInputStream());
          out = new DataOutputStream(socket.getOutputStream());
       } catch (IOException e) {
          return;
       }
     }
   public static synchronized void clearWb() {
       shapes = new Vector();
       whiteBoard.repaint();
     }
   public static synchronized void sockClose() {
       try {
          socket.close();
       } catch (IOException e) {
       } finally {
          sockAvail = false;
       }
     }
   public static syncronized void addShape(Shape s, boolean local){
       shapes.addElement(s);  // add shape to local whiteBoard

         if (local == true && sockAvail) {
          try {  // send shape to partner whiteBoard
             out.writeInt(s.x1);
             out.writeInt(s.y1);
             out.writeInt(s.x2);
             out.writeInt(s.y2);
             out.writeInt(s.c.getRGB());
             } catch (IOException e) {
                wb.sockClose();
                return;
             }
       }
  }
}          

Back to paper


Figure 21. Steps for Creating and Linking a Native Method on AIX

Back to paper


Figure 22.1. Contents of a makefile

JAVA_HOME = /user/lpp/Java
NATIVE = getService_byName
STUB = getService
CFLAGS=-I$(JAVA_HOME)/include/aix_pt -I$(JAVA_HOME)/include
LIB = libgetsvc.so
IMP = -bI:/usr/lpp/Java/include/java.exp
EXP = -bE:getService.exp
LDFLAGS = -bM:SRE - bnoentry -lc_r /usr/lib/libc.a

$(LIB):  $(STUB).o $(NATIVE).o
   ld $(IMP) $(EXP) $(LDFLAGS) -o $(LIB) $(STUB.o $(NATIVE).o
   mv $(LIB) $(JAVA_HOME)/lib/aix_pt

$(STUB).o: $(STUB).c $(STUB).class
   xlc_r $(CFLAGS) -c $(STUB).c

$(NATIVE).o: $(NATIVE).c $(STUB).class
   xlc_r $(CFLAGS) -c $(NATIVE).c

$(STUB).h: $(STUB).class
   javah $(STUB)

$(STUB).c: $(STUB).class
   javah -stubs $(STUB)

$(STUB).class: $(STUB).java
   javac $(STUB).java
   javah $(STUB)

clean:
   rm $(JAVA_HOME)/lib/aix_pt/$(LIB) $(STUB).class $(STUB).h \
   $(STUB).c $(STUB).o $(NATIVE).o $(LIB) 
        
Back to paper


Figure 22.2. Contents of a makefile

JAVA_HOME = /user/lpp/Java
NATIVE = getService_byName
STUB = getService
CFLAGS=-I$(JAVA_HOME)/include/aix_pt -I$(JAVA_HOME)/include
LIB = libgetsvc.so
IMP = -bI:/usr/lpp/Java/include/java.exp
EXP = -bE:getService.exp
LDFLAGS = -bM:SRE - bnoentry -lc_r /usr/lib/libc.a

$(LIB):  $(STUB).o $(NATIVE).o
   ld $(IMP) $(EXP) $(LDFLAGS) -o $(LIB) $(STUB.o $(NATIVE).o
   mv $(LIB) $(JAVA_HOME)/lib/aix_pt

$(STUB).o: $(STUB).c $(STUB).class
   xlc_r $(CFLAGS) -c $(STUB).c

$(NATIVE).o: $(NATIVE).c $(STUB).class
   xlc_r $(CFLAGS) -c $(NATIVE).c

$(STUB).h: $(STUB).class
   javah $(STUB)

$(STUB).c: $(STUB).class
   javah -stubs $(STUB)

$(STUB).class: $(STUB).java
   javac $(STUB).java
   javah $(STUB)

clean:
   rm $(JAVA_HOME)/lib/aix_pt/$(LIB) $(STUB).class $(STUB).h \
   $(STUB).c $(STUB).o $(NATIVE).o $(LIB) 
        
Back to paper




JavaTM is a trademark of Sun Microsystems, Inc.

Other companies, products, and service names may be trademarks or service marks of others.

Copyright    Trademark