Animasi 3D kapal laut with Java3D

setelah membuat projek animasi 2d menggunakan fungsi thread kini aku mengembangkan nya ke dalam bentuk 3 dimensi,, tapi ini cuma awal,, dari pembuatan objek 3dimensinya ..

Hasil Run Program :progress 5

source code dibawah ini :

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;

public class Lukisan extends Applet {

// Deklarasi Nama Animasi
TransformGroup Badan;
TransformGroup t_dek;
TransformGroup t_dek2;
TransformGroup t_dek3;
TransformGroup jendelakapal;
TransformGroup pagar;
TransformGroup pagar1;
TransformGroup pagar2;
TransformGroup pagar3;
TransformGroup pagar4;
TransformGroup pagar5;
TransformGroup pagar6;
TransformGroup pagar7;
TransformGroup pagar8;
TransformGroup pagar8a;
TransformGroup pagar8b;
TransformGroup pagar8c;
TransformGroup pagar8d;
TransformGroup pagar8e;
TransformGroup pagar9a;
TransformGroup pagar9b;
TransformGroup pagar9c;
TransformGroup pagar9d;
TransformGroup pagar9e;
TransformGroup pagar9;
TransformGroup benderamerah;
TransformGroup benderaputih;
TransformGroup tiang;
TransformGroup cerobong;
TransformGroup lambungdepan;
Transform3D Trans = new Transform3D();
Vector3f Vector = new Vector3f();
Cylinder Silinder,Silinder1;
Sphere tiangg2;
Cone tiangg;
Box jendela, Radar, tengah, tiangg1, cb3;

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

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);
return appearance;
}

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

lambung();
jendela(0.0f, 0.35f, 0.35f);
jendela(0.2f, 0.35f, 0.35f);
jendela(0.4f, 0.35f, 0.35f);
jendela(-0.2f, 0.35f, 0.35f);
jendela(-0.4f, 0.35f, 0.35f);
jendela(0.6f, 0.35f, 0.35f);
jendela(-0.6f, 0.35f, 0.35f);
jendela(0.0f, 0.35f, -0.35f);
jendela(0.2f, 0.35f, -0.35f);
jendela(0.4f, 0.35f, -0.35f);
jendela(-0.2f, 0.35f, -0.35f);
jendela(-0.4f, 0.35f, -0.35f);
jendela(0.6f, 0.35f, -0.35f);
jendela(-0.6f, 0.35f, -0.35f);
Radar1(1.36f, 0.22f, 0.0f);
badan_depan();
badan_belakang();
cerobong(-0.1f, 0.5f, 0f);
cerobong2(-0.1f, 0.5f, 0f);
cerobong3(-0.1f, 0.5f, 0f);
lingkaran1();
lingkaran2();
lingkaran3();
lingkaran4();
t_dek = new TransformGroup();
t_dek.setTransform(transform(0.0f, 0.3f, 0.0f));
tengah = new Box(0.8f, 0.18f, 0.35f, apr(0.0f, 0.0f, 8.0f));
t_dek.addChild(tengah);
Badan.addChild(t_dek);
//
t_dek2 = new TransformGroup();
t_dek2.setTransform(transform(0.0f, 0.3f, 0.0f));
tengah = new Box(0.60f, 0.28f, 0.30f, apr(0.5f, 0.5f, 0.5f));
t_dek2.addChild(tengah);
Badan.addChild(t_dek2);

t_dek3 = new TransformGroup();
t_dek3.setTransform(transform(0.0f, 0.2f, 0.0f));
tengah = new Box(0.82f, 0.03f, 0.36f, apr(0.9f, 0.9f, 0.0f));
t_dek3.addChild(tengah);
Badan.addChild(t_dek3);

jendelakapal = new TransformGroup();
jendelakapal.setTransform(transform(0.80f, 0.35f, 0.0f));
tengah = new Box(0.01f, 0.10f, 0.30f, apr(1.0f, 1.0f, 1.0f));
jendelakapal.addChild(tengah);
Badan.addChild(jendelakapal);

pagar = new TransformGroup();
pagar.setTransform(transform(0.99f, 0.26f, 0.0f));//sisi
tengah = new Box(0.01f, 0.01f, 0.32f, apr(0.0f, 0.0f, 0.0f));
pagar.addChild(tengah);
Badan.addChild(pagar);

