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
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import javax.swing.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;
public class Test extends JFrame{
public GLCanvas canvas;
public Test(){
super();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setMinimumSize(new java.awt.Dimension(640, 480));
canvas = new GLCanvas();
canvas.addGLEventListener(new Render(this));
this.getContentPane().add(canvas);
canvas.requestFocusInWindow();
}
public void refresh(){
this.canvas.display();
}
public static void main(String[]args){
new Test().setVisible(true);
}
}
class Render implements GLEventListener, MouseMotionListener, MouseWheelListener{
Test mw;
private float zoom = 1f;
public Render(Test test){
this.mw = test;
}
public void init(GLAutoDrawable arg0) {
final GL gl = arg0.getGL();
gl.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading
gl.glClearColor(1f, 1f, 1f, 1f);
gl.glClearDepth(1.0f);
gl.setSwapInterval(1);
arg0.addMouseWheelListener(this);
}
public void display(GLAutoDrawable arg0) {
final GL gl = arg0.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glTranslatef(0f, 0f, zoom);
gl.glColor3f(0f, 0f, 1f);
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3f(-0.5f, 0.5f, 0f);
gl.glVertex3f(0.5f, 0.5f, 0f);
gl.glVertex3f(0.5f, -0.5f, 0f);
gl.glVertex3f(-0.5f, -0.5f, 0f);
gl.glEnd();
gl.glFlush();
}
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
final GL gl = arg0.getGL();
gl.glViewport(0, 0, arg3, arg4);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(-2.5f * arg3 / arg4 * zoom, 2.5f * arg3 / arg4 * zoom, -2.5f * zoom, 2.5f * zoom, -5f * zoom, 5f * zoom);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
}
public void mouseDragged(MouseEvent arg0) {
}
public void mouseMoved(MouseEvent arg0) {
}
public void mouseWheelMoved(MouseWheelEvent arg0) {
if (arg0.getWheelRotation() == 1) {
zoom += 0.05f;
} else {
zoom -= 0.05f;
}
this.mw.refresh();
}
} |
Partager