JAVA的OPENGL,JOGL入门实例----不断变色的点阵 (源代码)

不断变色的点阵 (源代码)

第一个类(代码解读)

package test1.opengl;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
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

		final 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(final Component frame) { // 把窗体居中

		final Dimension screenSize =

		Toolkit.getDefaultToolkit().getScreenSize();

		final 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(final String[] args) {

		// TODO Auto-generated method stub

		final SecondJoglApp app = new SecondJoglApp();

		// 显示窗体

		SwingUtilities.invokeLater(

		new Runnable() {

			@Override
			public void run() {

				app.setVisible(true);

			}

		}

		);

		SwingUtilities.invokeLater(

		new Runnable() {

			@Override
			public void run() {

				animator.start(); // 开始动画线程

			}

		}

		);

	}

}

第二个类

package test1.opengl;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

public class SecondGLEventListener implements GLEventListener {

	public SecondGLEventListener() {

		// TODO Auto-generated constructor stub

	}

	@Override
	public void display(final 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); // 随机蓝

		final 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(final GLAutoDrawable arg0, final boolean arg1, final boolean arg2) {

		// TODO Auto-generated method stub

	}

	@Override
	public void init(final GLAutoDrawable gld) { // 初始函数

		// TODO Auto-generated method stub

		final GL gl = gld.getGL();

		final 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(final GLAutoDrawable arg0, final int arg1, final int arg2, final int arg3,

	final int arg4) {

		// TODO Auto-generated method stub

	}

}

JAVA的OPENGL,JOGL入门实例----不断变色的点阵 (源代码)_第1张图片

你可能感兴趣的:(Java)