ECLIPSE环境下 JOGL的配置请参照第一章。
package com.jogl.first;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
public class SecondJoglApp extends JFrame{
public static Animator animator=null;
public GLCanvas canvas=null;
public SecondJoglApp() throws HeadlessException {
super("第二个JOGL");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//初始化GLCapabilities
GLCapabilities glcaps = new GLCapabilities();
canvas = new GLCanvas(glcaps); //初试化GLcanvas,用法参照第一章
canvas.addGLEventListener(new SecondGLEventListener()); //GLcanvas添加GLEventListener
getContentPane().add(canvas, BorderLayout.CENTER); //给窗体panel添加GLcanvas
animator=new FPSAnimator(canvas,10,true); //本章重点,给canvas添加动画线程,每秒10帧
setSize(500, 300);
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
);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
final SecondJoglApp app = new SecondJoglApp();
//显示窗体
SwingUtilities.invokeLater (
new Runnable() {
public void run() {
app.setVisible(true);
}
}
);
SwingUtilities.invokeLater (
new Runnable() {
public void run() {
animator.start( ); //开始动画线程
}
}
);
}
}
package com.jogl.first;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.SwingUtilities;
public class SecondGLEventListener implements GLEventListener {
public SecondGLEventListener() {
// TODO Auto-generated constructor stub
}
@Override
public void display(GLAutoDrawable arg0) { //画图函数
// TODO Auto-generated method stub
float red = (float) ((Math.random())*(1.0f)); //随机红
float green = (float) ((Math.random())*(1.0f)); //随机绿
float blue = (float) ((Math.random())*(1.0f)); //随机蓝
GL gl = arg0.getGL(); //从GLAutoDrawable获取GL
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的画图颜色,也就是画刷的颜色
gl.glBegin(GL.GL_POINTS);
gl.glVertex2i((i*10), 150); //画点由glBegin(GL.GL_POINTS)开始,glEnd()结束
gl.glEnd();
}
}
@Override
public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
// TODO Auto-generated method stub
}
@Override
public void init(GLAutoDrawable gld) { //初始函数
// TODO Auto-generated method stub
GL gl = gld.getGL();
GLU glu = new GLU();
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); //使坐标系统出现在GL里
}
@Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
int arg4) {
// TODO Auto-generated method stub
}
}
运行效果: 点阵将不停闪动,各位可以尝试把程序改为多行点阵,或者竖的,多玩玩,找找乐趣,获益会更多,
佛语: 帮别人成就功德,本身就是一种功德