如何使用JOgl在jPanel上绘制

时间:2015-11-26 11:22:52

标签: netbeans jogl

我在Netbeans 8.0.2中使用JOGL。我想在面板上绘制经典的彩色三角形,并使用Netbeans调色板控件选择几何图形的边数(4,5边),但我不知道如何。面板在框架上。到目前为止,我的代码是:

Class 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)。

1 个答案:

答案 0 :(得分: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;
    }
}

将MyJoglFigure从调色板拖到JFrame设计后,您可以找到并编辑numberOfSides属性。请注意,我只打印它,我实际上并没有用它来改变绘图。 (为读者锻炼?)