Java3D

I used the connect four game code which can be downloaded from Free Java Applet.


  1. Objective
  2. Source
  3. Details
  4. Future Work
  5. Related Websites
How to play:
  • Click 1-7 button or clear button
  • Hold down the left mouse button and drag to rotate objects
  • Hold down the middle mouse button and drag to scale objects
  • Hold down the right mouse button and drag to translate objects

To learn java3d implementations.


Java Source Connect4.java
HTML Source Connect4.html
Necessary Files Connect4.class
Necessary Files Connect4Kernel.class
Necessary Files Connect4$ActionAdapter.class



Mouse Utility
  • Left button - Rotate objects
  • Right button - Translate objects
  • Middle button - Zoom in/out objects
     15:   import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
     16:   import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
     17:   import com.sun.j3d.utils.behaviors.mouse.MouseZoom;

    707:   private void setContent()
    708:   {

    711:      contentsTransGr = new TransformGroup();
    712:      contentsTransGr.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    713:      contentsTransGr.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    717:      // Mouse
    718:      MouseRotate rotator = new MouseRotate(contentsTransGr);
    719:      rotator.setSchedulingBounds(bounds);
    720:      contentBranch.addChild(rotator);
    721:      MouseTranslate translator = new MouseTranslate(contentsTransGr);
    722:      translator.setSchedulingBounds(bounds);
    723:      contentBranch.addChild(translator);
    724:      MouseZoom zoomer = new MouseZoom(contentsTransGr);
    725:      zoomer.setSchedulingBounds(bounds);
    726:      contentBranch.addChild(zoomer);

   1046:   }
   
3D Text
    707:   private void setContent()
    708:   {

    892:      // font
    893:      Font font = new Font("TimesRoman", Font.PLAIN, 1);
    894:      Font3D font3D = new Font3D(font, new FontExtrusion());
    895:      Text3D text3D = new Text3D(font3D, "1  2  3  4  5  6  7");
    896:      Appearance app = new Appearance();
    897:      Material material = new Material();
    898:      material.setAmbientColor(new Color3f(1.0f,0.7f,0.0f));
    899:      material.setDiffuseColor(new Color3f(1.0f,0.7f,0.0f));
    900:      app.setMaterial(material);
    901:      Shape3D shape3 = new Shape3D(text3D, app);
    902:      Transform3D t3 = new Transform3D();
    903:      t3.set(new Vector3f(-3.2f, 3.2f, 0.0f));
    904:      TransformGroup trans3 = new TransformGroup(t3);
    905:      trans3.addChild(shape3);
    906:      contentsTransGr.addChild(trans3);

   1046:   }
   
2D Text
    482:   private Text2D text2D;

    564:   public void DrawStatus(String str)
    565:   {
    566:      text2D.setString(str);
    568:   }

    707:   private void setContent()
    708:   {

   1023:      // font
   1024:      TransformGroup trans5 = new TransformGroup();
   1025:      Transform3D t5 = new Transform3D();
   1026:      t5.setScale(5.0f);
   1027:      t5.setTranslation(new Vector3f(-3.0f, -4.0f, 0.0f));
   1028:      trans5.setTransform(t5);
   1029:      text2D = new Text2D("Start your game!  ", new Color3f(1.0f,1.0f,1.0f),"MS Gothic",25,Font.BOLD);
   1030:      text2D.setCapability(Text2D.ALLOW_APPEARANCE_READ);
   1031:      text2D.setCapability(Text2D.ALLOW_APPEARANCE_WRITE);
   1032:      Appearance app5 = text2D.getAppearance();
   1033:      app5.setCapability(Appearance.ALLOW_TEXTURE_READ);
   1034:      app5.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
   1035:      Texture texture5 = app5.getTexture();
   1036:      texture5.setCapability(Texture.ALLOW_IMAGE_READ);
   1037:      texture5.setCapability(Texture.ALLOW_IMAGE_WRITE);
   1038:      trans5.addChild(text2D);
   1039:      contentsTransGr.addChild(trans5);

   1046:   }
   
