Membuat objek Android di pemrograman grafis tidak semudah membuat objek di photoshop. Di pemrograman grafis Anda harus paham mengenai koordinat-koordinat karena koordinatlah yang penting untuk membuat sebuah objek.
Coding membuat objek android pemrograman grafis pada java terdiri dari 2 files java, yaitu : Android.java dan Objek.java. Kedua files java tersebut harus berada dalam 1 source packages.
Tanpa panjang lebar lagi. Silahkan Anda coba coding membuat objek android ini. Admin harap hasilnya sama seperti yang Anda inginkan.
Berikut adalah coding dari Android.java :
Berikut adalah coding dari Objek.java :
Coding membuat objek android pemrograman grafis pada java terdiri dari 2 files java, yaitu : Android.java dan Objek.java. Kedua files java tersebut harus berada dalam 1 source packages.
Tanpa panjang lebar lagi. Silahkan Anda coba coding membuat objek android ini. Admin harap hasilnya sama seperti yang Anda inginkan.
Berikut adalah coding dari Android.java :
package org.yourorghere; import com.sun.opengl.util.Animator; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCanvas; import javax.media.opengl.GLEventListener; import javax.media.opengl.glu.GLU; import javax.media.opengl.glu.GLUquadric; public class Android implements GLEventListener { public static void main(String[] args) { Frame frame = new Frame("Simple JOGL Application"); GLCanvas canvas = new GLCanvas(); canvas.addGLEventListener(new Android()); frame.add(canvas); frame.setSize(640, 480); final Animator animator = new Animator(canvas); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // Run this on another thread than the AWT event queue to // make sure the call to Animator.stop() completes before // exiting new Thread(new Runnable() { @Override public void run() { animator.stop(); System.exit(0); } }).start(); } }); // Center frame frame.setLocationRelativeTo(null); frame.setVisible(true); animator.start(); } @Override public void init(GLAutoDrawable drawable) { // Use debug pipeline // drawable.setGL(new DebugGL(drawable.getGL())); GL gl = drawable.getGL(); System.err.println("INIT GL IS: " + gl.getClass().getName()); // Enable VSync gl.setSwapInterval(1); // Setup the drawing area and shading mode gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f); gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens. } @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL(); GLU glu = new GLU(); if (height <= 0) { // avoid a divide by zero error! height = 1; } final float h = (float) width / (float) height; gl.glViewport(0, 80, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(100.0f, h, 1.0, 30.0); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); } static double sudut = 0; @Override public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); // Clear the drawing area gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Reset the current matrix to the "identity" gl.glLoadIdentity(); gl.glEnable(GL.GL_DEPTH_TEST); // sudut += 2; // Move the "drawing cursor" around gl.glTranslatef(0.0f, 0.0f, -11.0f); gl.glRotated(sudut, 0, 1, 0); // Drawing Cylinder(gl, 2, 3); gl.glPushMatrix(); gl.glTranslated(0, 1.7, 0); HalfSphere(gl, 2); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslated(2.5, 0.5, 0); ArmsLegsEars(gl, 0.4f, 1.5f); gl.glTranslated(-5, 0, 0); ArmsLegsEars(gl, 0.4f, 1.5f); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslated(1, -2, 0); ArmsLegsEars(gl, 0.5f, 1.5f); gl.glTranslated(-2, 0, 0); ArmsLegsEars(gl, 0.5f, 1.5f); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslated(1.25, 3, 0); ArmsLegsEars(gl, 0.1f, 1.5f); gl.glTranslated(-2.5, 0, 0); ArmsLegsEars(gl, 0.1f, 1.5f); gl.glPopMatrix(); // Flush all drawing operations to the graphics card gl.glFlush(); } static void Cylinder(GL gl, float radius, float height) { GLU glu = new GLU(); GLUquadric q = glu.gluNewQuadric(); gl.glPushMatrix(); gl.glColor3d(0.2, 1, 0.2); gl.glTranslated(0, -height / 2, 0); gl.glRotated(90, -1, 0, 0); glu.gluCylinder(q, radius, radius, height, 30, 30); gl.glColor3d(0, 0.45, 1); glu.gluDisk(q, 0, radius, 30, 30); gl.glTranslatef(0, 0, height); glu.gluDisk(q, 0, radius, 30, 30); gl.glPopMatrix(); } static void Sphere(GL gl, float radius) { GLU glu = new GLU(); GLUquadric q = glu.gluNewQuadric(); glu.gluSphere(q, radius, 30, 30); } static void HalfSphere(GL gl, float radius) { GLU glu = new GLU(); GLUquadric q = glu.gluNewQuadric(); double[] eqn0 = {0.0, 4.0, 0.0, 0.0}; double[] eqn1 = {0.35, -0.2, -0.93, 2.0}; double[] eqn2 = {-0.35, -0.2, -0.93, 2.0}; gl.glClipPlane(GL.GL_CLIP_PLANE0, eqn0, 0); gl.glClipPlane(GL.GL_CLIP_PLANE1, eqn1, 0); gl.glClipPlane(GL.GL_CLIP_PLANE2, eqn2, 0); gl.glEnable(GL.GL_CLIP_PLANE0); gl.glEnable(GL.GL_CLIP_PLANE1); gl.glEnable(GL.GL_CLIP_PLANE2); gl.glColor3d(0.2, 1, 0.2); glu.gluSphere(q, radius, 120, 120); gl.glColor3d(1, 0.45, 0); gl.glDisable(GL.GL_CLIP_PLANE1); gl.glDisable(GL.GL_CLIP_PLANE2); glu.gluSphere(q, radius * 0.95, 30, 30); gl.glDisable(GL.GL_CLIP_PLANE0); gl.glColor3d(0.2, 1, 0.2); gl.glRotated(90, 1, 0, 0); gl.glColor3d(0, 0.45, 0); glu.gluDisk(q, 0, radius, 100, 100); gl.glRotated(-90, 1, 0, 0); } static void ArmsLegsEars(GL gl, float radius, float height){ GLU glu = new GLU(); GLUquadric q = glu.gluNewQuadric(); gl.glPushMatrix(); gl.glColor3d(0.0, 1, 0.0); gl.glTranslated(0, -height / 2, 0); gl.glRotated(90, -1, 0, 0); glu.gluCylinder(q, radius, radius, height, 30, 30); glu.gluSphere(q, radius, 30, 30); gl.glTranslatef(0, 0, height); glu.gluSphere(q, radius, 30, 30); gl.glPopMatrix(); } @Override public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
Berikut adalah coding dari Objek.java :
package org.yourorghere; import com.sun.opengl.util.GLUT; import javax.media.opengl.GL; import javax.media.opengl.glu.GLU; import javax.media.opengl.glu.GLUquadric; public class Objek { static void Kubus(GL gl) { gl.glBegin(GL.GL_POLYGON);/* f1: Surface bagian depan */ gl.glVertex3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(20.0f, 0.0f, 1.0f); gl.glVertex3f(20.0f, 0.0f, 0.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f2: Surface bagian bawah (ABFE) */ gl.glVertex3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(20.0f, 0.0f, 0.0f); gl.glVertex3f(20.0f, 10.0f, 0.0f); gl.glVertex3f(0.0f, 10.0f, 0.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f3:Surface bagian belakang (CDHG)*/ gl.glVertex3f(20.0f, 10.0f, 0.0f); gl.glVertex3f(20.0f, 10.0f, 1.0f); gl.glVertex3f(0.0f, 10.0f, 1.0f); gl.glVertex3f(0.0f, 10.0f, 0.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f4: Surface bagian atas (EFGH)*/ gl.glVertex3f(20.0f, 10.0f, 1.0f); gl.glVertex3f(20.0f, 0.0f, 1.0f); gl.glVertex3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(0.0f, 10.0f, 1.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f5: Surface bagian kiri (ADEH)*/ gl.glVertex3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.0f, 10.0f, 0.0f); gl.glVertex3f(0.0f, 10.0f, 1.0f); gl.glVertex3f(0.0f, 0.0f, 1.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f6: Surface bagian kanan (BCFG)*/ gl.glVertex3f(20.0f, 0.0f, 0.0f); gl.glVertex3f(20.0f, 0.0f, 1.0f); gl.glVertex3f(20.0f, 10.0f, 1.0f); gl.glVertex3f(20.0f, 10.0f, 0.0f); gl.glEnd(); } static void Badan(GL gl) { gl.glBegin(GL.GL_POLYGON);/* f1: Surface bagian depan */ gl.glColor3d(1,0,0); gl.glVertex3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(1.0f, 0.0f, 1.0f); gl.glVertex3f(1.0f, 0.0f, 0.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f2: Surface bagian bawah (ABFE) */ gl.glColor3d(1,0,0); gl.glVertex3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(1.0f, 0.0f, 0.0f); gl.glVertex3f(1.0f, 1.0f, 0.0f); gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f3:Surface bagian belakang (CDHG)*/ gl.glColor3d(1,0,0); gl.glVertex3f(1.0f, 1.0f, 0.0f); gl.glVertex3f(1.0f, 1.0f, 1.0f); gl.glVertex3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f4: Surface bagian atas (EFGH)*/ gl.glColor3d(1,0,0); gl.glVertex3f(1.0f, 1.0f, 1.0f); gl.glVertex3f(1.0f, 0.0f, 1.0f); gl.glVertex3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(0.0f, 1.0f, 1.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f5: Surface bagian kiri (ADEH)*/ gl.glColor3d(0,0,0); gl.glVertex3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(0.0f, 0.0f, 1.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f6: Surface bagian kanan (BCFG)*/ gl.glColor3d(0,0,0); gl.glVertex3f(1.0f, 0.0f, 0.0f); gl.glVertex3f(1.0f, 0.0f, 1.0f); gl.glVertex3f(1.0f, 1.0f, 1.0f); gl.glVertex3f(1.0f, 1.0f, 0.0f); gl.glEnd(); } static void Tangan(GL gl) { gl.glBegin(GL.GL_POLYGON);/* f1: Surface bagian depan */ gl.glColor3d(0,0,0); gl.glVertex3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.0f, 0.0f, 0.5f); gl.glVertex3f(0.5f, 0.0f, 0.5f); gl.glVertex3f(0.5f, 0.0f, 0.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f2: Surface bagian bawah (ABFE) */ gl.glColor3d(0,0,0); gl.glVertex3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.5f, 0.0f, 0.0f); gl.glVertex3f(0.5f, 0.5f, 0.0f); gl.glVertex3f(0.0f, 0.5f, 0.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f3:Surface bagian belakang (CDHG)*/ gl.glColor3d(0,0,0); gl.glVertex3f(0.5f, 0.5f, 0.0f); gl.glVertex3f(0.5f, 0.5f, 0.5f); gl.glVertex3f(0.0f, 0.5f, 0.5f); gl.glVertex3f(0.0f, 0.5f, 0.0f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f4: Surface bagian atas (EFGH)*/ gl.glColor3d(0,0,0); gl.glVertex3f(0.5f, 0.5f, 0.5f); gl.glVertex3f(0.5f, 0.0f, 0.5f); gl.glVertex3f(0.0f, 0.0f, 0.5f); gl.glVertex3f(0.0f, 0.5f, 0.5f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f5: Surface bagian kiri (ADEH)*/ gl.glColor3d(0,0,0); gl.glVertex3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.0f, 0.5f, 0.0f); gl.glVertex3f(0.0f, 0.5f, 0.5f); gl.glVertex3f(0.0f, 0.0f, 0.5f); gl.glEnd(); gl.glBegin(GL.GL_POLYGON);/* f6: Surface bagian kanan (BCFG)*/ gl.glColor3d(0,0,0); gl.glVertex3f(0.5f, 0.0f, 0.0f); gl.glVertex3f(0.5f, 0.0f, 0.5f); gl.glVertex3f(0.5f, 0.5f, 0.5f); gl.glVertex3f(0.5f, 0.5f, 0.0f); gl.glEnd(); } }
PENAMPAKAN PROGRAM JIKA DIJALANKAN
0 komentar untuk Contoh Coding Membuat Objek Android Pemrograman Grafis pada Java.
Perlihatkan Semua Komentar Tutup Semua Komentar