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]

Tugas 6 : Membuat Lukisan kapal 2D java

Di dalam bahasa pemrograman java kita bisa membuat grafik baik 2d maupun yang 3d.,
contohnya gambar di bawah ini :

untuk Source codenya disimpan dalam file  lukisan_kapal.java :


package tugas6;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
/**
* @author Haris
*/
public class lukisan_kapal extends JApplet {

public static void main(String s[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new lukisan_kapal();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}

public void init() {
JPanel panel = new Pan();
getContentPane().add(panel);
}

class Pan extends JPanel {

public Pan() {
setPreferredSize(new Dimension(1300, 600));
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g1 = (Graphics2D) g;
Graphics2D g2 = (Graphics2D) g;
// nama dan nim
kotak(g2);
GradientPaint paint1 = new GradientPaint(100, 100, new Color(130, 36, 2), 250, 100, new Color(253, 149, 51));
Font font = new Font("Comic Sans ms", Font.BOLD, 25);
g1.setPaint(paint1);
g1.setFont(font);
g1.drawString("Haris Budi Erwanto", 50, 500);
g1.drawString("( 10-65-0006 )", 65, 550);

garisdda(g2, 0, 350, 325, 350);
garisdda(g2, 625, 350, 1300, 350);

garisdda(g2, 350, 400, 600, 400);
garisdda(g2, 600, 400, 650, 300);
garisdda(g2, 300, 300, 350, 400);
garisdda(g2, 300, 300, 650, 300);

kotak_brass(g2, 350, 250, 600, 300);
bunder(g2, 1150, 100, 75);
body(g2);
garisdda(g2, 950, 100, 1050, 100);
garisdda(g2, 960, 25, 1060, 50);
garisdda(g2, 960, 175, 1060, 150);
garisdda(g2, 980, 250, 1080, 175);
budermidpoint(g2, 375, 275, 20);
budermidpoint(g2, 430, 275, 20);
budermidpoint(g2, 485, 275, 20);
budermidpoint(g2, 540, 275, 20);
awane(g2, -10, 50);
awane(g2, 90, 1);
awane(g2, 210, 50);
awane(g2, 310, 1);
awane(g2, 410, 50);
awane(g2, 510, 1);
awane(g2, 610, 50);
awane(g2, 710, 1);
}

public void body(Graphics2D g) {
GeneralPath gp = new GeneralPath();
GradientPaint p = new GradientPaint(0, 350, Color.gray, 50, 30, Color.black);
g.setPaint(p);

gp.moveTo(300, 300);
gp.lineTo(650, 300);
gp.lineTo(600, 400);
gp.lineTo(350, 400);
gp.closePath();
g.fill(gp);
}

public void kotak_brass(Graphics2D gr, int x_awal, int y_awal, int x_akhir, int y_akhir) {
GeneralPath gp = new GeneralPath();
Shape s = new Rectangle2D.Double(x_awal, y_awal, 250, 50);
GradientPaint p = new GradientPaint(0, 350, Color.green, 50, 30, Color.black);
gr.setPaint(p);
gr.fill(s);
int Dx = Math.abs(x_akhir - x_awal);
int Dy = Math.abs(y_akhir - y_awal);
int parameter = (2 * Dy) - Dx;
String status = "Kotak_BM";
this.repaint();
double g = x_awal, h = y_awal;
while (x_awal < x_akhir) {
if (parameter < 0) {
parameter += (2 * Dy);
} else {
y_awal++;
parameter = parameter + (2 * Dy) - (2 * Dx);
}
gp.moveTo(x_awal, y_awal);
x_awal++;
}
gp.lineTo(g, y_awal);
gp.lineTo(g, h);
gp.lineTo(x_awal, h);
gp.lineTo(x_awal, y_awal);
gp.closePath();
gr.draw(gp);}

public void garisdda(Graphics2D g, int x1, int y1, int x2, int y2) {

double m = (double) (y2 - y1) / (x2 - x1);
double y = (double) y1;
int iy;
for (int x = x1; x <= x2; x++) {
iy = (int) Math.round(y);
g.drawLine(x, iy, x, iy);
y += m;
}
}
}

public void kotak(Graphics2D g) {
Shape s = new Rectangle2D.Double(0, 0, 1300, 350);
GradientPaint p = new GradientPaint(0, 350, new Color(0, 255, 255), 50, 30, new Color(70, 51, 204));
g.setPaint(p);
g.fill(s);
Shape s1 = new Rectangle2D.Double(0, 350, 1300, 250);
GradientPaint r = new GradientPaint(0, 350, Color.white, 1300, 600, Color.blue);
g.setPaint(r);
g.fill(s1);
}

void budermidpoint(Graphics2D g, int x1, int y1, int r) {
Shape s = new Ellipse2D.Double(x1 - r, y1 - r, r * 2, r * 2);
g.setColor(Color.red);
g.fill(s);
int x = 0, y = r, p = 1 - r;
int xctr = x1, yctr = y1;
g.drawLine(xctr + x, yctr + y, xctr + x, yctr + y);
g.drawLine(xctr - x, yctr + y, xctr - x, yctr + y);
g.drawLine(xctr + x, yctr - y, xctr + x, yctr - y);
g.drawLine(xctr - x, yctr - y, xctr - x, yctr - y);
g.drawLine(xctr + y, yctr + x, xctr + y, yctr + x);
g.drawLine(xctr - y, yctr + x, xctr - y, yctr + x);
g.drawLine(xctr + y, yctr - x, xctr + y, yctr - x);
g.drawLine(xctr - y, yctr - x, xctr - y, yctr - x);
while (x < y) {
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
}
}

public void bunder(Graphics2D g, int x1, int y1, int r) {
Shape s = new Ellipse2D.Double(x1 - r, y1 - r, r * 2, r * 2);
GradientPaint p = new GradientPaint(x1, y1, Color.orange, x1 + 200, y1 + 50, Color.red);
g.setPaint(p);
g.fill(s);
}

public void awane(Graphics2D g2, int x, int y) {
GeneralPath gp = new GeneralPath();
gp.moveTo(30 + x, 80 + y);
gp.curveTo(-5 + x, 60 + y, 22 + x, 15 + y, 40 + x, 42 + y);
gp.curveTo(37 + x, 31 + y, 53 + x, 31 + y, 58 + x, 43 + y);
gp.curveTo(60 + x, 7 + y, 116 + x, 4 + y, 100 + x, 34 + y);
gp.curveTo(150 + x, 29 + y, 150 + x, 89 + y, 120 + x, 90 + y);
gp.curveTo(100 + x, 120 + y, 95 + x, 97 + y, 87 + x, 100 + y);
gp.curveTo(71 + x, 120 + y, 35 + x, 100 + y, 35 + x, 70 + y);
gp.closePath();
gp.closePath();
g2.setPaint(new GradientPaint(0 + x, 20 + y, Color.lightGray, 0 + x, 150 + y, Color.white));
g2.fill(gp);
g2.setColor(Color.white);
g2.draw(gp);
}

}