3D Geometry
  • IndexedTriangleArray - Frame and Disc
    707:   private void setContent()
    708:   {

    728:      // Frame
    729:      Point3d[] vertices = new Point3d[4*2+32*2];
    730:      vertices[0] = new Point3d( 0.5, 0.5, 0.07);
    731:      vertices[1] = new Point3d(-0.5, 0.5, 0.07);
    732:      vertices[2] = new Point3d(-0.5,-0.5, 0.07);
    733:      vertices[3] = new Point3d( 0.5,-0.5, 0.07);
    734:      vertices[4] = new Point3d( 0.5, 0.5,-0.07);
    735:      vertices[5] = new Point3d(-0.5, 0.5,-0.07);
    736:      vertices[6] = new Point3d(-0.5,-0.5,-0.07);
    737:      vertices[7] = new Point3d( 0.5,-0.5,-0.07);
    738:
    739:      double x, y, z, theta;
    740:      theta = 2.0*Math.PI/32.0;
    741:      for (int i=0; i<2; i++) {
    742:         if (i == 0)
    743:            z = 0.04;
    744:         else
    745:            z = -0.04;
    746:         for (int j=0; j<32; j++) {
    747:            x = 0.4*Math.cos(theta*j);
    748:            y = 0.4*Math.sin(theta*j);
    749:            vertices[8+i*32+j]  = new Point3d(x, y, z);
    750:         }
    751:      }
    752:
    753:      int[] indices = new int[3*4*2+3*8*4*2+3*32*2];
    754:      int index = 0;
    755:      // front face
    756:      for (int j=0; j<4; j++) {
    757:         indices[index++] = j;
    758:         if (j != 3) {
    759:            indices[index++] = j+1;
    760:            indices[index++] = 16+8*j;
    761:         }
    762:         else {
    763:            indices[index++] = 0;
    764:            indices[index++] = 8;
    765:         }
    766:         for (int k=0; k<8; k++) {
    767:            indices[index++] = 8+8*j+k;
    768:            indices[index++] = j;
    769:            if (j != 3 || k != 7)
    770:               indices[index++] = 8+8*j+k+1;
    771:            else
    772:               indices[index++] = 8;
    773:         }
    774:      }
    775:      // rear face
    776:      for (int j=0; j<4; j++) {
    777:         indices[index++] = 4+j;
    778:         if (j != 3) {
    779:            indices[index++] = 48+8*j;
    780:            indices[index++] = 4+j+1;
    781:         }
    782:         else {
    783:            indices[index++] = 40;
    784:            indices[index++] = 4;
    785:         }
    786:         for (int k=0; k<8; k++) {
    787:            indices[index++] = 4+j;
    788:            indices[index++] = 40+8*j+k;
    789:            if (j != 3 || k != 7)
    790:               indices[index++] = 40+8*j+k+1;
    791:            else
    792:               indices[index++] = 40;
    793:         }
    794:      }
    795:      // inside
    796:      for (int i=0; i<32; i++) {
    797:         indices[index++] = 8+i;
    798:         if (i != 31)
    799:            indices[index++] = 8+i+1;
    800:         else
    801:            indices[index++] = 8;
    802:         indices[index++] = 40+i;
    803:         if (i != 31) {
    804:            indices[index++] = 8+i+1;
    805:            indices[index++] = 40+i+1;
    806:         }
    807:         else {
    808:            indices[index++] = 8;
    809:            indices[index++] = 40;
    810:         }
    811:         indices[index++] = 40+i;
    812:      }
    813:
    814:      Color3f[] colors = { new Color3f(1.0f,0.0f,0.0f),   // red
    815:                           new Color3f(0.0f,1.0f,0.0f),   // green
    816:                           new Color3f(0.0f,0.0f,1.0f),   // blue
    817:                           new Color3f(0.0f,1.0f,1.0f),   // cyan
    818:                           new Color3f(1.0f,0.0f,1.0f),   // magenta
    819:                           new Color3f(1.0f,1.0f,0.0f),   // yellow
    820:                           new Color3f(0.9f,0.0f,0.0f),   // dark red
    821:                           new Color3f(0.9f,0.9f,0.0f) }; // dark yellow
    822:
    823:      int[] colorIndices = new int[3*4*2+3*8*4*2+3*32*2];
    824:      index = 0;
    825:      for (int i=0; i<3*4*2+3*8*4*2; i++)
    826:         colorIndices[index++] = 2;
    827:      for (int i=0; i<3*32*2; i++)
    828:         colorIndices[index++] = 1;
    829:
    830:      IndexedTriangleArray geometry = new IndexedTriangleArray(vertices.length,
    831:               GeometryArray.COORDINATES | GeometryArray.COLOR_3,
    832:               indices.length);
    833:
    834:      geometry.setCoordinates(0, vertices);
    835:      geometry.setCoordinateIndices(0, indices);
    836:      geometry.setColors(0, colors);
    837:      geometry.setColorIndices(0, colorIndices);
    838:
    839:      //
    840:      for (int i=0; i<6; i++) {
    841:         for (int j=0; j<7; j++) {
    842:            Shape3D shape = new Shape3D(geometry);
    843:            Transform3D t2 = new Transform3D();
    844:            t2.set(new Vector3f(-3.0f+1.0f*j,-2.5f+1.0f*i,0.0f));
    845:            TransformGroup trans2 = new TransformGroup(t2);
    846:            trans2.addChild(shape);
    847:            contentsTransGr.addChild(trans2);
    848:         }
    849:      }
    850:
    851:      // Frame : edge
    852:      Point3d[] vertices2 = new Point3d[8];
    853:      vertices2[0] = new Point3d(-3.5, 3.0, 0.07);
    854:      vertices2[1] = new Point3d(-3.5,-3.0, 0.07);
    855:      vertices2[2] = new Point3d( 3.5,-3.0, 0.07);
    856:      vertices2[3] = new Point3d( 3.5, 3.0, 0.07);
    857:      vertices2[4] = new Point3d(-3.5, 3.0,-0.07);
    858:      vertices2[5] = new Point3d(-3.5,-3.0,-0.07);
    859:      vertices2[6] = new Point3d( 3.5,-3.0,-0.07);
    860:      vertices2[7] = new Point3d( 3.5, 3.0,-0.07);
    861:
    862:      int[] indices2 = { 0,4,5,
    863:                         0,5,1,
    864:                         1,5,6,
    865:                         1,6,2,
    866:                         2,6,7,
    867:                         2,7,3,
    868:                         3,7,4,
    869:                         3,4,0 };
    870:
    871:      int[] colorIndices2 = { 2,2,2,
    872:                              2,2,2,
    873:                              1,1,1,
    874:                              1,1,1,
    875:                              2,2,2,
    876:                              2,2,2,
    877:                              1,1,1,
    878:                              1,1,1 };
    879:
    880:      IndexedTriangleArray geometry2 = new IndexedTriangleArray(vertices2.length,
    881:               GeometryArray.COORDINATES | GeometryArray.COLOR_3,
    882:               indices2.length);
    883:
    884:      geometry2.setCoordinates(0, vertices2);
    885:      geometry2.setCoordinateIndices(0, indices2);
    886:      geometry2.setColors(0, colors);
    887:      geometry2.setColorIndices(0, colorIndices2);
    888:      Shape3D shape2 = new Shape3D(geometry2);
    889:
    890:      contentsTransGr.addChild(shape2);

    908:      // Disc
    909:      Point3d[] vertices4 = new Point3d[32*2];
    910:      for (int i=0; i<2; i++) {
    911:         if (i == 0)
    912:            z = 0.1;
    913:         else
    914:            z = -0.1;
    915:         for (int j=0; j<32; j++) {
    916:            x = 0.4*Math.cos(theta*j);
    917:            y = 0.4*Math.sin(theta*j);
    918:            vertices4[32*i+j] = new Point3d(x, y, z);
    919:         }
    920:      }
    921:      int[] indices4 = new int[3*15*2*2+3*32*2];
    922:      index = 0;
    923:      // front
    924:      for (int i=0; i<32; i++) {
    925:         if (i == 15 || i == 31)
    926:            continue;
    927:         indices4[index++] = i;
    928:         indices4[index++] = i+1;
    929:         if (i < 16)
    930:            indices4[index++] = 16;
    931:         else
    932:            indices4[index++] = 0;
    933:      }
    934:      // rear
    935:      for (int i=0; i<32; i++) {
    936:         if (i == 15 || i == 31)
    937:            continue;
    938:         indices4[index++] = 32+i;
    939:         if (i < 16)
    940:            indices4[index++] = 48;
    941:         else
    942:            indices4[index++] = 32;
    943:         indices4[index++] = 32+i+1;
    944:      }
    945:      // side
    946:      for (int i=0; i<32; i++) {
    947:         indices4[index++] = i;
    948:         indices4[index++] = 32+i;
    949:         if (i != 31)
    950:            indices4[index++] = i+1;
    951:         else
    952:            indices4[index++] = 0;
    953:         indices4[index++] = 32+i;
    954:         if (i != 31) {
    955:            indices4[index++] = 32+i+1;
    956:            indices4[index++] = i+1;
    957:         }
    958:         else {
    959:            indices4[index++] = 32;
    960:            indices4[index++] = 0;
    961:         }
    962:      }
    963:
    964:      // disc 1 : red
    965:      int[] colorIndices4a = new int[3*15*2*2+3*32*2];
    966:      index = 0;
    967:      for (int i=0; i<3*15*2*2; i++)
    968:         colorIndices4a[index++] = 0;
    969:      for (int i=0; i<3*32*2; i++)
    970:         colorIndices4a[index++] = 6;
    971:
    972:      IndexedTriangleArray geometry4a = new IndexedTriangleArray(vertices4.length,
    973:               GeometryArray.COORDINATES | GeometryArray.COLOR_3,
    974:               indices4.length);
    975:      geometry4a.setCoordinates(0, vertices4);
    976:      geometry4a.setCoordinateIndices(0, indices4);
    977:      geometry4a.setColors(0, colors);
    978:      geometry4a.setColorIndices(0, colorIndices4a);
    979:
    980:      for (int i=0; i<21; i++) {
    981:         Shape3D shape4a = new Shape3D(geometry4a);
    982:         Transform3D t4 = new Transform3D();
    983:         t4.set(new Vector3f(-4.0f,-3.0f+0.2f*i,0.0f));
    984:         Transform3D tmp = new Transform3D();
    985:         tmp.set(new AxisAngle4d(1.0,0.0,0.0, Math.PI/2.0));
    986:         t4.mul(tmp);
    987:         trans4a[i] = new TransformGroup(t4);
    988:         trans4a[i].setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    989:         trans4a[i].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    990:         trans4a[i].addChild(shape4a);
    991:         contentsTransGr.addChild(trans4a[i]);
    992:      }
    993:
    994:      // disc 2 : yellow
    995:      int[] colorIndices4b = new int[3*15*2*2+3*32*2];
    996:      index = 0;
    997:      for (int i=0; i<3*15*2*2; i++)
    998:         colorIndices4b[index++] = 5;
    999:      for (int i=0; i<3*32*2; i++)
   1000:         colorIndices4b[index++] = 7;
   1001:
   1002:      IndexedTriangleArray geometry4b = new IndexedTriangleArray(vertices4.length,
   1003:               GeometryArray.COORDINATES | GeometryArray.COLOR_3,
   1004:               indices4.length);
   1005:      geometry4b.setCoordinates(0, vertices4);
   1006:      geometry4b.setCoordinateIndices(0, indices4);
   1007:      geometry4b.setColors(0, colors);
   1008:      geometry4b.setColorIndices(0, colorIndices4b);
   1009:      for (int i=0; i<21; i++) {
   1010:         Shape3D shape4b = new Shape3D(geometry4b);
   1011:         Transform3D t4 = new Transform3D();
   1012:         t4.set(new Vector3f(4.0f,-3.0f+0.2f*i,0.0f));
   1013:         Transform3D tmp = new Transform3D();
   1014:         tmp.set(new AxisAngle4d(1.0,0.0,0.0, Math.PI/2.0));
   1015:         t4.mul(tmp);
   1016:         trans4b[i] = new TransformGroup(t4);
   1017:         trans4b[i].setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
   1018:         trans4b[i].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
   1019:         trans4b[i].addChild(shape4b);
   1020:         contentsTransGr.addChild(trans4b[i]);
   1021:      }

   1046:   }
   


Write own algorithm.