ASSALAMU ALAIKUM WR.WB
Kali ini saya mau memposting tugas GRAFIKA KOMPUTER untk membuat ALGORITMA BRESSENHEM berikut ini adalah source kode dan tampilan setelah di RUN :
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;
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) {
}
}
kalau di RUN maka hasilnya akan tampil seperti dibawah ini :
Sabtu, 26 Oktober 2013
Selasa, 22 Oktober 2013
TUGAS GRAFIKA KOMPUTER
untuk source code nya :
package robby;
import java.awt.*;
import java.awt.event.ActionEvent;
/**
*
* @author Putri
*/
public class Robby extends Panel{
Robby(){
setBackground(new Color(227, 227, 227));// warna background
}
public void paint(Graphics A){
A.setColor(new Color(50,55,70));//warna font Nama
A.setFont(new Font("Snap ITC", Font.BOLD,20));// jenis font
A.drawString("Nama : robby meirio",20, 255);// koordinat/letak font
A.setColor(new Color(34, 11, 55));//warna font Nama dan kelas
A.setFont(new Font("Showcard Gothic", Font.BOLD,20));// jenis font
A.drawString("Nim : 11110100",350, 255);// koordinat/letak font
A.setColor(new Color(34, 11, 55));//warna font Nama dan kelas
A.setFont(new Font("Showcard Gothic", Font.BOLD,20));// jenis font
A.drawString("Kelas : A",20, 155);// koordinat/letak font
// A.drawOval(30, 100, 40, 30);
// A.drawLine(10, 100, 100,100);
A.setColor(Color.red);
A.drawArc(229,29, 152, 151, 0, 360);//draw lingkaran kepala
A.setColor(new Color(237, 202, 39));
A.fillArc(230,30, 150, 150, 0, 360);//warnai lingkaran kepala
A.setColor(Color.blue);
A.fillArc(265,70, 20, 40, 0, 360);//mata putih kiri
A.setColor(Color.blue);
A.fillArc(325,70, 20, 40, 0, 360);//mata putih kanan
A.setColor(Color.BLACK);
A.fillArc(325,75, 20, 30, 0, 360);//mata hitam kanan
A.setColor(Color.BLACK);
A.fillArc(265,75, 20, 30, 0, 360);//mata hitam kiri
A.setColor(Color.red);
A.fillArc(275,100, 65, 60, 0, -180);//fill mulut
Image gambar = Toolkit.getDefaultToolkit().getImage("Domo-kun.png");
A.drawImage(gambar, 20, 30, 180, 165, this);
Image gambarr = Toolkit.getDefaultToolkit().getImage("heart005.png");
A.drawImage(gambarr, 20, 30, 30, 30, this);
//A.setColor(Color.GREEN);
//A.fillOval(280, 50, 100, 90);
// A.drawRoundRect(30, 100, 200, 30, 20, 20);
}
public static void main(String[] args) {
// TODO code application logic here
Frame f1 = new Frame();
Robby H = new Robby();
f1.add(H);
f1.setSize(600, 300);
f1.setVisible(true);
}
public void actionPerformed(ActionEvent ev) {
String command = ev.getActionCommand();
if ("Exit".equals(command)) {
System.exit(0);
}
}
}
package robby;
import java.awt.*;
import java.awt.event.ActionEvent;
/**
*
* @author Putri
*/
public class Robby extends Panel{
Robby(){
setBackground(new Color(227, 227, 227));// warna background
}
public void paint(Graphics A){
A.setColor(new Color(50,55,70));//warna font Nama
A.setFont(new Font("Snap ITC", Font.BOLD,20));// jenis font
A.drawString("Nama : robby meirio",20, 255);// koordinat/letak font
A.setColor(new Color(34, 11, 55));//warna font Nama dan kelas
A.setFont(new Font("Showcard Gothic", Font.BOLD,20));// jenis font
A.drawString("Nim : 11110100",350, 255);// koordinat/letak font
A.setColor(new Color(34, 11, 55));//warna font Nama dan kelas
A.setFont(new Font("Showcard Gothic", Font.BOLD,20));// jenis font
A.drawString("Kelas : A",20, 155);// koordinat/letak font
// A.drawOval(30, 100, 40, 30);
// A.drawLine(10, 100, 100,100);
A.setColor(Color.red);
A.drawArc(229,29, 152, 151, 0, 360);//draw lingkaran kepala
A.setColor(new Color(237, 202, 39));
A.fillArc(230,30, 150, 150, 0, 360);//warnai lingkaran kepala
A.setColor(Color.blue);
A.fillArc(265,70, 20, 40, 0, 360);//mata putih kiri
A.setColor(Color.blue);
A.fillArc(325,70, 20, 40, 0, 360);//mata putih kanan
A.setColor(Color.BLACK);
A.fillArc(325,75, 20, 30, 0, 360);//mata hitam kanan
A.setColor(Color.BLACK);
A.fillArc(265,75, 20, 30, 0, 360);//mata hitam kiri
A.setColor(Color.red);
A.fillArc(275,100, 65, 60, 0, -180);//fill mulut
Image gambar = Toolkit.getDefaultToolkit().getImage("Domo-kun.png");
A.drawImage(gambar, 20, 30, 180, 165, this);
Image gambarr = Toolkit.getDefaultToolkit().getImage("heart005.png");
A.drawImage(gambarr, 20, 30, 30, 30, this);
//A.setColor(Color.GREEN);
//A.fillOval(280, 50, 100, 90);
// A.drawRoundRect(30, 100, 200, 30, 20, 20);
}
public static void main(String[] args) {
// TODO code application logic here
Frame f1 = new Frame();
Robby H = new Robby();
f1.add(H);
f1.setSize(600, 300);
f1.setVisible(true);
}
public void actionPerformed(ActionEvent ev) {
String command = ev.getActionCommand();
if ("Exit".equals(command)) {
System.exit(0);
}
}
}
Langganan:
Postingan (Atom)