- JOGL Tutorial
- JOGL - Home
- JOGL - Overview
- JOGL - Installation
- JOGL Basic Templates
- JOGL - API for Basic Templates
- JOGL - Canvas with AWT
- JOGL - Canvas with Swing
- JOGL - GLJPanel Class
- JOGL Graphical Shapes
- JOGL - Drawing Basics
- JOGL - Drawing with GL_Lines
- JOGL - Pre-defined shapes
- JOGL Effects & Transformation
- JOGL - Transformation
- JOGL - Coloring
- JOGL - Scaling
- JOGL - Rotation
- JOGL - Lighting
- JOGL 3D Graphics
- JOGL - 3D Basics
- JOGL - 3D Triangle
- JOGL - 3D Cube
- JOGL - Appendix
- JOGL Useful Resources
- JOGL - Quick Guide
- JOGL - Useful Resources
- JOGL - Discussion
JOGL - 带秋千的帆布
本章介绍如何使用Canvas和javax.swing 包中的JFrame类绘制 JOGL 基本框架。在这里,我们将实例化一个 JFrame 并使用add()方法将画布对象添加到 JFrame 的实例中。
将 Canvas 与 AWT 结合使用可为您提供具有重量级功能的图形框架。为了拥有一个轻量级的图形框架,您需要将GLCanvas与 Swing 结合使用。在 Swing 中使用GLCanvas时,您可以将GLCanvas直接放置在JFrame窗口中,也可以将其添加到JPanel中。
下面给出的是结合 JOGL 的GLCanvas类和javax.swing包的JFrame类创建 JOGL 基本框架的程序。
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
public class BasicFrame implements GLEventListener {
@Override
public void display(GLAutoDrawable arg0) {
// method body
}
@Override
public void dispose(GLAutoDrawable arg0) {
//method body
}
@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);
BasicFrame b = new BasicFrame();
glcanvas.addGLEventListener(b);
glcanvas.setSize(400, 400);
//creating frame
final JFrame frame = new JFrame (" Basic Frame");
//adding canvas to it
frame.getContentPane().add(glcanvas);
frame.setSize(frame.getContentPane().getPreferredSize());
frame.setVisible(true);
}//end of main
}//end of classimport
如果编译并执行上述程序,将生成以下输出。它显示了我们使用GLCanvas和 Swing 窗口时形成的基本框架。
