当前页面: 开发资料首页 → Java 专题 → 你好,JOGL - Java与OpenGl的绑定
摘要: 你好,JOGL - Java与OpenGl的绑定
import net.java.games.jogl.*;
public class HelloWorld {
public static void main (String args[]) {
try {
System.loadLibrary("jogl");
System.out.println(
"Hello World! (The native libraries are installed.)"
);
GLCapabilities caps = new GLCapabilities();
System.out.println(
"Hello JOGL! (The jar appears to be available.)"
);
} catch (Exception e) {
System.out.println(e);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.java.games.jogl.*;
/**
* This is a basic JOGL app. Feel free to
* reuse this code or modify it.
* 这是个基础的JOGL程序,你可以随意重用该代码或者修改它。
*/
public class SimpleJoglApp extends JFrame {
public static void main(String[] args) {
final SimpleJoglApp app = new SimpleJoglApp();
// show what we've done
//看一下我们做了什么
SwingUtilities.invokeLater (
new Runnable() {
public void run() {
app.setVisible(true);
}
}
);
}
public SimpleJoglApp() {
//set the JFrame title
//设置JFrame标题
super("Simple JOGL Application");
//kill the process when the JFrame is closed
//当JFrame关闭的时候,结束进程
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//only three JOGL lines of code ... and here they are
//只有三行JOGL代码 ... 如下
GLCapabilities glcaps = new GLCapabilities();
GLCanvas glcanvas =
GLDrawableFactory.getFactory().createGLCanvas(glcaps);
glcanvas.addGLEventListener(new SimpleGLEventListener());
//add the GLCanvas just like we would any Component
//像其它组件一样把GLCanvas加入
getContentPane().add(glcanvas, BorderLayout.CENTER);
setSize(500, 300);
//center the JFrame on the screen
//使JFrame显示在屏幕中央
centerWindow(this);
}
public void centerWindow(Component frame) {
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.width > screenSize.width )
frameSize.width = screenSize.width;
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
frame.setLocation (
(screenSize.width - frameSize.width ) >> 1,
(screenSize.height - frameSize.height) >> 1
);
}
}
GLCanvas glcanvas =
GLDrawableFactory.getFactory().createGLCanvas(glcaps);
import java.awt.*;
import java.awt.event.*;
import net.java.games.jogl.*;
/**
* For our purposes only two of the
* GLEventListeners matter. Those would
* be init() and display().
* 为了达到我们的目的,GLEventListener中只有两个方法有用。
* 它们是init()和display()。
*/
public class SimpleGLEventListener implements GLEventListener
{
/**
* Take care of initialization here.
* 注意这里的初始化。
*/
public void init(GLDrawable drawable) {
}
/**
* Take care of drawing here.
* 注意这里的绘图。
*/
public void display(GLDrawable drawable) {
}
/**
* Called when the GLDrawable (GLCanvas
* or GLJPanel) has changed in size. We
* won't need this, but you may eventually
* need it -- just not yet.
* 当GLDrawable(GLCanvas或GLJPanel)大小改变时被调用。
* 我们不需要它,但你可能最后会用到——虽然现在并不需要。
*/
public void reshape(
GLDrawable drawable,
int x,
int y,
int width,
int height
) {}
/**
* If the display depth is changed while the
* program is running this method is called.
* Nowadays this doesn't happen much, unless
* a programmer has his program do it.
* 当程序运行时显示深度被改变的时候此方法被调用。
* 现在这种事发生得不多,除非程序里面触发此事。
*/
public void displayChanged(
GLDrawable drawable,
boolean modeChanged,
boolean deviceChanged
) {}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.java.games.jogl.*;
/**
* This is a basic JOGL app. Feel free to
* reuse this code or modify it.
* 这是个基础的JOGL程序,你可以随意重用该代码或者修改它。
*/
public class SecondJoglApp extends JFrame {
public static void main(String[] args) {
final SecondJoglApp app = new SecondJoglApp();
//show what we've done
//看一下我们做了什么
SwingUtilities.invokeLater (
new Runnable() {
public void run() {
app.setVisible(true);
}
}
);
}
public SecondJoglApp() {
//set the JFrame title
//设置JFrame标题
super("Second JOGL Application");
//kill the process when the JFrame is closed
//当JFrame关闭的时候,结束进程
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//only three JOGL lines of code ... and here they are
//只有三行JOGL代码 ... 如下
GLCapabilities glcaps = new GLCapabilities();
GLCanvas glcanvas =
GLDrawableFactory.getFactory().createGLCanvas(glcaps);
glcanvas.addGLEventListener(new SecondGLEventListener());
//add the GLCanvas just like we would any Component
//像其它组件一样把GLCanvas加入
getContentPane().add(glcanvas, BorderLayout.CENTER);
setSize(500, 300);
//center the JFrame on the screen
//使JFrame显示在屏幕中央
centerWindow(this);
}
public void centerWindow(Component frame) {
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.width > screenSize.width )
frameSize.width = screenSize.width;
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
frame.setLocation (
(screenSize.width - frameSize.width ) >> 1,
(screenSize.height - frameSize.height) >> 1
);
}
}
import net.java.games.jogl.*;
/**
* For our purposes only two of the GLEventListeners matter.
* Those would be init() and display().
* 为了达到我们的目的,GLEventListener中只有两个方法有用。
* 它们是init()和display()。
*/
public class SecondGLEventListener implements GLEventListener
{
/**
* Take care of initialization here.
* 注意这里的初始化。
*/
public void init(GLDrawable gld) {
GL gl = gld.getGL();
GLU glu = gld.getGLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glViewport(0, 0, 500, 300);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
}
/**
* Take care of drawing here.
* 注意这里的绘图。
*/
public void display(GLDrawable drawable) {
float red = 0.0f;
float green = 0.0f;
float blue = 0.0f;
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glPointSize(5.0f);
for (int i=0; i<50; i++) {
red -= .09f;
green -= .12f;
blue -= .15f;
if (red < 0.15) red = 1.0f;
if (green < 0.15) green = 1.0f;
if (blue < 0.15) blue = 1.0f;
gl.glColor3f(red, green, blue);
gl.glBegin(GL.GL_POINTS);
gl.glVertex2i((i*10), 150);
gl.glEnd();
}
}
public void reshape(
GLDrawable drawable,
int x,
int y,
int width,
int height
) {}
public void displayChanged(
GLDrawable drawable,
boolean modeChanged,
boolean deviceChanged
) {}
}