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