pagar1 = new TransformGroup();
pagar1.setTransform(transform(0.99f, 0.23f, 0.0f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar1.addChild(tengah);
Badan.addChild(pagar1);

pagar2 = new TransformGroup();
pagar2.setTransform(transform(0.99f, 0.23f, 0.3f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar2.addChild(tengah);
Badan.addChild(pagar2);

pagar3 = new TransformGroup();
pagar3.setTransform(transform(0.99f, 0.23f, -0.3f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar3.addChild(tengah);
Badan.addChild(pagar3);

//pagar belakang
pagar4 = new TransformGroup();
pagar4.setTransform(transform(-0.92f, 0.26f, 0.0f));//sisi
tengah = new Box(0.01f, 0.01f, 0.32f, apr(0.0f, 0.0f, 0.0f));
pagar4.addChild(tengah);
Badan.addChild(pagar4);

pagar5 = new TransformGroup();
pagar5.setTransform(transform(-0.92f, 0.23f, 0.0f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar5.addChild(tengah);
Badan.addChild(pagar5);

pagar6 = new TransformGroup();
pagar6.setTransform(transform(-0.92f, 0.23f, 0.3f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar6.addChild(tengah);
Badan.addChild(pagar6);

pagar7 = new TransformGroup();
pagar7.setTransform(transform(-0.92f, 0.23f, -0.3f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar7.addChild(tengah);
Badan.addChild(pagar7);
//

//pagar samping
pagar8 = new TransformGroup();
pagar8.setTransform(transform(0.0f, 0.25f, -0.45f));
tengah = new Box(0.80f, 0.01f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar8.addChild(tengah);
Badan.addChild(pagar8);

pagar8a = new TransformGroup();
pagar8a.setTransform(transform(0.0f, 0.23f, -0.45f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar8a.addChild(tengah);
Badan.addChild(pagar8a);

pagar8b = new TransformGroup();
pagar8b.setTransform(transform(0.4f, 0.23f, -0.45f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar8b.addChild(tengah);
Badan.addChild(pagar8b);

pagar8c = new TransformGroup();
pagar8c.setTransform(transform(-0.4f, 0.23f, -0.45f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar8c.addChild(tengah);
Badan.addChild(pagar8c);

pagar8d = new TransformGroup();
pagar8d.setTransform(transform(0.79f, 0.23f, -0.45f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar8d.addChild(tengah);
Badan.addChild(pagar8d);

pagar8e = new TransformGroup();
pagar8e.setTransform(transform(-0.79f, 0.23f, -0.45f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar8e.addChild(tengah);
Badan.addChild(pagar8e);

pagar9 = new TransformGroup();
pagar9.setTransform(transform(0.0f, 0.25f, 0.45f));
tengah = new Box(0.80f, 0.01f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar9.addChild(tengah);
Badan.addChild(pagar9);

pagar9a = new TransformGroup();
pagar9a.setTransform(transform(0.0f, 0.23f, 0.45f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar9a.addChild(tengah);
Badan.addChild(pagar9a);

pagar9b = new TransformGroup();
pagar9b.setTransform(transform(0.4f, 0.23f, 0.45f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar9b.addChild(tengah);
Badan.addChild(pagar9b);

pagar9c = new TransformGroup();
pagar9c.setTransform(transform(-0.4f, 0.23f, 0.45f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar9c.addChild(tengah);
Badan.addChild(pagar9c);

pagar9d = new TransformGroup();
pagar9d.setTransform(transform(0.79f, 0.23f, 0.45f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar9d.addChild(tengah);
Badan.addChild(pagar9d);

pagar9e = new TransformGroup();
pagar9e.setTransform(transform(-0.79f, 0.23f, 0.45f));
tengah = new Box(0.01f, 0.03f, 0.02f, apr(0.0f, 0.0f, 0.0f));
pagar9e.addChild(tengah);
Badan.addChild(pagar9e);
//
benderamerah = new TransformGroup();
benderamerah.setTransform(transform(-1.17f, 0.75f, 0.0f));
tengah = new Box(0.20f, 0.07f, 0.0f, apr(15f, 0.0f, 0.0f));
benderamerah.addChild(tengah);
Badan.addChild(benderamerah);

benderaputih = new TransformGroup();
benderaputih.setTransform(transform(-1.17f, 0.61f, 0.0f));
tengah = new Box(0.20f, 0.07f, 0.0f, apr(1.0f, 1.0f, 1.0f));
benderaputih.addChild(tengah);
Badan.addChild(benderaputih);

}

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

public void cerobong(float x, float y, float z) {
cerobong = new TransformGroup();
cerobong.setTransform(transform(-0.40f, 0.47f, -0.01f));
Silinder = new Cylinder(0.1f, 0.7f, 1, 500, 1, apr(0.8f, 0.2f, 0.0f));
cerobong.addChild(Silinder);
Badan.addChild(cerobong);
}

public void cerobong2(float x, float y, float z) {
cerobong = new TransformGroup();
cerobong.setTransform(transform(-0.10f, 0.47f, -0.01f));
Silinder = new Cylinder(0.1f, 0.6f, 1, 500, 1, apr(0.8f, 0.2f, 0.0f));
cerobong.addChild(Silinder);
Badan.addChild(cerobong);
}

public void cerobong3(float x, float y, float z) {
cerobong = new TransformGroup();
cerobong.setTransform(transform(0.20f, 0.47f, -0.01f));
Silinder = new Cylinder(0.1f, 0.5f, 1, 500, 1, apr(0.8f, 0.2f, 0.0f));
cerobong.addChild(Silinder);
Badan.addChild(cerobong);
}

public void jendela(float x, float y, float z) {
cerobong = new TransformGroup();
cerobong.setTransform(transform(x, y, z));
jendela = new Box(0.07f, 0.05f, 0.01f, apr(1f, 1f, 5.0f));
cerobong.addChild(jendela);
Badan.addChild(cerobong);
}

public void Radar1(float x, float y, float z) {
cerobong = new TransformGroup();
cerobong.setTransform(transform(-0.98f, 0.47f, -0.01f));
Silinder = new Cylinder(0.02f, 0.75f, 1, 500, 1, apr(0.8f, 0.2f, 0.0f));
cerobong.addChild(Silinder);
Badan.addChild(cerobong);
}

public void lingkaran1() {
tiang = new TransformGroup();
tiang.setTransform(transform(-0.53f, 0.10f, 0.0f));
tiangg2= new Sphere(0.30f,1,50, apr(0.0f, 0.0f, 0.0f));
tiang.addChild(tiangg2);
Badan.addChild(tiang);
}

public void lingkaran2() {
tiang = new TransformGroup();
tiang.setTransform(transform(-0.18f, 0.10f, 0.0f));
tiangg2= new Sphere(0.30f,1,50, apr(0.0f, 0.0f, 0.0f));
tiang.addChild(tiangg2);
Badan.addChild(tiang);
}

public void lingkaran3() {
tiang = new TransformGroup();
tiang.setTransform(transform(0.53f, 0.10f, 0.0f));
tiangg2= new Sphere(0.30f,1,50, apr(0.0f, 0.0f, 0.0f));
tiang.addChild(tiangg2);
Badan.addChild(tiang);
}

public void lingkaran4() {
tiang = new TransformGroup();
tiang.setTransform(transform(0.18f, 0.10f, 0.0f));
tiangg2= new Sphere(0.30f,1,50, apr(0.0f, 0.0f, 0.0f));
tiang.addChild(tiangg2);
Badan.addChild(tiang);
}

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.3f, 0.3f, 0.3f));
xccc1.addChild(lambung_kapal);
Badan.addChild(xccc1);
}

public void badan_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(1.6f, -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.3f, 0.3f, 0.3f));
xccc.addChild(tetrahedron);

Badan.addChild(xccc);
}
public void badan_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.3f, 0.3f, 0.3f));
xccc.addChild(tetrahedron);

Badan.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);

Pusat();
objTrans.addChild(Badan);
BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
Background bg = new Background(new Color3f(0.0f, 9.0f, 0.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() {
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(), 1200, 600);
frame.setLocationRelativeTo(null);
}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s