How to move the object to where I click the mouse? [closed]

5

My problem is this: I have an OpenGl program with Java.

I made an animation for the drawing to move, increase and decrease in size, so far so good.

When I was creating a function where when clicking the mouse, my drawing moves to where I clicked I could not because when I click, the mouse takes the x and y coordinate in relation to the window pixel and not in relation to where I clicked on the drawing .

If I could get the right coordinate, just play on my global variable x and y that my drawing would move.

Follow the Code:

package teste;

import com.sun.opengl.util.*;
//import lwjgl.opengl.Display;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;



/**
 * MouseClique.java <BR>
 * author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P>
 *
 * This version is equal to Brian Paul's version 1.2 1999/10/21
 */
public class MouseClique implements GLEventListener, KeyListener, MouseListener {

    float x=0,y=0,x2,larg,alt;
    float Multi;
    float win;
    float xp1,yp1,xp2,yp2,xp3,yp3, rp,gp,bp; // Ponta Avião
    float LimitPositivoY, LimitNegativoY, LimitPositivoX,LimitNegativoX;
    boolean StatusCima, StatusBaixo,StatusEsquerda,StatusDireita;
    boolean direita = false;  
    boolean esquerda = true;
    boolean cima = false;  
    boolean baixo = false; 
    boolean Inicia = false;


    public void display(GLAutoDrawable drawable) {
        GL gl;
        GLU glu;
        gl = drawable.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, -6.0f);

        gl.glBegin(GL.GL_TRIANGLES); //Ponta do Aviao
            gl.glColor3f(rp, gp, bp); 
            gl.glVertex2f(x+xp1,y+ yp1);  
            gl.glVertex2f(x+xp2,y+ yp2); 
            gl.glVertex2f(x+xp3,y+ yp3); 
            gl.glColor3f(rp, gp, bp); 
        gl.glEnd();

        gl.glPointSize(5);
         gl.glBegin(GL.GL_POINTS); //Linhas Janela Esquerda Detalhe
            gl.glColor3f(0, 0, 0);    
            gl.glVertex2f(0,0);  
            gl.glVertex2f(-1,0);
            gl.glVertex2f(-2,0);
            gl.glVertex2f(-3,0);
            gl.glVertex2f(-4,0);
            gl.glVertex2f(-5,0);
            gl.glVertex2f(1,0);
            gl.glVertex2f(2,0);
            gl.glVertex2f(3,0);
            gl.glVertex2f(4,0);
            gl.glVertex2f(5,0); 
            gl.glVertex2f(0,1);
            gl.glVertex2f(0,2);
            gl.glVertex2f(0,3);
            gl.glVertex2f(0,4);
            gl.glVertex2f(0,5);
            gl.glVertex2f(0,-1);
            gl.glVertex2f(0,-2);
            gl.glVertex2f(0,-3);
            gl.glVertex2f(0,-4);
            gl.glVertex2f(0,-5);
            gl.glColor3f(0, 0, 0);
        gl.glEnd();
        gl.glFlush();

        Anima(); 

    }

        public void keyPressed(KeyEvent e){
        //Controle dos eventos de teclado
        System.out.println("Evento do teclado");
    switch(e.getKeyChar()) //Cores
    {
            case 'I': 
            case 'i':
                //Inicia = true;
                break;
            case 'P':
            case 'p':
                //Inicia = false;
                break;
            case 'Q':
            case 'q':
            // Ponta Avião
            rp = 0;
            gp = 0;
            bp =0;  

                break;
            case 'A':
            case 'a':
                // Ponta Avião
            rp = 1;
            gp = 1;
            bp =1;  


                break;
            case '1':
            if(xp2*1.2f<=LimitPositivoX && yp2*1.2f<=LimitPositivoY && xp3*1.2f>=LimitNegativoX && yp3*1.2f>=LimitNegativoY)
            {
            xp1 = xp1*1.2f;
            yp1 = yp1*1.2f;
            xp2 = xp2*1.2f;
            yp2 = yp2*1.2f;
            xp3 = xp3*1.2f;
            yp3 = yp3*1.2f;

            }
                break;
        }
        switch(e.getKeyCode()) // Posição
        {
            case KeyEvent.VK_LEFT:
                direita =false;
                esquerda=true;
                cima = false;
                baixo = false;
                break;
            case KeyEvent.VK_RIGHT:
                cima = false;
                baixo = false;
                direita = true;
                esquerda =false;
                break;
            case KeyEvent.VK_UP:
                cima = true;
                baixo = false;
                direita = false;
                esquerda =false;
                break;
            case KeyEvent.VK_DOWN:
                cima = false;
                baixo = true;
                direita = false;
                esquerda =false;
                break;
        } 
    }

        public void keyReleased(KeyEvent e){}

    public void keyTyped(KeyEvent e){}

    public void mousePressed(MouseEvent e) {  
        System.out.println("Evento do mouse");
        switch (e.getButton()) {
            case MouseEvent.BUTTON1:
                float w, h;
               w=(float)e.getX();
               h=(float)e.getY();
                System.out.println(w+","+h);
               break;
            case MouseEvent.BUTTON3:

               break;
        }
    }  

    public void mouseReleased(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mouseClicked(MouseEvent e) {}

    public void mouseEntered(MouseEvent e) {}

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

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();
        gl.setSwapInterval(1);
        gl.glClearColor(1.0f, 0.96f, 0.71f, 0.0f);
        Multi = 8;
        // Ponta Avião
        xp1 = -1f/Multi;
        yp1 = 3f/Multi;
        xp2 = 0f/Multi;
        yp2 = 5.0f/Multi;
        xp3 = 1f/Multi;
        yp3 = 3f/Multi;
        rp = 0;
        gp = 0;
        bp = 0;  
        gl.glShadeModel(GL.GL_SMOOTH);

       drawable.addKeyListener(this); 
       drawable.addMouseListener(this); 
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();
        if (height <= 0) {
            height = 1;
        }
        final float h = (float) width / (float) height;
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 1.0, 20.0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void Anima() {
        if(Inicia){
           if(direita) {
            // Ponta Avião
            xp1 += 0.03f;
            xp2 += 0.03f;
            xp3 += 0.03f;

        }
        if(esquerda) {
            // Ponta Avião
            xp1 -= 0.03f;
            xp2 -= 0.03f;
            xp3 -= 0.03f;


        }
        if(cima){
            // Ponta Avião
            yp1 += 0.03f;
            yp2 += 0.03f;
            yp3 += 0.03f;


        }
        if(baixo){
            // Ponta Avião
            yp1 -= 0.03f;
            yp2 -= 0.03f;
            yp3 -= 0.03f;

        }
        if(yp2>=LimitPositivoY){
            cima = false;
            baixo = true;
            esquerda = false;
            direita = false;
        }
        if(yp3<=LimitNegativoY){
            cima = true;
            baixo = false;
            esquerda = false;
            direita = false;
        }
        if(xp2<=LimitNegativoX) {
            esquerda=false;
            direita=true;
            cima = false;
            baixo = false;
        }
        if(xp3>=LimitPositivoX) {
            esquerda=true;
            direita=false;
            cima = false;
            baixo = false;
        }
        try {
        //    Thread.sleep(15);
        } catch (Exception e) {
            e.printStackTrace();
        }  
        }   
    }

    public static void main(String[] args) {
        Frame frame = new Frame("Animaç±ao");
        GLCanvas canvas = new GLCanvas();
        canvas.addGLEventListener(new MouseClique());
        frame.add(canvas);
        frame.setSize(1280, 720);
        final Animator animator = new Animator(canvas);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                new Thread(new Runnable() {
                    public void run() {
                        animator.stop();
                        System.exit(0);
                    }
                }).start();
            }
        });
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        animator.start();
    }
}
    
asked by anonymous 03.09.2017 / 23:44

0 answers