JOGL學習(1)—— 第一個JOGL程序

這裏寫一個最簡單的JOGL程序,證明配置成功,並且做一點簡單的說明:

  • JOGL就是重寫GLEventListener裏面的一些方法,用過swing畫圖的應該知道,通過repaint來實現圖形顯示,這裏就是重寫display來實現繪圖。
  • 下面的程序實現了最簡單的在畫布上添加GLEventListener
package t1;

import java.awt.Frame;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;

//繼承Lister接口
public class T1 implements GLEventListener {
	//顯示函數,後面都在這個裏面設置圖形形狀等
   @Override
   public void display(GLAutoDrawable arg0) {
      // method body
   }
   
  //釋放資源
   @Override
   public void dispose(GLAutoDrawable arg0) {
      //method body
   }
   
   //初始化,調用GL等功能
   @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);
      //實例化對象
      T1 b = new T1();
      glcanvas.addGLEventListener(b);        
      glcanvas.setSize(800, 800);
      //creating frame
      final Frame frame = new Frame (" Basic Frame");
      //adding canvas to frame
      frame.add(glcanvas);
      //初始化窗口大小
      frame.setSize( 1800, 1800 );
      frame.setVisible(true);
   }
}

 

上面的程序有點亂,代碼全都在main函數裏面,這裏再貼一個簡單的:

  • 該程序實現了全屏藍色代碼,使用glClear方法把整個屏幕都畫成藍色
package led;

import javax.swing.JFrame;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;

public class Test1 implements GLEventListener{
	private GL2 gl;
	@Override
	public void init(GLAutoDrawable drawable) {
		gl = drawable.getGL().getGL2();
	}
	@Override
	public void dispose(GLAutoDrawable drawable) {
		
	}
	@Override
	public void display(GLAutoDrawable drawable) {
		gl.glClearColor(0.0f,0.0f,1.0f,1.0f);
		gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
		gl.glFlush();
	}
	@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		
	}

	public static void main(String[] args) {
		GLCanvas canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL2)));
		Test1 opengl = new Test1();
		canvas.addGLEventListener(opengl);
		
		canvas.setSize(500,500);
		JFrame frame = new JFrame("第一個JOGL程序");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		frame.getContentPane().add(canvas);
		frame.setSize(frame.getContentPane().getPreferredSize());
		frame.setVisible(true);
		
	}
}
  • 顯示效果:有放大,縮小,關閉功能

 

這個程序和上面程序對比,就很容易看出canvas在frame中的作用

package t1;

import javax.swing.JFrame;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;

public class T2 implements GLEventListener{
	private GL2 gl;
	@Override
	public void init(GLAutoDrawable drawable) {
		gl = drawable.getGL().getGL2();
	}
	@Override
	public void dispose(GLAutoDrawable drawable) {
		
	}
	@Override
	public void display(GLAutoDrawable drawable) {
		gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
		gl.glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
		gl.glFlush();
	}
	@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		
	}

	public static void main(String[] args) {
		GLCanvas canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL2)));
		T2 opengl = new T2();
		canvas.addGLEventListener(opengl);
		
		canvas.setSize(500,500);
		JFrame frame = new JFrame("第一個JOGL程序");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		frame.getContentPane().add(canvas);
		frame.setSize(frame.getContentPane().getPreferredSize());
		frame.setVisible(true);
		
	}
}

在主方法中,創建一個GLCanvas對象canvas,一個JFrame對象frame,把canvas加到frame中,並給他們設置尺寸。而想讓自己繪製的東西出現在canvas上,就必須爲canvas添加GL監聽事件(GLEventListener),我們編寫的OpenGL類實現了GLEventListener接口。所以爲canvas添加OpenGL對象作爲監聽事件即可,而我們的繪圖程序就寫在OpenGL中的display()方法之中,這個方法是實現了GLEventListener中的抽象方法。

  • OpenGL類要實現如下幾個抽象方法:
  • 所有的方法都是由GLAutoDrawable接口對象觸
  • GLAutoDrawable接口對象創建的時候被調用,而這個接口對象是在GLCanvas對象加入窗口部件時自動創建的。這個方法可以被多次調用,例如:GLCanvas對象從窗口部件中移除了然後再次加入。

public void init(GLAutoDrawable drawable);
  • 當組件的大小尺寸被改變時候調用,第一次繪製的時候也會調用。

public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
  • 在reshape之後調用

public void display(GLAutoDrawable drawable);
  • 在GLCanvas對象從窗口部件中移除的時候調用,清理回收資源

public void dispose(GLAutoDrawable drawable);
  • 顯示效果:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章