Pada tugas ke 8 kali ini kita akan membuat java 3D.
adapun tampilanya adalah sebagai berikut :
adapun Source Kodenya adalah sebagai berikut :
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Triangulator;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.text.NumberFormat;
import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.ImageComponent;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Material;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Screen3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.BoundingBox;
import javax.media.j3d.Font3D;
import javax.media.j3d.FontExtrusion;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Text3D;
import javax.swing.Box;
import javax.swing.JTabbedPane;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import org.w3c.dom.Text;
public class Kabah extends Applet {
SimpleUniverse ryo;
boolean isApplication;
Canvas3D canvas;
//OffScreenCanvas3D offScreenCanvas;
View lihat;
// Deklarasi Nama Animasi
TransformGroup Badan;
TransformGroup Kotak;
TransformGroup Kotak2;
TransformGroup Kotak3;
TransformGroup Kotak4;
TransformGroup Kotak5;
TransformGroup Kotak6;
TransformGroup Kotak7;
TransformGroup Kotak8;
TransformGroup Kotak9;
TransformGroup Kotak10;
TransformGroup Kotak11;
TransformGroup Kotak12;
TransformGroup Kotak13;
TransformGroup Bulat;
TransformGroup Bulat2;
TransformGroup Kotak14;
TransformGroup Bulat3;
TransformGroup Bulat4;
TransformGroup Kotak15;
TransformGroup Kotak16;
TransformGroup Bulat5;
TransformGroup Bulat6;
TransformGroup Kotak17;
TransformGroup Kotak18;
TransformGroup Kotak19;
TransformGroup Kotak20;
// Atur Putar
String Filebadankabah;
int FileSeq = 0;
float SkalaLayar = 1.5f;
//Unsur GUI
JTabbedPane tabbedPane;
//Temporari
Transform3D Trans = new Transform3D();
Vector3f Vector = new Vector3f();
com.sun.j3d.utils.geometry.Box kubus1;
com.sun.j3d.utils.geometry.Box kubus2;
com.sun.j3d.utils.geometry.Box kubus3;
com.sun.j3d.utils.geometry.Box kubus4;
com.sun.j3d.utils.geometry.Box kubus5;
com.sun.j3d.utils.geometry.Box kubus6;
com.sun.j3d.utils.geometry.Box kubus7;
com.sun.j3d.utils.geometry.Box kubus8;
com.sun.j3d.utils.geometry.Box kubus9;
com.sun.j3d.utils.geometry.Box kubus10;
com.sun.j3d.utils.geometry.Box kubus11;
Cylinder kubus12;
Cylinder kubus13;
Sphere bulat;
Sphere bulat2;
com.sun.j3d.utils.geometry.Box kubus14;
Sphere bulat3;
Sphere bulat4;
Cylinder kubus15;
Cylinder kubus16;
Sphere bulat5;
Sphere bulat6;
com.sun.j3d.utils.geometry.Box kubus17;
com.sun.j3d.utils.geometry.Box kubus18;
com.sun.j3d.utils.geometry.Box kubus19;
com.sun.j3d.utils.geometry.Box kubus20;
private Font3D Font3D;
TransformGroup Text = new TransformGroup();
Transform3D transform(float x, float y, float z) {
Vector.set(x, y, z);
Trans.set(Vector);
return Trans;
}
Appearance apr(float r, float g, float b) {
Color3f Red = new Color3f(r, g, b);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(0.0f, 0.0f, 0.0f);
// Mix Warna(pewarnaan)
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 apr2(float r, float g, float b) {
// Membuat Warna
Color3f Red = new Color3f(r, g, b);
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;
}
//rotasi
TransformGroup putar(double d1, double d2) {
Transform3D rotate = new Transform3D();
Transform3D temprotate = new Transform3D();
rotate.rotX(Math.PI / d1);
temprotate.rotY(Math.PI / d2);
rotate.mul(temprotate);
TransformGroup objRotate = new TransformGroup(rotate);
return objRotate;
}
TransformGroup putar1(double d1, double d2) {
Transform3D rotate = new Transform3D();
Transform3D temprotate = new Transform3D();
rotate.rotZ(Math.PI / d1);
temprotate.rotY(Math.PI / d2);
rotate.mul(temprotate);
TransformGroup objRotate = new TransformGroup(rotate);
return objRotate;
}
TransformGroup putar2(double d1, double d2) {
Transform3D rotate = new Transform3D();
Transform3D temprotate = new Transform3D();
rotate.rotZ(Math.PI / d1);
temprotate.rotY(Math.PI / d2);
rotate.mul(temprotate);
TransformGroup objRotate = new TransformGroup(rotate);
return objRotate;
}
// Konstan
Point3f man = new Point3f();
Vector3f an = new Vector3f(0.0f, 1.0f, 0.0f);
// NumberFormat
NumberFormat nf;
private TransformGroup tubuh;
private TransformGroup bdn;
// Inti Program membuat bentuk gambar 3D
void Gambarkabah() {
Badan = new TransformGroup();
//putar
TransformGroup x1 = putar(1.0, 0.5);
TransformGroup x2 = putar(1.0, 0.1);
TransformGroup x3 = putar(1.0, 0.5);
TransformGroup x4 = putar(25.0, 0.1);
TransformGroup x5 = putar(1.0, 0.1);
// center
Vector.set(0.0f, -1.5f, 0.0f);
Trans.set(Vector);
Badan.setTransform(Trans);
//membuat ka'bah
Kotak = new TransformGroup();
Vector.set(0.0f, 0.5f, 1.0f);
Trans.set(Vector);
Kotak.setTransform(Trans);
kubus1 = new com.sun.j3d.utils.geometry.Box(0.6f, 0.5f, 0.5f, apr(0.2f, 0.2f, 0.2f));//membuat bentuk dan warna
Kotak2 = new TransformGroup();
Vector.set(0.0f, 1.11f, 1.0f);
Trans.set(Vector);
Kotak2.setTransform(Trans);
kubus2 = new com.sun.j3d.utils.geometry.Box(0.6f, 0.07f, 0.5f, apr(7.0f, 0.9f, 0.0f));
Kotak3 = new TransformGroup();
Vector.set(0.0f, 1.31f, 1.0f);
Trans.set(Vector);
Kotak3.setTransform(Trans);
kubus3 = new com.sun.j3d.utils.geometry.Box(0.6f, 0.13f, 0.5f, apr(0.2f, 0.2f, 0.2f));
Kotak4 = new TransformGroup();
Vector.set(-0.4f, 1.02f, 1.0f);
Trans.set(Vector);
Kotak4.setTransform(Trans);
kubus4 = new com.sun.j3d.utils.geometry.Box(0.2f, 0.02f, 0.5f, apr(2f, 2f, 2f));
Kotak5 = new TransformGroup();
Vector.set(-0.15f, 1.02f, 1.0f);
Trans.set(Vector);
Kotak5.setTransform(Trans);
kubus5 = new com.sun.j3d.utils.geometry.Box(0.05f, 0.02f, 0.5f, apr(0.2f, 0.2f, 0.2f));
Kotak6 = new TransformGroup();
Vector.set(0f, 1.02f, 1.0f);
Trans.set(Vector);
Kotak6.setTransform(Trans);
kubus6 = new com.sun.j3d.utils.geometry.Box(0.1f, 0.02f, 0.5f, apr(2f, 2f, 2f));
Kotak7 = new TransformGroup();
Vector.set(0.15f, 1.02f, 1.0f);
Trans.set(Vector);
Kotak7.setTransform(Trans);
kubus7 = new com.sun.j3d.utils.geometry.Box(0.05f, 0.02f, 0.5f, apr(0.2f, 0.2f, 0.2f));
Kotak8 = new TransformGroup();
Vector.set(0.4f, 1.02f, 1.0f);
Trans.set(Vector);
Kotak8.setTransform(Trans);
kubus8 = new com.sun.j3d.utils.geometry.Box(0.2f, 0.02f, 0.5f, apr(2f, 2f, 2f));
Kotak9 = new TransformGroup();
Vector.set(-0.3f, 0.32f, 1.52f);
Trans.set(Vector);
Kotak9.setTransform(Trans);
kubus9 = new com.sun.j3d.utils.geometry.Box(0.2f, 0.3f, 0f, apr(2f, 2f, 2f));
Kotak10 = new TransformGroup();
Vector.set(0.0f, 0.8f, -3.3f);
Trans.set(Vector);
Kotak10.setTransform(Trans);
kubus10 = new com.sun.j3d.utils.geometry.Box(1.5f, 0.8f, 0.7f, apr(0.9f, 0.9f, 9.0f));
Kotak11 = new TransformGroup();
Vector.set(0.0f, 0.42f, -2.5f);
Trans.set(Vector);
Kotak11.setTransform(Trans);
kubus11 = new com.sun.j3d.utils.geometry.Box(0.6f, 0.4f, 0.5f, apr(0.9f, 0.9f, 9.0f));
Kotak12 = new TransformGroup();
Vector.set(-0.7f, 0.52f, -2f);
Trans.set(Vector);
Kotak12.setTransform(Trans);
kubus12 = new Cylinder(0.15f, 1f, apr(0.9f, 0.5f, 0.0f));
Kotak13 = new TransformGroup();
Vector.set(0.7f, 0.52f, -2f);
Trans.set(Vector);
Kotak13.setTransform(Trans);
kubus13 = new Cylinder(0.15f, 1f, apr(0.9f, 0.5f, 0.0f));
Bulat = new TransformGroup();
Vector.set(0.7f, 1.2f, -2f);
Trans.set(Vector);
Bulat.setTransform(Trans);
bulat = new Sphere(0.3f, 1, 2, apr(0.9f, 0.5f, 0.0f));
Bulat2 = new TransformGroup();
Vector.set(-0.7f, 1.2f, -2f);
Trans.set(Vector);
Bulat2.setTransform(Trans);
bulat2 = new Sphere(0.3f, 1, 2, apr(0.9f, 0.5f, 0.0f));
Kotak14 = new TransformGroup();
Vector.set(0f, 0.32f, -1.9f);
Trans.set(Vector);
Kotak14.setTransform(Trans);
kubus14 = new com.sun.j3d.utils.geometry.Box(0.2f, 0.3f, 0f, apr(2f, 2f, 2f));
Bulat3 = new TransformGroup();
Vector.set(0f, 0.8f, -2.2999f);
Trans.set(Vector);
Bulat3.setTransform(Trans);
bulat3 = new Sphere(0.3f, 1, 50, apr(2f, 2f, 2f));
Bulat4 = new TransformGroup();
Vector.set(0f, 1.5f, -3.3f);
Trans.set(Vector);
Bulat4.setTransform(Trans);
bulat4 = new Sphere(0.7f, 1, 50, apr(2f, 2f, 2f));
Kotak15 = new TransformGroup();
Vector.set(-1.8f, 0.89f, -2.8f);
Trans.set(Vector);
Kotak15.setTransform(Trans);
kubus15 = new Cylinder(0.4f, 1.8f, apr(7.0f, 0.9f, 0.0f));
Kotak16 = new TransformGroup();
Vector.set(1.8f, 0.89f, -2.8f);
Trans.set(Vector);
Kotak16.setTransform(Trans);
kubus16 = new Cylinder(0.4f, 1.8f, apr(7.0f, 0.9f, 0.0f));
Bulat5 = new TransformGroup();
Vector.set(1.8f, 2.2f, -2.8f);
Trans.set(Vector);
Bulat5.setTransform(Trans);
bulat5 = new Sphere(0.6f, 1, 2, apr(7.0f, 0.9f, 0.0f));
Bulat6 = new TransformGroup();
Vector.set(-1.8f, 2.2f, -2.8f);
Trans.set(Vector);
Bulat6.setTransform(Trans);
bulat6 = new Sphere(0.6f, 1, 2, apr(7.0f, 0.9f, 0.0f));
Kotak17 = new TransformGroup();
Vector.set(-1.5f, 0f, 1.0f);
Trans.set(Vector);
Kotak17.setTransform(Trans);
kubus17 = new com.sun.j3d.utils.geometry.Box(1.5f, 0.02f, 1.5f, apr(0.3f, 0.5f, 0.9f));
Kotak18 = new TransformGroup();
Vector.set(1.5f, 0f, 1.0f);
Trans.set(Vector);
Kotak18.setTransform(Trans);
kubus18 = new com.sun.j3d.utils.geometry.Box(1.5f, 0.02f, 1.5f, apr(0.9f, 0.9f, 9.0f));
Kotak19 = new TransformGroup();
Vector.set(-1.5f, 0f, -2f);
Trans.set(Vector);
Kotak19.setTransform(Trans);
kubus19 = new com.sun.j3d.utils.geometry.Box(1.5f, 0.02f, 1.5f, apr(0.9f, 0.5f, 0.0f));
Kotak20 = new TransformGroup();
Vector.set(1.5f, 0f, -2f);
Trans.set(Vector);
Kotak20.setTransform(Trans);
kubus20 = new com.sun.j3d.utils.geometry.Box(1.5f, 0.02f, 1.5f, apr(1f, -1f, -1f));
//untuk memangil atau menampilkan gambar
Badan.addChild(Kotak);
Kotak.addChild(kubus1);
Badan.addChild(Kotak2);
Kotak2.addChild(kubus2);
Badan.addChild(Kotak3);
Kotak3.addChild(kubus3);
Badan.addChild(Kotak4);
Kotak4.addChild(kubus4);
Badan.addChild(Kotak5);
Kotak5.addChild(kubus5);
Badan.addChild(Kotak6);
Kotak6.addChild(kubus6);
Badan.addChild(Kotak7);
Kotak7.addChild(kubus7);
Badan.addChild(Kotak8);
Kotak8.addChild(kubus8);
Badan.addChild(Kotak9);
Kotak9.addChild(kubus9);
Badan.addChild(Kotak10);
Kotak10.addChild(kubus10);
Badan.addChild(Kotak11);
Kotak11.addChild(kubus11);
Badan.addChild(Kotak12);
Kotak12.addChild(kubus12);
Badan.addChild(Kotak13);
Kotak13.addChild(kubus13);
Badan.addChild(Bulat);
Bulat.addChild(bulat);
Badan.addChild(Bulat2);
Bulat2.addChild(bulat2);
Badan.addChild(Kotak14);
Kotak14.addChild(kubus14);
Badan.addChild(Bulat3);
Bulat3.addChild(bulat3);
Badan.addChild(Bulat4);
Bulat4.addChild(bulat4);
Badan.addChild(Kotak15);
Kotak15.addChild(kubus15);
Badan.addChild(Kotak16);
Kotak16.addChild(kubus16);
Badan.addChild(Bulat5);
Bulat5.addChild(bulat5);
Badan.addChild(Bulat6);
Bulat6.addChild(bulat6);
Badan.addChild(Kotak17);
Kotak17.addChild(kubus17);
Badan.addChild(Kotak18);
Kotak18.addChild(kubus18);
Badan.addChild(Kotak19);
Kotak19.addChild(kubus19);
Badan.addChild(Kotak20);
Kotak20.addChild(kubus20);
//selasai bentuk
Point3f[] tetrahedronCoordinates = {
new Point3f(0.5f, 0.0f, 0.0f),
new Point3f(-0.5f, 0.0f, 0.0f),
new Point3f(0.0f, 0.0f, -1.0f),
new Point3f(0.0f, 0.5f, 0.0f)
};
//The four faces (triangles) of the tetrahedron.
int coordIndices[] = {
0, 3, 1,//It is interesting to see what happens when 0,3,1 is changed to 0,1,3.
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();
Point3f[] tetrahedronCoordinatesB = {
new Point3f(0.5f, 0.0f, 0.0f),
new Point3f(-0.5f, 0.0f, 0.0f),
new Point3f(0.0f, 0.0f, -1.0f),
new Point3f(0.0f, 0.4f, 0.0f)
};
//The four faces (triangles) of the tetrahedron.
int coordIndicesB[] = {0, 3, 1, 0, 2, 3, 0, 1, 2, 1, 3, 2};
GeometryInfo giB = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
giB.setCoordinates(tetrahedronCoordinatesB);
giB.setCoordinateIndices(coordIndicesB);
NormalGenerator ngB = new NormalGenerator();
ngB.generateNormals(giB);
GeometryArray teB = giB.getGeometryArray();
}
//membuat text
void Teks() {
Text.setTransform(transform(0f, 2f, -5f));
Font3D font3D = new Font3D(new Font("Comic San Serif", Font.PLAIN, 1),
new FontExtrusion());
Text3D textGeom = new Text3D(font3D, new String("Assalamu'alaikum"));
textGeom.setAlignment(Text3D.ALIGN_CENTER);
Shape3D textShape = new Shape3D();
textShape.setGeometry(textGeom);
textShape.setAppearance(apr(3f, 2f, 0.3f));
TransformGroup x = putar(30,35);
Text.addChild(x);
x.addChild(textShape);
}
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 kabah
Gambarkabah();
Teks();
objTrans.addChild(Text);
objTrans.addChild(Badan);
BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
Background bg = new Background(new Color3f(0.0f, 0.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.1f, -0.5f, -0.5f);
DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(2, 1000, 2000, 3000, 2000, 1000);
RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
return objRoot;
}
public Kabah() {
this(false, 1.0f);
}
public Kabah(boolean isApplication, float initOffScreenScale) {
this.isApplication = isApplication;
this.SkalaLayar = initOffScreenScale;
}
public void init() {
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3);
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas = new Canvas3D(config);
add("Center", canvas);
ryo = new SimpleUniverse(canvas);
if (isApplication) {
//offScreenCanvas = new OffScreenCanvas3D(config, true);
Screen3D sOn = canvas.getScreen3D();
// Screen3D sOff = offScreenCanvas.getScreen3D();
Dimension dim = sOn.getSize();
dim.width *= SkalaLayar;
dim.height *= SkalaLayar;
// sOff.setSize(dim);
// sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * SkalaLayar);
// sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * SkalaLayar);
// ryo.getViewer().getView().addCanvas3D(offScreenCanvas);
}
BranchGroup scene = createSceneGraph();
ryo.getViewingPlatform().setNominalViewingTransform();
ryo.addBranchGraph(scene);
lihat = ryo.getViewer().getView();
}
public static void main(String[] args) {
float initOffScreenScale = 2.5f;
for (int i = 0; i < args.length; i++) {
if (args[i].equals(
"-s")) {
if (args.length >= (i + 1)) {
initOffScreenScale = Float.parseFloat(args[i + 1]);
i++;
}
}
}
new MainFrame(new Kabah(true, initOffScreenScale), 1000, 700);
}
public void mouseReleased(MouseEvent e) {
Point loc = canvas.getLocationOnScreen();
// offScreenCanvas.setOffScreenLocation(loc);
Dimension dim = canvas.getSize();
dim.width *= SkalaLayar;
dim.height *= SkalaLayar;
nf.setMinimumIntegerDigits(3);
// offScreenCanvas.snapImageFile(Filebadanmercu + nf.format(FileSeq++), dim.width, dim.height);
nf.setMinimumIntegerDigits(0);
}
}
Tidak ada komentar:
Posting Komentar