本文概述
- JOGL Square示例
- JOGL三角形示例
JOGL Square示例 在此示例中, 我们将绘制四个不同的边, 以使它们都在一个正方形的点处连接。
package com.srcmini.jogl;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
public class Square implements GLEventListener { @Overridepublic void init(GLAutoDrawable arg0) {}@Overridepublic void display(GLAutoDrawable drawable) {final GL2 gl = drawable.getGL().getGL2();
//Drawing top edgegl.glBegin( GL2.GL_LINES );
gl.glVertex2d(-0.4, 0.4);
gl.glVertex2d(0.4, 0.4);
gl.glEnd();
//Drawing bottom edgegl.glBegin( GL2.GL_LINES );
gl.glVertex2d(-0.4, -0.4);
gl.glVertex2d(0.4, -0.4);
gl.glEnd();
//Drawing right edgegl.glBegin( GL2.GL_LINES );
gl.glVertex2d(-0.4, 0.4);
gl.glVertex2d(-0.4, -0.4);
gl.glEnd();
//Drawing left edgegl.glBegin( GL2.GL_LINES );
gl.glVertex2d(0.4, 0.4);
gl.glVertex2d(0.4, -0.4);
gl.glEnd();
}@Overridepublic void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { }@Overridepublic void dispose(GLAutoDrawable arg0) {}public static void main(String[] args) {final GLProfile gp = GLProfile.get(GLProfile.GL2);
GLCapabilities cap = new GLCapabilities(gp);
final GLCanvas gc = new GLCanvas(cap);
Square sq = new Square();
gc.addGLEventListener(sq);
gc.setSize(400, 400);
final JFrame frame = new JFrame("JOGL Line");
frame.add(gc);
frame.setSize(500, 400);
frame.setVisible(true);
} }
输出:
文章图片
JOGL三角形示例 在此示例中, 我们将绘制三个不同的边, 以使它们都在三角形形状的点处连接。
Triangle.java
package com.srcmini.jogl;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
public class Triangle implements GLEventListener { @Overridepublic void init(GLAutoDrawable arg0) {}@Overridepublic void display(GLAutoDrawable drawable) {final GL2 gl = drawable.getGL().getGL2();
// Base edgegl.glBegin (GL2.GL_LINES);
gl.glVertex2d(-0.65, -0.65);
gl.glVertex2d(0.65, -0.65);
gl.glEnd();
//Right edgegl.glBegin (GL2.GL_LINES);
gl.glVertex2d(0, 0.65);
gl.glVertex2d(-0.65, -0.65);
gl.glEnd();
//Left edgegl.glBegin (GL2.GL_LINES);
gl.glVertex2d(0, 0.65);
gl.glVertex2d(0.65, -0.65);
gl.glEnd();
gl.glFlush();
}@Overridepublic void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { }@Overridepublic void dispose(GLAutoDrawable arg0) {}public static void main(String[] args) {final GLProfile gp = GLProfile.get(GLProfile.GL2);
GLCapabilities cap = new GLCapabilities(gp);
final GLCanvas gc = new GLCanvas(cap);
Triangle tr= new Triangle();
gc.addGLEventListener(tr);
gc.setSize(400, 400);
final JFrame frame = new JFrame("JOGL Triangle");
frame.add(gc);
frame.setSize(500, 400);
frame.setVisible(true);
} }
【JOGL 2D对象】输出:
文章图片
因此, 只要将特定形状的线连接起来就可以设计任何类型的图形。