/*******************************************************************************
* Copyright (c) 2015, 2015 Technologies Corporation.
******************************************************************************/
package test.muban;
import java.awt.FlowLayout;
import java.awt.Frame;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.SwingUtilities;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.sun.opengl.util.FPSAnimator;
public class BasicFrame implements GLEventListener {
static Display display;
static Shell shell;
static FPSAnimator animator;
public Frame creatSWT() {
display = Display.getDefault();
shell = new Shell();
shell.setSize(550, 550);
shell.setText("SWT_OpenGL");
shell.setLayout(null);
final Composite compSWT = new Composite(shell, SWT.EMBEDDED);// SWT.EMBEDDED必须
compSWT.setSize(500, 500);
compSWT.setLocation(0, 0);
final java.awt.Frame frame = SWT_AWT.new_Frame(compSWT);
frame.setLayout(new FlowLayout());
shell.layout();
shell.open();
return frame;
}
public static void main(final String[] args) {
final GLCapabilities capabilities = new GLCapabilities();
final GLCanvas glcanvas = new GLCanvas(capabilities);// 创建画布
final BasicFrame basicFrame = new BasicFrame();
glcanvas.addGLEventListener(basicFrame);// basicFrame重写了GLEventListener的画图的方法
glcanvas.setSize(500, 500);
animator = new FPSAnimator(glcanvas, 10, true);
final Frame frame = basicFrame.creatSWT();
frame.add(glcanvas);// 把画布放进 窗口内
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
animator.start(); // 开始动画线程
}
}
);
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
animator.stop();
display.dispose();
}
@Override
public void init(final GLAutoDrawable drawable) { // 初始函数
final GL gl = drawable.getGL();
final GLU glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 250.0f, 1f); // 设置背景颜色
gl.glViewport(0, 0, 100, 100); // 视点大小
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
// 裁剪横坐标(left,right)纵坐标(bottom,top)范围内的视图,放进GL可见视图中
glu.gluOrtho2D(0.0, 100, 0.0, 100.0); // 使坐标系统出现在GL里,此时屏幕中最左面是坐标0,右面是500,最下0,嘴上500
}
@Override
public void display(final GLAutoDrawable drawable) { // 画图函数
final GL gl = drawable.getGL(); // 从GLAutoDrawable获取GL
gl.glClear(GL.GL_COLOR_BUFFER_BIT); // 填充背景颜色
gl.glColor3f(250.0f, 0.0f, 0.0f); // 设置GL的画图颜色,也就是画刷的颜色
gl.glBegin(GL.GL_LINES);
gl.glVertex2d(5, 5);
gl.glVertex2d(95, 5);
gl.glVertex2d(5, 95);
gl.glVertex2d(95, 95);
gl.glVertex2d(5, 50);
gl.glVertex2d(95, 50);
gl.glVertex2d(50, 5);
gl.glVertex2d(50, 95);
gl.glEnd();
gl.glFlush();
System.out.println("1");
}
@Override
public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
}
@Override
public void displayChanged(final GLAutoDrawable drawable, final boolean modeChanged, final boolean deviceChanged) {
}
}
jogl:在SWT中使用OpenGL的模板(SWT_AWT桥接)
最新推荐文章于 2021-03-04 00:31:44 发布