2

好吧,我想我可能需要一些帮助。我是 JOGL 的新手,我正在尝试在 3d 画布上绘制任何东西,但重点是使用鼠标拖动事件。知道我怎么能做到这一点?

我尝试了以下方法,但没有成功。而且在下面的代码中,我不明白为什么 display() 方法在被调用之前被调用事件。

提前非常感谢..

package Testing.Drawing;

import java.awt.event.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;

public class TestDraw2
  extends JFrame
    implements GLEventListener, KeyListener,MouseListener, MouseMotionListener
{
  private GLCapabilities caps;
  private GLCanvas canvas;
  private float rotAngle = 0f;
  private boolean rotate = false;


  public TestDraw2()
  {
    super("TestDraw2");

    caps = new GLCapabilities();
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);
    canvas.addKeyListener(this);
   // canvas.addMouseListener(this);
    //canvas.addMouseMotionListener(this);

    setVisible(true);
    //this.getContentPane().add(canvas);
  }
  public void run()
  {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(512, 512);
    setLocationRelativeTo(null);
    this.getContentPane().add(canvas);
    canvas.requestFocusInWindow();

  }
float mouse_x = 0.0f;
float mouse_y = 0.0f;

public static void main(String[] args)
  {
    new TestDraw2().run();
  }
  final double PI = Math.PI;
     final double PI_2 = PI / 2;
     final double sin(double i) {
      return Math.sin(i);
    }
    final double cos(double i) {
     return Math.cos(i);
    }

  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    drawable.addKeyListener(this);
    drawable.addMouseListener(this);
    drawable.addMouseMotionListener(this);
    //
    float values[] = new float[2];
    gl.glGetFloatv(GL.GL_LINE_WIDTH_GRANULARITY, values, 0);
    System.out.println("GL.GL_LINE_WIDTH_GRANULARITY value is " + values[0]);
    gl.glGetFloatv(GL.GL_LINE_WIDTH_RANGE, values, 0);
    System.out.println("GL.GL_LINE_WIDTH_RANGE values are " + values[0] + ", "
                       + values[1]);
    gl.glEnable(GL.GL_LINE_SMOOTH);
    gl.glEnable(GL.GL_BLEND);
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
    gl.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_DONT_CARE);
    gl.glLineWidth(1.5f);
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  }

  public void display(GLAutoDrawable drawable)
  {
    //draw(drawable);
    /*GL gl = drawable.getGL();
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    gl.glColor3f(0.0f, 1.0f, 0.0f);
    gl.glPushMatrix();
    gl.glRotatef(-rotAngle, 0.0f, 0.0f, 0.1f);
    gl.glBegin(GL.GL_LINES);
    gl.glVertex2f(-0.5f, 0.5f);
    gl.glVertex2f(0.5f, -0.5f);
    gl.glEnd();
    gl.glPopMatrix();
    gl.glColor3f(0.0f, 0.0f, 1.0f);
    gl.glPushMatrix();
    gl.glRotatef(rotAngle, 0.0f, 0.0f, 0.1f);
    gl.glBegin(GL.GL_LINES);
    gl.glVertex2f(0.5f, 0.5f);
    gl.glVertex2f(-0.5f, -0.5f);
    gl.glEnd();
    gl.glPopMatrix();
    gl.glFlush();
    if (rotate) rotAngle += 1f;
    if (rotAngle >= 360f) rotAngle = 0f;*/

     GL gl = drawable.getGL();

        //gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glColor3f(0.0f, 1.0f, 0.0f);
        gl.glPushMatrix();
        //gl.glRotatef(-rotAngle, 0.0f, 0.0f, 0.1f)
        gl.glTranslatef(mouse_x, mouse_y,0.1f);
       // gl.glRasterPos2f(mouse_x, mouse_y);
        //gl.glDrawPixels(gl.GL_CURRENT_RASTER_POSITION);

        gl.glBegin(GL.GL_POINTS);
        System.out.println("start values of x,y are: "+mouse_x+", "+mouse_y);
        gl.glVertex2f(mouse_x, mouse_y);
        //gl.glVertex2f(0.5f, -0.5f);
        gl.glEnd();

        gl.glPopMatrix();
        gl.glFlush();
  }

  public void draw(){

      canvas.display();

   }
  public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
  {
    GL gl = drawable.getGL();
    GLU glu = new GLU();
    gl.glViewport(0, 0, w, h);
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    if (w <= h) //
    glu.gluOrtho2D(-1.0, 1.0, -1.0 * (float) h / (float) w, //
        1.0 * (float) h / (float) w);
    else glu.gluOrtho2D(-1.0 * (float) w / (float) h, //
        1.0 * (float) w / (float) h, -1.0, 1.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
  }

  public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
      boolean deviceChanged)
  {
  }

  public void keyTyped(KeyEvent key)
  {
  }

  public void keyPressed(KeyEvent key)
  {
    switch (key.getKeyCode()) {
      case KeyEvent.VK_ESCAPE:
        System.exit(0);
      case KeyEvent.VK_R:
        rotate = !rotate;

        canvas.display();
      default:
        break;
    }
  }

  public void keyReleased(KeyEvent key)
  {
  }
  //////////////////////////////////////////////////////////////////////
    // Mouse events

    // Do-nothing methods, but required nonetheless
    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    // Mouse event called when mouse button is pressed and moved
    public void mouseDragged(MouseEvent e) {

     // float x = e.getX();
     // float y = e.getY();

      // Button 1 pressed
      if ((e.getModifiers() & e.BUTTON1_MASK) != 0){
          mouse_x = e.getX();
          mouse_y = e.getY();
          draw();

      }

      /*  if ((e.getModifiers() & e.BUTTON1_MASK) != 0) {
        camera_yaw += angle_sensitivity * (double) (mouse_x - x);
        if (camera_yaw > PI) {
          camera_yaw -= 2.0 * PI;
        } else if (camera_yaw < -PI) {
          camera_yaw += 2.0 * PI;
        }
        camera_pitch += angle_sensitivity * (double) (y - mouse_y);
        if (camera_pitch > PI_2) {
          camera_pitch = PI_2;
        } else if (camera_pitch < -PI_2) {
          camera_pitch = -PI_2;
        }
      }*/

      // Button 2 pressed: change position
      /*if ((e.getModifiers() & e.BUTTON2_MASK) != 0) {
        camera_x += dist_sensitivity * (double) (y - mouse_y)
                    * cos (camera_yaw) * cos (camera_pitch);
        camera_y += dist_sensitivity * (double) (y - mouse_y)
                    * sin (camera_yaw) * cos (camera_pitch);
        camera_z += dist_sensitivity * (double) (y - mouse_y)
                    * sin (camera_pitch);
      }/*

      // Button 3 pressed: rotate scene
     /* if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
        scene_yaw += angle_sensitivity * (double) (x - mouse_x);
        if (scene_yaw > PI) {
          scene_yaw -= 2.0 * PI;
        } else if (scene_yaw < -PI) {
          scene_yaw += 2.0 * PI;
        }
        scene_pitch += angle_sensitivity * (double) (y - mouse_y);
        if (scene_pitch > PI) {
          scene_pitch -= 2.0 * PI;
        } else if (scene_pitch < -PI) {
          scene_pitch += 2.0 * PI;
        }
      }*/
    }

    // Passive motion callback to capture mouse movements while buttons
    // are not pressed
    public void mouseMoved(MouseEvent e) {
      // Update mouse position
    //  mouse_x = e.getX();
      //mouse_y = e.getY();
    }
}
4

0 回答 0