Hilfe: JOGL / OPENGL in Frame
Hallo,
Ich habe angefangen ein Programm zu schreiben, bei dem ich ein Koordinatensystem zeichne und ein Würfel drin habe. Dieses Fenster habe ich in ein Frame eingebaut. Ich habe 3 Schieberegler eingebaut in den Frame, mitdem ich mein Korrdinatensystem in dem gewünschten Winkel verdrehen kann.
Jedoch besteht mein Problem darin, das ich nicht vom Hauptfenster in den Panel zugreifen kann...
Ich möchte die Parameter durch get- und set-Methoden zu display() schicken, jedoch kann ich nicht auf die Methoden zugreifen.
Hallo,
Ich habe angefangen ein Programm zu schreiben, bei dem ich ein Koordinatensystem zeichne und ein Würfel drin habe. Dieses Fenster habe ich in ein Frame eingebaut. Ich habe 3 Schieberegler eingebaut in den Frame, mitdem ich mein Korrdinatensystem in dem gewünschten Winkel verdrehen kann.
Jedoch besteht mein Problem darin, das ich nicht vom Hauptfenster in den Panel zugreifen kann...
Ich möchte die Parameter durch get- und set-Methoden zu display() schicken, jedoch kann ich nicht auf die Methoden zugreifen.
Java:
package org.yourorghere;
import com.sun.opengl.util.Animator;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLJPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
/**
*
* @author cylab
* @author mbien
*/
public class SimpleGLJPanel extends JFrame implements MouseListener {
private Animator animator;
/** Creates new form MainFrame */
public SimpleGLJPanel() {
initComponents();
setTitle("Simple JOGL Application");
panel.addGLEventListener(new GLRenderer());
animator = new Animator(panel);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
}
@Override
public void setVisible(boolean show){
if(!show)
animator.stop();
super.setVisible(show);
if(!show)
animator.start();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
JLabel label = new JLabel();
panel = new GLJPanel(createGLCapabilites());
jSlider1 = new JSlider();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
label.setText("Below you see a GLJPanel");
GroupLayout panelLayout = new GroupLayout(panel);
panel.setLayout(panelLayout);
panelLayout.setHorizontalGroup(
panelLayout.createParallelGroup(Alignment.LEADING)
.addGap(0, 458, Short.MAX_VALUE)
);
panelLayout.setVerticalGroup(
panelLayout.createParallelGroup(Alignment.LEADING)
.addGap(0, 257, Short.MAX_VALUE)
);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label)
.addComponent(jSlider1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addContainerGap(125, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(label)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jSlider1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(120, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* Called from within initComponents().
* hint: to customize the generated code choose 'Customize Code' in the contextmenu
* of the selected UI Component you wish to cutomize in design mode.
* @return Returns customized GLCapabilities.
*/
private GLCapabilities createGLCapabilites() {
GLCapabilities capabilities = new GLCapabilities();
capabilities.setHardwareAccelerated(true);
// try to enable 2x anti aliasing - should be supported on most hardware
capabilities.setNumSamples(2);
capabilities.setSampleBuffers(true);
return capabilities;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
// Run this in the AWT event thread to prevent deadlocks and race conditions
// panel.addKeyListener();
EventQueue.invokeLater(new Runnable() {
public void run() {
// switch to system l&f for native font rendering etc.
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.INFO, "can not enable system look and feel", ex);
}
SimpleGLJPanel frame = new SimpleGLJPanel();
frame.setVisible(true);
}
});
}
// Variables declaration - do not modify
private JSlider jSlider1;
private GLJPanel panel;
// End of variables declaration
public void mouseClicked(MouseEvent e) {
int i = e.getX();
System.out.println(i);
}
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
} [/TABLE]
[TABLE]package org.yourorghere;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.GLUT;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
/**
* GLRenderer.java <BR>
* author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P>
*
* This version is equal to Brian Paul's version 1.2 1999/10/21
*/
public class GLRenderer implements GLEventListener, KeyListener, MouseListener {
public static void main(String[] args) {
Frame frame = new Frame("Simple JOGL Application");
GLCanvas canvas = new GLCanvas();
canvas.addGLEventListener((GLEventListener) new SimpleGLJPanel());
frame.add(canvas);
frame.setSize(640, 480);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
// Center frame
frame.setLocationRelativeTo(null);
frame.setVisible(true);
animator.start();
}
public void init(GLAutoDrawable drawable) {
// Use debug pipeline
// drawable.setGL(new DebugGL(drawable.getGL()));
GL gl = drawable.getGL();
drawable.addKeyListener(this);
drawable.addMouseListener(this);
// Setup the drawing area and shading mode
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glEnable(gl.GL_DEPTH_TEST);
gl.glClearDepth(1.0);
gl.glDepthFunc(GL.GL_LESS);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
GLU glu = new GLU();
gl.glViewport(x,y,width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
if (width <= height) {
gl.glOrtho(-2,2,-2*((double) height)/(double) width,2*((double) height)/(double)width,-5,5);
}
else{ gl.glOrtho( -2*((double) width)/(double) height ,2*((double)width )/(double)height,-2,2,-5,5);}
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(0.5, 0.5, 0.5, 0, 0, 0, 0, 1, 0);
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
GLUT glut = new GLUT();
for(int i = 0; i < 6; i++) {
gl.glPushMatrix();
//gl.glRotatef(viewRotX, 1.0f, 0.0f, 0.0f);
//gl.glRotatef(viewRotY, 0.0f, 1.0f, 0.0f);
gl.glColor3f(1.0f,1.0f,1.0f);
glut.glutWireCube(0.8f);
gl.glColor3f(1.0f, 0.5f, 0.0f); // ORANGE
if (i < 4) {gl.glRotatef(i * 90.0f, 0.0f, 1.0f, 0.0f);}
if (i == 4) {gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);}
if (i == 5) {gl.glRotatef(90.0f,-1.0f, 0.0f, 0.0f);}
gl.glTranslatef(0.0f, 0.0f, 1.5f);
gl.glBegin(GL.GL_LINES); {
gl.glVertex3f( 0.0f, 0.0f, 0.0f);
gl.glVertex3f( 0.0f, 0.0f,-1.5f);}
gl.glEnd();
glut.glutWireCone(0.05f, 0.25f, 10, 5);
gl.glRasterPos3f(-0.1f, 0.1f, 0.4f);
switch (i) {
case 0: drawText("Z"); break;
case 1: drawText("X"); break;
case 2: drawText("-Z"); break;
case 3: drawText("-X"); break;
case 4: drawText("-Y"); break;
case 5: drawText("Y"); break;
}
gl.glPopMatrix();
}
}
private void drawText (String text){
GLUT glut = new GLUT();
for (int i = 0; i < text.length(); i++)
{ glut.glutBitmapCharacter(GLUT.BITMAP_9_BY_15, text.charAt(i));}}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void keyPressed(KeyEvent e) {
System.out.println("38");
}
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseClicked(MouseEvent e) {
int i = e.getX();
System.out.println(i);
}
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
//public class MyGLEventListener Implements GLEventListener, KeyListener {
//
//
//}
}
Zuletzt bearbeitet von einem Moderator: