Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Float;
import java.util.*;
import javax.swing.JFrame;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.*;
import com.jogamp.opengl.util.FPSAnimator;
import static com.jogamp.opengl.GL2.*;
public class JOGLMouseMotionSwing implements GLEventListener{
private final List points;
public JOGLMouseMotionSwing() {
points = new ArrayList<>();
//OpenGLの描画機能を指定
GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2));
//Create JFrame
JFrame frame = new JFrame();
frame.setTitle("JOGLMouseMotionSwing");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//create Canvas
GLCanvas canvas = new GLCanvas(caps);
canvas.setSize(300,400);
canvas.addGLEventListener(this);
canvas.addMouseListener(new mylistener());
//add Canvas to Frame
frame.getContentPane().add(canvas);
frame.getSize(frame.getContentPane().getPreferredSize());
frame.pack();
frame.setVisible(true);
//animator
final FPSAnimator animator = new FPSAnimator(canvas,60);
animator.start();
}
@Override
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClearColor(1f, 1f, 1f, 1.0f);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2();
gl.glMatrixMode(GL_PROJECTION);
gl.glLoadIdentity();
System.out.printf("x:%d, y:%d, w:%d, h:%d, %n", x, y, width, height);
gl.glOrthof(x, x+width, y+height, y, -1.0f, 1.0f);
gl.glMatrixMode(GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL_COLOR_BUFFER_BIT);
gl.glColor3f(1.0f,0.0f,0.0f);
gl.glBegin(GL_LINES);
for(int i = 0; i < points.size() -1; i++) {
Point2D.Float p0 = (Float)points.get(i);
Point2D.Float p1 = (Float)points.get(i + 1);
gl.glVertex2d(p0.getX(), p0.getY());
gl.glVertex2d(p1.getX(), p1.getY());
}
gl.glEnd();
}
@Override
public void dispose(GLAutoDrawable drawable) {}
//MouseAdapterを継承したmylistenerを作る
public class mylistener extends MouseAdapter{
public void mouseClicked(MouseEvent e){
System.out.printf("%d, %d%n", e.getX(), e.getY());
points.add(new Point2D.Float(e.getX(), e.getY()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment