Selamat Datang di blog sederhana saya,Semoga apa yang Kalian Inginkan dapat terpenuhi disini.

Implementasi Algoritma Bressenham Dengan Java Dan OpenGL

 Selamat siang pemirsa....!!
mau bagi bagi tugas lagi nich,hehehe....
program ini adalah tugas saya di kuliah Pemrograman Komputer Grafis membuat Garis dengan Algoritma Bressenham.Mungkin masih kurang sempurna,tapi lumayan bisa buat referensi teman teman yang mungkin lagi sibuk Ubek ubek google.program ini saya buat pake java netbeans dengan plugin OpenGL.Kalau belum ngerti cara pake OpenGL di Netbeans,bisa dibaca di sini

Langsung saja di simak source programnya


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;



public class Bressenham implements GLEventListener {


public static void main(String[] args) {

Frame frame = new Frame("Bressenham");

GLCanvas canvas = new GLCanvas();


canvas.addGLEventListener(new Bressenham());

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() {


public void run() {

animator.stop();

System.exit(0);

}

}).start();

}

});

// Center frame

frame.setLocationRelativeTo(null);

frame.setVisible(true);

animator.start();

}


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(0.0f, 0.0f, 0.0f, 0.0f);

gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.

}


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, 0, width, height);

gl.glMatrixMode(GL.GL_PROJECTION);

gl.glLoadIdentity();

glu.gluPerspective(11000.0f, h, 1.0, 20.0);

gl.glMatrixMode(GL.GL_MODELVIEW);

gl.glLoadIdentity();

}


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();


// Menggambar Garis Putus Putus Bawah

gl.glTranslatef(-1.5f, 0.0f, -6.0f);

gl.glPointSize(12.0f);

int p1x=40;

int p1y=15;

int p2x=45;

int p2y=15;

int dx=Math.abs(p2x-p1x);

int dy=Math.abs(p2y-p1y);

int pk=2*dy-dx;

int k;

for (int x=0;x<10;x++){



for (k=40;k<p2x;k++){

if (pk<=0){

pk=pk+2*dy;

p1x-=1;



gl.glBegin(GL.GL_POINTS);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p1x, p1y-4);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p1x, p1y-25);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p1x, p1y-25-20);

gl.glEnd();

gl.glFlush();

}else {

pk=pk+(2*dy)-(2*dx);

p1x-=1;

p1y-=1;



gl.glBegin(GL.GL_POINTS);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p1x, p1y-4);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p1x, p1y-25);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p1x, p1y-25-20);

gl.glEnd();



gl.glFlush();

}

}

p1x-=10;

} // Menggambar Garis Putus Miring Kanan

int p3x=5;

int p3y=5;

int p4x=10;

int p4y=10;

int dx2=Math.abs(p4x-p3x);

int dy2=Math.abs(p4y-p3y);

int pk2=2*dy2-dx2;

int k2;

gl.glBegin(GL.GL_POINTS);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p3x+2, p3y+2);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p3x+2-10, p3y+2-10);

for (k2=p3x;k2<p4x;k2++){

if (pk2<=0){

pk2=pk2+2*dy2;

p3x-=1;



gl.glBegin(GL.GL_POINTS);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p3x, p3y);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p3x-10, p3y-10);





gl.glEnd();

gl.glFlush();

}else {

pk2=pk2+(2*dy2)-(2*dx2);

p3x-=1;

p3y-=1;



gl.glBegin(GL.GL_POINTS);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p3x, p3y);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p3x-10, p3y-10);







gl.glEnd();

gl.glFlush();

}

}

int p5x=-10;//menggambar garis putus putus miring ke kiri

int p5y=-10;

int p6x=-3;

int p6y=-17;

int dx3=Math.abs(p6x-p5x);

int dy3=Math.abs(p6y-p5y);

int pk3=2*dy3-dx3;

int k3;

gl.glBegin(GL.GL_POINTS);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p5x, p5y);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p6x, p6y);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p6x+9, p6y-9);


for (k3=-7;k3<p6x;k3++){

if (pk3<=0){

pk3=pk3+2*dy3;

p5x+=1;

gl.glBegin(GL.GL_POINTS);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p3x, p3y);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p5x+9, p5y-9);





gl.glEnd();

gl.glFlush();

}else {

pk3=pk3+(2*dy3)-(2*dx3);

p5x+=1;

p5y-=1;

gl.glBegin(GL.GL_POINTS);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p5x, p5y);

gl.glColor3f(1.0f, 0.0f, 0.0f);

gl.glVertex2f(p5x+9, p5y-9);





gl.glEnd();

gl.glFlush();

}

}



}


public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {

}

}

hehehehe....pusing gak bacanya ?hehehehe....
kalau di running jadinya seperti ini





sekian dulu ya....kita jumpa lagi di lain kesempatan.daaaahhhhhhh..........
Share:

0 comments:

Post a Comment

  • ()
  • ()
Show more

Halaman