这里写一个最简单的JOGL程序,证明配置成功,并且做一点简单的说明:
- JOGL就是重写GLEventListener里面的一些方法,用过swing画图的应该知道,通过repaint来实现图形显示,这里就是重写display来实现绘图。
- 下面的程序实现了最简单的在画布上添加GLEventListener
package t1;
import java.awt.Frame;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
//继承Lister接口
public class T1 implements GLEventListener {
//显示函数,后面都在这个里面设置图形形状等
@Override
public void display(GLAutoDrawable arg0) {
// method body
}
//释放资源
@Override
public void dispose(GLAutoDrawable arg0) {
//method body
}
//初始化,调用GL等功能
@Override
public void init(GLAutoDrawable arg0) {
// method body
}
@Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
// method body
}
public static void main(String[] args) {
//getting the capabilities object of GL2 profile
final GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(profile);
// The canvas
final GLCanvas glcanvas = new GLCanvas(capabilities);
//实例化对象
T1 b = new T1();
glcanvas.addGLEventListener(b);
glcanvas.setSize(800, 800);
//creating frame
final Frame frame = new Frame (" Basic Frame");
//adding canvas to frame
frame.add(glcanvas);
//初始化窗口大小
frame.setSize( 1800, 1800 );
frame.setVisible(true);
}
}
- 参考维基百科:https://iowiki.com/jogl/jogl_canvas_with_swing.html
- 显示效果,就一个黑框框,连关闭功能都没有
上面的程序有点乱,代码全都在main函数里面,这里再贴一个简单的:
- 该程序实现了全屏蓝色代码,使用glClear方法把整个屏幕都画成蓝色
package led;
import javax.swing.JFrame;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
public class Test1 implements GLEventListener{
private GL2 gl;
@Override
public void init(GLAutoDrawable drawable) {
gl = drawable.getGL().getGL2();
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void display(GLAutoDrawable drawable) {
gl.glClearColor(0.0f,0.0f,1.0f,1.0f);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
gl.glFlush();
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
public static void main(String[] args) {
GLCanvas canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL2)));
Test1 opengl = new Test1();
canvas.addGLEventListener(opengl);
canvas.setSize(500,500);
JFrame frame = new JFrame("第一个JOGL程序");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(canvas);
frame.setSize(frame.getContentPane().getPreferredSize());
frame.setVisible(true);
}
}
- 显示效果:有放大,缩小,关闭功能
这个程序和上面程序对比,就很容易看出canvas在frame中的作用
package t1;
import javax.swing.JFrame;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
public class T2 implements GLEventListener{
private GL2 gl;
@Override
public void init(GLAutoDrawable drawable) {
gl = drawable.getGL().getGL2();
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void display(GLAutoDrawable drawable) {
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
gl.glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
gl.glFlush();
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
public static void main(String[] args) {
GLCanvas canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL2)));
T2 opengl = new T2();
canvas.addGLEventListener(opengl);
canvas.setSize(500,500);
JFrame frame = new JFrame("第一个JOGL程序");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(canvas);
frame.setSize(frame.getContentPane().getPreferredSize());
frame.setVisible(true);
}
}
在主方法中,创建一个GLCanvas对象canvas,一个JFrame对象frame,把canvas加到frame中,并给他们设置尺寸。而想让自己绘制的东西出现在canvas上,就必须为canvas添加GL监听事件(GLEventListener),我们编写的OpenGL类实现了GLEventListener接口。所以为canvas添加OpenGL对象作为监听事件即可,而我们的绘图程序就写在OpenGL中的display()方法之中,这个方法是实现了GLEventListener中的抽象方法。
- OpenGL类要实现如下几个抽象方法:
- 所有的方法都是由GLAutoDrawable接口对象触
- GLAutoDrawable接口对象创建的时候被调用,而这个接口对象是在GLCanvas对象加入窗口部件时自动创建的。这个方法可以被多次调用,例如:GLCanvas对象从窗口部件中移除了然后再次加入。
public void init(GLAutoDrawable drawable);
- 当组件的大小尺寸被改变时候调用,第一次绘制的时候也会调用。
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
- 在reshape之后调用
public void display(GLAutoDrawable drawable);
- 在GLCanvas对象从窗口部件中移除的时候调用,清理回收资源
public void dispose(GLAutoDrawable drawable);
- 显示效果: