Assigment 7 : Make 3D Object with Java Programming


Java 3D is a scene graph based 3D application programming interface (API) for the Java platform. It runs atop either OpenGL or Direct3D. Since version 1.2, Java 3D has been developed under the Java Community Process. A Java 3D scene graph is a directed acyclic graph (DAG).
Compared to other solutions, Java 3D is not only a wrapper around these graphics APIs, but an interface that encapsulates the graphics programming using a true object-oriented approach. Here a scene is constructed using a scene graph that is a representation of the objects that have to be shown. This scene graph is structured as a tree containing several elements that are necessary to display the objects. Additionally, Java 3D offers extensive spatialized sound support.

in this section , i show you about my computer graphic project., this model about Watercraft made with java, and you can download or view here :

[spoiler title=”lukisan 3d kapal”]

package tugas7;

import java.applet.Applet;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Box;

import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.geometry.Triangulator;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.Frame;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Shape3D;
import javax.vecmath.Point3f;
import javax.vecmath.Point4f;

/**
*
* @author Haris
*/
public class lukisan_kapal_3d extends Applet {

// Deklarasi Nama Animasi
TransformGroup pusat;
TransformGroup t_dek;
TransformGroup meriam;
TransformGroup cerobong;
TransformGroup lambungdepan;
TransformGroup matahari;
Transform3D Trans = new Transform3D();
Vector3f Vector = new Vector3f();
Cylinder Silinder;
Sphere lingkaran1;
Sphere lingkaran2;
Cone lambd;
Box lambung, dek, cb1, cb2, cb3;

Transform3D transform(float x, float y, float z) {
Vector.set(x, y, z);
Trans.set(Vector);
return Trans;
}

Appearance apr() {
// Membuat Warna
Color3f Red = new Color3f(0.0f, 0.9f, 0.0f);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(0.0f, 0.0f, 0.0f);
Material material = new Material(Red, black, Red, white, 64);
Appearance appearance = new Appearance();
appearance.setMaterial(material);
// appearance.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_LINE,PolygonAttributes.CULL_BACK,0.0f));
return appearance;
}

Appearance apr(float x, float y, float z) {
// Membuat Warna
Color3f Red = new Color3f(x, y, z);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(0.0f, 0.0f, 0.0f);
Material material = new Material(Red, black, Red, white, 64);
Appearance appearance = new Appearance();
appearance.setMaterial(material);
// appearance.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_LINE,PolygonAttributes.CULL_BACK,0.0f));
return appearance;
}

// Inti Program
void GambarOrang() {
pusat = new TransformGroup();
pusat.setTransform(transform(0.0f, 0f, 0.0f));

lambung();
dek_depan();
dek_belakang();
cerobong(-0.5f, 0.5f, 0f);
cerobong(-0.3f, 0.5f, 0f);
cerobong(-0.1f, 0.5f, 0f);

meriam(0.25f, 0.35f, 0.20f);
meriam(0.25f, 0.35f, -0.20f);
meriam(0.0f, 0.35f, 0.20f);
meriam(0.0f, 0.35f, -0.20f);
meriam(0.5f, 0.35f, 0.20f);
meriam(0.5f, 0.35f, -0.20f);
meriam(-0.25f, 0.35f, 0.20f);
meriam(-0.25f, 0.35f, -0.20f);
meriam(-0.5f, 0.35f, 0.20f);
meriam(-0.5f, 0.35f, -0.20f);

t_dek = new TransformGroup();
t_dek.setTransform(transform(0.0f, 0.35f, 0.0f));
dek = new Box(0.8f, 0.15f, 0.3f, apr());
t_dek.addChild(dek);
pusat.addChild(t_dek);

// Membuat Badan
matahari = new TransformGroup();
matahari.setTransform(transform(2f, 2.900f, 0.0f));
lingkaran1 = new Sphere(0.70f, 1, 500, apr());
matahari.addChild(lingkaran1);
pusat.addChild(matahari);

}

public void komponenbox(TransformGroup pusat, float x, float y, float z, Box box) {
TransformGroup a = new TransformGroup();
a.setTransform(transform(x, y, z));
a.addChild(box);
pusat.addChild(a);

}

public void meriam(float x, float y, float z) {
meriam = new TransformGroup();
meriam.setTransform(transform(x, y, z));
lingkaran2 = new Sphere(0.15f, 1, 500, apr(1.2f, 0f, 0f));
meriam.addChild(lingkaran2);
pusat.addChild(meriam);
}

public void cerobong(float x, float y, float z) {
cerobong = new TransformGroup();
cerobong.setTransform(transform(x, y, z));
Silinder = new Cylinder(0.1f, 0.9f, 1, 500, 1, apr());
cerobong.addChild(Silinder);
pusat.addChild(cerobong);
}

public void lambung() {
TransformGroup xccc1 = new TransformGroup();
Vector.set(0.0f, 1.7f, 0.7f);
Trans.set(Vector);
xccc1.setTransform(Trans);
Point3f[] lambunge = {
new Point3f(0.9f, -1.5f, -0.2f),
new Point3f(-0.9f, -1.5f, -0.2f),
new Point3f(-0.9f, -2f, -0.7f),
new Point3f(0.9f, -2f, -0.7f),
new Point3f(0.9f, -1.5f, -1.2f),
new Point3f(-0.9f, -1.5f, -1.2f)
};
//The four faces (triangles) of the tetrahedron.
int titik_lambung[] = {
0, 1, 2, 3,
2, 5, 4, 3,
1, 0, 4, 5
};
GeometryInfo gii = new GeometryInfo(GeometryInfo.QUAD_ARRAY);
gii.setCoordinates(lambunge);
gii.setCoordinateIndices(titik_lambung);
NormalGenerator ngr = new NormalGenerator();
ngr.generateNormals(gii);
GeometryArray ga = gii.getGeometryArray();

//Generate the tetrahedron as a Shape.
Shape3D lambung_kapal = new Shape3D(ga, apr(0.9f, 0.9f, 0.0f));
xccc1.addChild(lambung_kapal);
pusat.addChild(xccc1);
}

public void dek_depan( ) {
TransformGroup xccc = new TransformGroup();

xccc.setTransform(transform(0.0f, 1.7f, 0.7f));

Point3f[] tetrahedronCoordinates = {
new Point3f(0.9f, -1.5f, -0.2f),
new Point3f(2f, -1.5f, -0.7f),
new Point3f(0.9f, -1.5f, -1.2f),
new Point3f(0.9f, -2f, -0.7f)
};
//The four faces (triangles) of the tetrahedron.
int coordIndices[] = {
3, 1, 0,
0, 2, 3,
0, 1, 2,
1, 3, 2
};
GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
gi.setCoordinates(tetrahedronCoordinates);
gi.setCoordinateIndices(coordIndices);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(gi);
GeometryArray te = gi.getGeometryArray();

//Generate the tetrahedron as a Shape.
Shape3D tetrahedron = new Shape3D(te, apr(0.5f, 0.2f, 0.2f));
xccc.addChild(tetrahedron);

pusat.addChild(xccc);
}
public void dek_belakang( ) {
TransformGroup xccc = new TransformGroup();

xccc.setTransform(transform(0.0f, 1.7f, 0.7f));

Point3f[] tetrahedronCoordinates = {
new Point3f(-0.9f, -1.5f, -0.2f),
new Point3f(-1.5f, -1.5f, -0.7f),
new Point3f(-0.9f, -1.5f, -1.2f),
new Point3f(-0.9f, -2f, -0.7f)
};
//The four faces (triangles) of the tetrahedron.
int coordIndices[] = {
0,1,3,
3,2,0,
2,1,0,
2,3,1
};
GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
gi.setCoordinates(tetrahedronCoordinates);
gi.setCoordinateIndices(coordIndices);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(gi);
GeometryArray te = gi.getGeometryArray();

//Generate the tetrahedron as a Shape.
Shape3D tetrahedron = new Shape3D(te, apr(0.2f, 0.3f, 0.2f));
xccc.addChild(tetrahedron);

pusat.addChild(xccc);
}

BranchGroup createSceneGraph() {
// Membuat Putaran
BranchGroup objRoot = new BranchGroup();

// Membuat TransformGroup
TransformGroup objScale = new TransformGroup();
Transform3D scaleTrans = new Transform3D();
scaleTrans.set(1 / 3.5f);
objScale.setTransform(scaleTrans);
objRoot.addChild(objScale);

// Membuat TransformGroup dan initialize
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objScale.addChild(objTrans);

// Menambahkan Method Gambar Orang
GambarOrang();
objTrans.addChild(pusat);
BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);

Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f));
bg.setApplicationBounds(bounds);
objTrans.addChild(bg);

// Mengatur Mouse
MouseRotate mr = new MouseRotate();
mr.setTransformGroup(objTrans);
mr.setSchedulingBounds(bounds);
mr.setFactor(0.007);
objTrans.addChild(mr);

// Mengatur Terang
Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objRoot.addChild(ambientLightNode);
Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f);
Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f);

DirectionalLight light1 = new DirectionalLight(light1Color,
light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);

return objRoot;
}

public lukisan_kapal_3d() {
setLayout(new GridLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
add("center", canvas3D);
BranchGroup scene = createSceneGraph();
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public static void main(String[] args) {
Frame frame = new MainFrame(new lukisan_kapal_3d(), 1200, 600);
frame.setLocationRelativeTo(null);
}
}

[/spoiler]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s