2015-11-26 62 views
0

我在Netbeans 8.0.2中使用JOGL。我想在面板上繪製經典的彩色三角形,並且使用Netbeans調色板控件選擇幾何圖形的邊數(4,5側),但我不知道如何。面板在框架上。到目前爲止,我的代碼是:如何使用JOgl在jPanel上繪圖

類PanelGL:

package panelgl; 

import com.jogamp.opengl.*; 
import com.jogamp.newt.event.WindowAdapter; 
import com.jogamp.newt.event.WindowEvent; 
import com.jogamp.newt.opengl.GLWindow; 
import com.jogamp.opengl.GLCapabilities; 
import com.jogamp.opengl.awt.GLCanvas; 
import com.jogamp.opengl.awt.GLJPanel; 
import java.awt.Dimension; 
import javax.swing.JFrame; 

public class PanelGL implements GLEventListener{ 

     public static void main(String[] args) { 
      final GLProfile profile = GLProfile.get(GLProfile.GL2); 
      GLCapabilities capabilities = new GLCapabilities(profile); 

      final GLJPanel glpanel = new GLJPanel(capabilities); 
      PanelGL triangle = new PanelGL(); 
      glpanel.addGLEventListener(triangle); 
      glpanel.setSize(400, 400); 

      final GLFrame frame = new GLFrame ("Colored Triangle"); 

      frame.getContentPane().add(glpanel); 
      frame.setSize(frame.getContentPane().getPreferredSize()); 
      frame.setVisible(true); 
     } 

     @Override 
     public void init(GLAutoDrawable glad) { } 

     @Override 
     public void dispose(GLAutoDrawable glad) { } 

     @Override 
     public void display(GLAutoDrawable glad) {  
       final GL2 gl = glad.getGL().getGL2(); 
       gl.glBegin(GL2.GL_TRIANGLES); 
       gl.glColor3f(1.0f, 0.0f, 0.0f); // Red 
       gl.glVertex3f(0.5f,0.7f,0.0f); // Top 
       gl.glColor3f(0.0f,1.0f,0.0f);  // green 
       gl.glVertex3f(-0.2f,-0.50f,0.0f); // Bottom Left 
      gl.glColor3f(0.0f,0.0f,1.0f);  // blue 
      gl.glVertex3f(0.5f,-0.5f,0.0f); // Bottom Right 

      gl.glEnd();  
     } 

    @Override 
    public void reshape(GLAutoDrawable glad, int i, int i1, int i2, int i3) { } 

} 

類GLFrame

 public class GLFrame extends javax.swing.JFrame { 

     public GLFrame(String title) { 
      this.setTitle(title); 
      initComponents(); 
     } 

      private javax.swing.JPanel glPanel; 
    } 

glPanel是通過調色板增加。

我想要繪製在這個具體面板(glPanel)。

回答

0

創建一個擴展GLJPanel而不是GLEventListener的類。你仍然會在內部使用GLEventListener。編輯完課程後,您可以在彈出菜單的「項目」窗口中右鍵單擊它,以選擇Tools - >Add To Palette ...將其添加到調板中。要創建一個可編輯的屬性,只需在getter和setter中使用正常的bean模式,或者使用Alt-Insert,然後選擇Add Property來添加變量以及getter和setter。

如..

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


public class MyJOGLFigure extends GLJPanel { 

    public MyJOGLFigure() { 
     this.setPreferredSize(new Dimension(100,100)); 
     final GLProfile profile = GLProfile.get(GLProfile.GL2); 
     GLCapabilities capabilities = new GLCapabilities(profile); 
     this.addGLEventListener(new GLEventListener() { 

      @Override 
      public void init(GLAutoDrawable glad) { 

      } 

      @Override 
      public void dispose(GLAutoDrawable glad) { 

      } 

      @Override 
      public void display(GLAutoDrawable glad) { 
       System.out.println("numberOfSides = " + numberOfSides); 
       final GL2 gl = glad.getGL().getGL2(); 
       gl.glBegin(GL2.GL_TRIANGLES); 
       gl.glColor3f(1.0f, 0.0f, 0.0f); // Red 
       gl.glVertex3f(0.5f, 0.7f, 0.0f); // Top 
       gl.glColor3f(0.0f, 1.0f, 0.0f);  // green 
       gl.glVertex3f(-0.2f, -0.50f, 0.0f); // Bottom Left 
       gl.glColor3f(0.0f, 0.0f, 1.0f);  // blue 
       gl.glVertex3f(0.5f, -0.5f, 0.0f); // Bottom Right 

       gl.glEnd(); 
      } 

      @Override 
      public void reshape(GLAutoDrawable glad, int i, int i1, int i2, int i3) { 

      } 
     }); 
    } 

    // Example editable property. 
    private int numberOfSides = 3; 

    /** 
    * Get the value of numberOfSides 
    * 
    * @return the value of numberOfSides 
    */ 
    public int getNumberOfSides() { 
     return numberOfSides; 
    } 

    /** 
    * Set the value of numberOfSides 
    * 
    * @param numberOfSides new value of numberOfSides 
    */ 
    public void setNumberOfSides(int numberOfSides) { 
     this.numberOfSides = numberOfSides; 
    } 
} 

你從調色板到JFrame設計拖着MyJoglFigure後,你可以找到和編輯numberOfSides財產。請注意,我只打印它,我實際上並未使用它來更改圖紙。 (爲讀者鍛鍊?)