import net.java.games.jogl.*; import java.awt.*; import java.awt.event.*; import java.beans.*; import javax.swing.*; import javax.swing.event.*; public class Main extends JFrame { private JMenuBar menubar; private JMenu fileMenu, testMenu; private JMenuItem fileExit; private JMenuItem testStartLeft, testStartRight; private JMenuItem testStopLeft, testStopRight; private JMenuItem testStartAll; private JSplitPane verticalSplitPane; private JSplitPane horizontalSplitPane; private TestPanel leftPanel, rightPanel, bottomPanel; public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { Main m = new Main(); m.setVisible(true); MyAnimator anim = m.new MyAnimator(); anim.start(); } }); } public Main() { // This call ensures that the JMenuBar will render over the GLCanvas. JPopupMenu.setDefaultLightWeightPopupEnabled(false); // Create components. verticalSplitPane = new JSplitPane(); horizontalSplitPane = new JSplitPane(); menubar = new JMenuBar(); fileMenu = new JMenu(); testMenu = new JMenu(); fileExit = new JMenuItem(); testStartLeft = new JMenuItem(); testStartRight = new JMenuItem(); testStopLeft = new JMenuItem(); testStopRight = new JMenuItem(); testStartAll = new JMenuItem(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); verticalSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); getContentPane().add(verticalSplitPane, BorderLayout.CENTER); verticalSplitPane.setTopComponent(horizontalSplitPane); fileMenu.setText("File"); testMenu.setText("Test"); fileExit.setText("Exit"); fileExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { fileExitActionPerformed(evt); } }); testStartLeft.setText("Start Left Pane"); testStartLeft.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { leftPanel.start(); } }); testStartRight.setText("Start Right Pane"); testStartRight.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { rightPanel.start(); } }); testStopLeft.setText("Stop Left Pane"); testStopLeft.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { leftPanel.stop(); } }); testStopRight.setText("Stop Right Pane"); testStopRight.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { rightPanel.stop(); } }); testStartAll.setText("Start All"); testStartAll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { rightPanel.start(); leftPanel.start(); bottomPanel.start(); } }); fileMenu.add(fileExit); testMenu.add(testStartLeft); testMenu.add(testStopLeft); testMenu.add(testStartRight); testMenu.add(testStopRight); testMenu.add(testStartAll); menubar.add(fileMenu); menubar.add(testMenu); setJMenuBar(menubar); leftPanel = new TestPanel(); rightPanel = new TestPanel(); bottomPanel = new TestPanel(); horizontalSplitPane.setLeftComponent(leftPanel); horizontalSplitPane.setRightComponent(rightPanel); verticalSplitPane.setBottomComponent(bottomPanel); setSize(800, 600); horizontalSplitPane.setDividerLocation(400); verticalSplitPane.setDividerLocation(300); } private void fileExitActionPerformed(ActionEvent evt) { System.exit(0); } public class TestPanel extends JPanel { GLCanvas canvas; Animator animator; boolean started; public TestPanel() { GLCapabilities cap=new GLCapabilities(); cap.setHardwareAccelerated(true); cap.setDoubleBuffered(true); // The JPanel must use the Border layout manager. setLayout(new BorderLayout()); // Create the GLCanvas. canvas = GLDrawableFactory.getFactory().createGLCanvas(cap); canvas.addGLEventListener(new TestRenderer()); // Create an animator thread for the canvas. // animator = new Animator(canvas); // Add the canvas to the center of the JPanel. add(canvas, BorderLayout.CENTER); // We need to create a Dimension object for the JPanel minimum size to fix a GLCanvas resize bug. // The GLCanvas normally won't recieve resize events that shrink a JPanel controled by a JSplitPane. setMinimumSize(new Dimension()); } public void draw() { canvas.display(); } // public void start(){ // animator.start(); // } // public void stop(){ // animator.stop(); // } public void start(){ started = true; } public void stop(){ started = false; } public boolean isStarted() { return started; } class TestRenderer implements GLEventListener { private GL gl; private GLDrawable gldrawable; float angle = 0; public void init(GLDrawable drawable) { gl = drawable.getGL(); this.gldrawable = drawable; } public void reshape(GLDrawable drawable, int x, int y, int width, int height) { gl.glViewport(0, 0, width, height); } public void display(GLDrawable drawable) { //gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glRotatef(.1f, 0f, 0f, 1f); gl.glBegin(GL.GL_POLYGON); gl.glVertex2f(-0.5f, -0.5f); gl.glVertex2f(-0.5f, 0.5f); gl.glVertex2f(0.5f, 0.5f); gl.glVertex2f(0.5f, -0.5f); gl.glEnd(); /* try { Thread.sleep(10); } catch (InterruptedException e) { } */ } public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) {} } } public class MyAnimator extends Thread { public void run() { while (true) { if (leftPanel.isStarted()) { leftPanel.draw(); } if (rightPanel.isStarted()) { rightPanel.draw(); } if (bottomPanel.isStarted()) { bottomPanel.draw(); } try { Thread.sleep(10); } catch (InterruptedException e) { } } } } }