package editor;
import com.sun.opengl.util.Animator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.media.opengl.*;
public class OGL_editor extends JFrame implements GLEventListener,KeyListener,MouseListener,
MouseWheelListener, MouseMotionListener, ActionListener
{
public int angle;
public JFrame frame;
public JMenuBar meinMenu;
public Animator animator;
public GridBagLayout gbl;
public GraphicsDevice myDevice;
private JPanel pnl_object_choice;
private JPanel pnl_object_properties;
private JTextArea txt;
private GLCanvas canvas;
private JScrollPane scrollingArea;
public OGL_editor(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
angle=0; //nur test für OpenGl-Bewegung
//frame erzeugen und Listener Registrieren
frame=new JFrame("Mein TEST");
frame.addKeyListener(this);
frame.setBounds(0,0,800,600);
//Menü erzeugen
create_menu();
//Panels erzeugen
create_panel();
create_panel_properties();
//canvas erzeugen
create_canvas();
animator = new Animator(canvas);
animator.start();
//LAYOUT ausrichten
Layout();
//Falls FS möglich ist, dann FS
myDevice= GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
txt.append("Fullscreen-Support: "+myDevice.isFullScreenSupported());
if(myDevice.isFullScreenSupported()){
frame.setResizable(false);
//Fehler tritt nur auf, wenn folgende 2 Zeilen einkommentiert sind
myDevice.setFullScreenWindow(frame);
myDevice.setDisplayMode(new DisplayMode(1024,768,32,0));
}
frame.setVisible(true);
}
public static void main(String[] args) {
OGL_editor fenster = new OGL_editor();
}
private void Layout(){
GridBagLayout gbl = new GridBagLayout();
frame.setLayout(gbl);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx=1;//Container im Frame Maximieren
c.weighty=1;//Container im Frame Maximieren
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 2;
frame.add(pnl_object_choice, c);
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 2;
frame.add(pnl_object_properties, c);
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
frame.add(canvas, c);
txt = new JTextArea(1,10);
txt.setEditable(false);
scrollingArea = new JScrollPane(txt);
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
frame.add(scrollingArea, c);
}
private GLCanvas create_canvas(){
canvas = new GLCanvas();
canvas.addGLEventListener(this);
return canvas;
}
private void create_menu(){
meinMenu=new JMenuBar();
//Menutest
JMenu datei =new JMenu("Datei");
meinMenu.add(datei);
JMenuItem oeffnen= new JMenuItem("Öffnen");
JMenuItem schliessen= new JMenuItem("Schließen");
schliessen.addActionListener(this);
datei.add(oeffnen);
datei.add(schliessen);
frame.setJMenuBar(meinMenu);
}
private JPanel create_panel(){
pnl_object_choice = new JPanel();
pnl_object_choice.add(new JLabel("Objektauswahl"));
pnl_object_choice.setBackground(new Color(255,255,0));
return pnl_object_choice;
}
private JPanel create_panel_properties(){
pnl_object_properties = new JPanel();
pnl_object_properties.add(new JLabel("Objekteigenschaften"));
pnl_object_properties.setBackground(new Color(255,0,0));
return pnl_object_properties;
}
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glEnable(GL.GL_NORMALIZE);
drawable.addMouseListener(this); //Setzt für dieses Objekt Listener für MouseEvents
drawable.addMouseMotionListener(this);//Setzt für dieses Objekt Listener für MouseMove
drawable.addMouseWheelListener(this);//Setzt für dieses Objekt Listener für MouseWheel
drawable.addKeyListener(this);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
float h = (float)height / (float)width;
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glScaled(0.5,0.5,0.5); //bestimmt die Sklalierung
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Standard hintergrundfarbe
//procedure glFrustum(left, right, bottom, top, zNear, zFar : glDouble);
gl.glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0,0,-20);
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
// Clear the drawing area
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Reset the current matrix to the "identity"
gl.glLoadIdentity();
// Move the "drawing cursor" around
gl.glTranslatef(-1.5f, 0.0f, -6.0f);
gl.glRotatef(angle, 1, 0,0);
// Draw A Quad
gl.glBegin(GL.GL_QUADS);
gl.glColor3f(0.5f, 0.5f, 1.0f); // Set the current drawing color to light blue
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
gl.glVertex3f(1.0f, 1.0f, 0.0f); // Top Right
gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
// Done Drawing The Quad
gl.glEnd();
// Flush all drawing operations to the graphics card
gl.glFlush();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
public void actionPerformed(ActionEvent ae){
JMenuItem mi =(JMenuItem)ae.getSource();
String menupunkt=mi.getText();
if(menupunkt=="Schließen")
System.exit(0);
txt.append(mi.getText());
}
@Override
public void keyPressed(KeyEvent e)
{
// TODO Auto-generated method stub
txt.append("\nKeyCode: "+e.getKeyCode());
angle+=10;
if(e.getKeyCode()==27){
System.exit(0);
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseWheelMoved(MouseWheelEvent e){}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void windowClosing (WindowEvent event)
{
System.exit(0);
}
}