目录

JOGL - 使用AWT的画布( Canvas with AWT)

本章介绍如何使用带有AWT帧的Canvas绘制JOGL基本帧。 在这里,我们将构造一个AWT框架,并使用框架类的add()方法将画布对象添加到AWT框架。

下面给出了编写程序的步骤,该程序使用JOGL的Canvas类和AWT的Frame类组合创建JOGL基本框架。

第1步:创建类

最初创建一个实现GlEventListener接口的类并导入包javax.media.opengl。 实现所有四个方法display(), dispose(), reshape(), init(). 由于这是基本框架,因此讨论了原始任务,例如创建canvas类,将其添加到框架中。 所有GLEVentListener接口方法GLEVentListener实现。

第2步:准备画布

(a)构造GLCanvas类对象

final GLCanvas glcanvas = new GLCanvas( xxxxxxx );
//here capabilities obj should be passed as parameter

(b)实例化GLCapabilities

GLCapabilities capabilities = new GLCapabilities( xxxxx );
//here profile obj should be passed as parameter

(c)生成GLProfile对象

因为它是静态方法,所以使用类名调用它。 由于本教程是关于JOGL2的,让我们生成GL2接口对象。

final GLProfile profile = GLProfile.get( GLProfile.GL2 );
// both, variable and method are static hence both are called using class name.

让我们看看canvas的代码片段。

//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);

(d)现在使用addGLEventListener()方法将GLEventListener添加到画布。 该方法需要GLEventListener接口的对象作为参数。 因此,传递实现GLEventListener的类的对象。

BasicFrame basicframe = newBasic Frame( );// class which implements
GLEventListener interface
glcanvas.addGLEventListener( basicframe );

(e)使用GLCanvas从javax.media.opengl.awt.AWTGLAutoDrawable继承的setSize()方法设置框架的大小。

glcanvas.setSize( 400, 400 );

现在您已准备好使用GLCanvas

第3步:创建框架

通过实例化JSE AWT框架组件的Frame类Object来创建框架。

添加画布并使框架可见。

//creating frame
final Frame frame = new frame( " Basic Frame" );
//adding canvas to frame
frame.add( glcanvas );
frame.setVisible( true ); 

第4步:全屏查看框架

要以全屏模式查看帧,请使用java.awt.Toolkit类获取默认屏幕大小。 现在,使用这些默认屏幕尺寸尺寸,使用setSize()方法设置框架尺寸。

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(screenSize.width, screenSize.height);

让我们通过程序来使用AWT生成基本框架 -

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 Frame frame = new Frame (" Basic Frame");
      //adding canvas to frame
      frame.add(glcanvas);
      frame.setSize( 640, 480 );
      frame.setVisible(true);
   }
}

如果编译并执行上述程序,则会生成以下输出。 它显示了当我们使用带有AWT的GLCanvas类时形成的基本框架 -

基本框架
↑回到顶部↑
WIKI教程 @2018