Sie sind auf Seite 1von 4

package org.yourorghere; import import import import import import import import import import com.sun.opengl.util.Animator; java.awt.Frame; java.awt.event.

WindowAdapter; java.awt.event.WindowEvent; javax.media.opengl.GL; javax.media.opengl.GLAutoDrawable; javax.media.opengl.GLCanvas; javax.media.opengl.GLEventListener; javax.media.opengl.glu.GLU; javax.swing.JOptionPane;

/** * Elips.java <BR> * author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P> * * This version is equal to Brian Paul's version 1.2 1999/10/21 */ public class Elips implements GLEventListener { int jari_hor; int jari_ver; int xtengah; int ytengah; public static void main(String[] args) { Frame frame = new Frame("elips"); GLCanvas canvas = new GLCanvas(); canvas.addGLEventListener(new Elips()); 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(); } }); frame.setLocationRelativeTo(null); frame.setVisible(true); animator.start(); }

public void init(GLAutoDrawable drawable) { try { String str = JOptionPane.showInputDialog("Masukkan Lebar Horisontal"); int x = Integer.parseInt(str); String str1 = JOptionPane.showInputDialog("Masukkan Lebar Vertikal"); int x1 = Integer.parseInt(str1); String str2 = JOptionPane.showInputDialog("Masukkan x titik tengah"); int y = Integer.parseInt(str2); String str3 = JOptionPane.showInputDialog("Masukkan y titik tengah"); int z = Integer.parseInt(str3); this.jari_hor = x; this.jari_ver = x1; this.xtengah = y; this.ytengah = z; GL gl = drawable.getGL(); System.err.println("INIT GL IS: " + gl.getClass().getName()); gl.setSwapInterval(1); gl.glClearColor(0.0F, 0.0F, 0.0F, 0.0F); gl.glShadeModel(7425); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); Elips l = new Elips(); l.init(drawable); } } public void reshape(GLAutoDrawable drawable, int x, int y, int width, int heig ht) { GL gl = drawable.getGL(); GLU glu = new GLU(); if (height <= 0) { height = 1; } float h = width / height; gl.glViewport(0, 0, width, height); gl.glMatrixMode(5889); gl.glLoadIdentity(); glu.gluPerspective(11000.0D, h, 1.0D, 20.0D); gl.glMatrixMode(5888); gl.glLoadIdentity(); } public void elips(GLAutoDrawable drawable, int Rx, int Ry, int xCentre, int yC entre) { Elips e = new Elips(); GL gl = drawable.getGL(); int x = 0; int y = 0; int int int int Ry2 = Ry Rx2 = Rx twoRy2 = twoRx2 = * * 2 2 Ry; Rx; * Ry2; * Rx2;

x = 0; y = Ry; int px = 0;

int py = twoRx2 * y; if (2 * Ry * Ry * x < 2 * Rx * Rx * y) { int p = (int)Math.round(Ry2 - Rx2 * Ry + 0.25D * Rx2); e.piksel(drawable, x, y, xCentre, yCentre); while (px < py) { x++; px += twoRy2; if (p < 0) { p += Ry2 + px; } else { y--; py -= twoRx2; p += Ry2 + px - py; } e.piksel(drawable, x, y, xCentre, yCentre); } } int p = (int)Math.round(Ry2 * (x + 0.5D) * (x + 0.5D) + Rx2 * (y - 1) * (y 1) - Rx2 * Ry2); while (y > 0) { y--; py -= twoRx2; if (p > 0) { p += Rx2 - py; } else { x++; px += twoRy2; p += Ry2 + px - py; } e.piksel(drawable, x, y, xCentre, yCentre); } } public void piksel(GLAutoDrawable drawable, int x, int y, int xCentre, int yCe ntre) { GL gl = drawable.getGL(); gl.glBegin(0); gl.glColor3f(0.0f, 0.0f, 1.0f); // Set the gl.glVertex2i(xCentre + x, yCentre + y); gl.glColor3f(0.0f, 0.0f, 1.0f); // Set the gl.glVertex2i(xCentre - x, yCentre + y); gl.glColor3f(0.0f, 0.0f, 1.0f); // Set the gl.glVertex2i(xCentre + x, yCentre - y); gl.glColor3f(0.0f, 0.0f, 1.0f); // Set the gl.glVertex2i(xCentre - x, yCentre - y); gl.glColor3f(0.0f, 0.0f, 1.0f); // Set the gl.glEnd(); gl.glFlush(); } public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); gl.glClear(16640); gl.glLoadIdentity(); gl.glPointSize(2.0F); current drawing color to blue current drawing color to blue current drawing color to blue current drawing color to blue current drawing color to blue

gl.glTranslatef(-1.5F, 0.0F, -6.0F); Elips e = new Elips(); e.elips(drawable, this.jari_hor, this.jari_ver, this.xtengah, this.ytengah); } public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boole an deviceChanged) { } }

Das könnte Ihnen auch gefallen