1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| public class Main{
static {
GLProfile.initSingleton(true);
}
GLUT glut = new GLUT();
GLU glu = new GLU();
FPSAnimator animator;
FPSAnimator animator2;
GLCanvas viewCanvas;
GLCanvas viewCanvas2;
boolean visibility = false ;
public Main(){
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
// création des canvas
viewCanvas = new GLCanvas(caps);
viewCanvas.addGLEventListener(new TotoListener(Color.WHITE));
viewCanvas2 = new GLCanvas(caps);
viewCanvas2.addGLEventListener(new TotoListener(Color.GREEN));
viewCanvas.setPreferredSize(new Dimension(300, 300));
viewCanvas2.setPreferredSize(new Dimension(300, 300));
// animation
animator = new FPSAnimator( viewCanvas, 60);
animator.add(viewCanvas);
animator.start();
animator2 = new FPSAnimator( viewCanvas2, 60);
animator2.add(viewCanvas2);
animator2.start();
// bouton pour changer le canvas visible
JButton but = new JButton("change");
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
viewCanvas.setVisible(visibility);
viewCanvas2.setVisible(!visibility);
if(viewCanvas.isVisible()){
animator2.stop();
animator.start();
}
else if(viewCanvas2.isVisible()){
animator.stop();
animator2.start();
}
visibility = !visibility;
}
});
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(BorderLayout.EAST,viewCanvas);
frame.getContentPane().add(BorderLayout.WEST,viewCanvas2);
frame.getContentPane().add(BorderLayout.SOUTH,but);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null); // au milieu
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Main m = new Main();
}
});
}
//Listener
class TotoListener implements GLEventListener{
float r,g,b;
public TotoListener(Color c){
r = c.getRed();
g = c.getGreen();
b = c.getBlue();
}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0f, 0.0f, -6);
gl.glColor3f(r, g, b);
glut.glutWireSphere(1, 20, 20);
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void init(GLAutoDrawable drawable) {
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
GL2 gl = drawable.getGL().getGL2();
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, (float) (width) / (float) (height), 1.0f,100.0f);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
}
} |
Partager