如何使用JOgl在jPanel上绘制



我在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)。

创建一个扩展GLJPanel而不是GLEventListener的类。您仍然可以在内部使用GLEventListener。编译完类后,可以在弹出菜单的Projects窗口中右键单击它,选择Tools->Add To Palette ...将其添加到调色板中。要创建一个可编辑的属性,只需使用带有getter和setter的普通bean模式,或者使用Alt-Insert,然后选择Add Property来添加变量以及getter和seter。

例如。。

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属性。注意,我只是打印它,实际上我并没有用它来更改图纸。(读者练习?)

相关内容

  • 没有找到相关文章

最新更新