HHD Site
 
 

 

 

Health and Human Development Home
  Dept of Kinesiology
 

 

HOW ARE GRAPHIC OBJECTS DISPLAYED IN JAVA?


Objective:    Show a simple anatomical representation. 

Overview:


Source code:

import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.URL;

// APPLET TO PUT 3D MODEL INTO PAGE - FROM WIREFRAME DEMO //
public class ThreeD extends Applet implements Runnable {

// INITIALIZATION //
public void init() {
 mdname = getParameter("model");
 try {
     scalefudge = Float.valueOf(getParameter("scale")).floatValue();
 }
 resize(size().width <= 20 ? 400 : size().width,
        size().height <= 20 ? 400 : size().height);
}

// READ INPUT STREAM AS SPECIFIED IN HTML DOC //
public void run() {
 InputStream is = null;
 try {
     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
     is = new URL(getDocumentBase(), mdname).openStream();
     Model3D m = new Model3D (is);
     md = m;
     float xw = m.xmax - m.xmin;
     float yw = m.ymax - m.ymin;
     float zw = m.zmax - m.zmin;
     float f1 = size().width / xw;
     float f2 = size().height / xw;
     xfac = 0.7f * (f1 < f2 ? f1 : f2) * scalefudge;
     }
 repaint();
}

// CREATE 3D MODEL BY PARSING INPUT STREAM //
Model3D (InputStream is) throws IOException, FileFormatException {
 this();
 StreamTokenizer st = new StreamTokenizer(is);
    scan:
 while (true) {
     switch (st.nextToken()) {
       default:
  break scan;
       case StreamTokenizer.TT_EOL:
  break;
       case StreamTokenizer.TT_WORD:
  if ("v".equals(st.sval)) {
      double x = 0, y = 0, z = 0;
      if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
   x = st.nval;
   if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
       y = st.nval;
       if (st.nextToken() == StreamTokenizer.TT_NUMBER)
    z = st.nval;
   }
      }
      addVert((float) x, (float) y, (float) z);
}

// ADD LINE FROM VERTEX P1 TO VERTEX P2 //
    void add(int p1, int p2) {
 int i = ncon;
 if (i >= maxcon)
     if (con == null) {
  maxcon = 100;
  con = new int[maxcon];
     } else {
  maxcon *= 2;
  int nv[] = new int[maxcon];
  System.arraycopy(con, 0, nv, 0, con.length);
  con = nv;
     }
 if (p1 > p2) {
     int t = p1;
     p1 = p2;
     p2 = t;
 }
 con[i] = (p1 << 16) | p2;
 ncon = i + 1;
    }

// PAINT MODEL TO GRAPHICS CONTEXT //
    static Color gr[];

void paint(Graphics g) {
 if (vert == null || nvert <= 0)
     return;
 transform();
 if (gr == null) {
     gr = new Color[16];
     for (int i = 0; i < 16; i++) {
  int grey = (int) (170*(1-Math.pow(i/15.0, 2.3)));
  gr[i] = new Color(grey, grey, grey);
     }
 }
  g.setColor(gr[grey]);
  g.drawLine(v[p1], v[p1 + 1],v[p2], v[p2 + 1]);
  }
}



HTML code:
<html>
<body>
<applet code=ThreeD.class width=300 height=400>
<param name=model1 value=femur3.txt>
<param name=model2 value=Limits2.txt>
<param name=scale value="1">
</applet>
</html>
</body> 
Java Output:

Instructions:
Drag or click the mouse to change the orientation of the femur model.  


Return to Welcome Page