Author Topic: How to add Mouse Listener  (Read 12119 times)

Offline alex

  • byte
  • *
  • Posts: 19
    • View Profile
How to add Mouse Listener
« on: March 24, 2009, 03:02:41 am »
I wanna to add MouseListener into this example for rotate or scale 3D object in canvas
the code is
Code: [Select]
package org.bm;

import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.threed.jpct.*;
import com.threed.jpct.util.*;

public class CarDemo extends JFrame {

World world;
Camera camara;
FrameBuffer buffer;
Texture colores[];
KeyMapper mapaTeclas;
Canvas canvasRender;
ThreadRender render;

/** Creates new form AutoFrame */
public CarDemo() {
Config.maxPolysVisible = 20000;
Config.farPlane = 12000;
Config.glMipmap = false;
Config.glForceEnvMapToSecondStage = true;

initComponents();
setTitle("test");

world = new World();
world.setAmbientLight(120, 120, 120);
world.addLight(new SimpleVector(0, -30, 0), 20, 20, 20);
camara = world.getCamera();
camara.setPosition(10, -5, -8);
camara.lookAt(new SimpleVector(0, 0, 0));
buffer = new FrameBuffer(600, 400,
FrameBuffer.SAMPLINGMODE_NORMAL);
buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
buffer.enableRenderer(IRenderer.RENDERER_OPENGL);
canvasRender = buffer.enableGLCanvasRenderer(IRenderer.RENDERER_OPENGL);

mapaTeclas = new KeyMapper(canvasRender);
pnl_Canvas.add(canvasRender);

render = new ThreadRender(world, buffer, mapaTeclas, canvasRender);
render.OGL = true;
render.start();

colores = new Texture[8];
colores[0] = new Texture("Auto/Texturas/ROJO.JPG");
colores[1] = new Texture("Auto/Texturas/AZUL.JPG");
colores[2] = new Texture("Auto/Texturas/VERDE.JPG");
colores[3] = new Texture("Auto/Texturas/NEGRO.JPG");
colores[4] = new Texture("Auto/Texturas/TOMATE.JPG");
colores[5] = new Texture("Auto/Texturas/AMARILLO.JPG");
colores[6] = new Texture("Auto/Texturas/GRIS.JPG");
colores[7] = new Texture("Auto/Texturas/BLANCO.JPG");
}

private void initComponents() {
btn_blanco = new JButton();
btn_gris = new JButton();
pnl_Canvas = new JPanel();
btn_amarillo = new JButton();
btn_negro = new JButton();
btn_verde = new JButton();
btn_azul = new JButton();
btn_rojo = new JButton();
btn_tomate = new JButton();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 10, 2, 2));
getContentPane().setLayout(new BorderLayout());

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

btn_blanco
.setIcon(new javax.swing.ImageIcon("Auto/Texturas/BLANCO.JPG"));
btn_blanco.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
btn_blancoMouseReleased(evt);
}
});

buttonPanel.add(btn_blanco);


btn_gris.setIcon(new javax.swing.ImageIcon("Auto/Texturas/GRIS.JPG"));
btn_gris.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
btn_grisMouseReleased(evt);
}
});

buttonPanel.add(btn_gris);


pnl_Canvas.setBorder(new javax.swing.border.EtchedBorder());
this.getContentPane().add(pnl_Canvas, BorderLayout.CENTER);


btn_amarillo.setIcon(new javax.swing.ImageIcon(
"Auto/Texturas/AMARILLO.JPG"));
btn_amarillo.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
btn_amarilloMouseReleased(evt);
}
});

buttonPanel.add(btn_amarillo);

btn_negro.setIcon(new javax.swing.ImageIcon("Auto/Texturas/NEGRO.JPG"));
btn_negro.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
btn_negroMouseReleased(evt);
}
});

buttonPanel.add(btn_negro);


btn_verde.setIcon(new javax.swing.ImageIcon("Auto/Texturas/VERDE.JPG"));
btn_verde.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
btn_verdeMouseReleased(evt);
}
});

buttonPanel.add(btn_verde);


btn_azul.setIcon(new javax.swing.ImageIcon("Auto/Texturas/AZUL.JPG"));
btn_azul.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
btn_azulMouseReleased(evt);
}
});

buttonPanel.add(btn_azul);

btn_rojo.setIcon(new javax.swing.ImageIcon("Auto/Texturas/ROJO.JPG"));
btn_rojo.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
btn_rojoMouseReleased(evt);
}
});

buttonPanel.add(btn_rojo);


btn_tomate
.setIcon(new javax.swing.ImageIcon("Auto/Texturas/TOMATE.JPG"));
btn_tomate.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
btn_tomateMouseReleased(evt);
}
});

buttonPanel.add(btn_tomate);

this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
this.setSize(600, 600);
}

private void btn_verdeMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_verdeMouseReleased
// Add your handling code here:
render.texturaActual = colores[2];
render.cambiarTextura("Base");
canvasRender.requestFocus();
}

private void btn_negroMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_negroMouseReleased
// Add your handling code here:
render.texturaActual = colores[3];
render.cambiarTextura("Base");
canvasRender.requestFocus();
}

private void btn_tomateMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_tomateMouseReleased
// Add your handling code here:
render.texturaActual = colores[4];
render.cambiarTextura("Base");
canvasRender.requestFocus();
}

private void btn_amarilloMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_amarilloMouseReleased
// Add your handling code here:
render.texturaActual = colores[5];
render.cambiarTextura("Base");
canvasRender.requestFocus();
}

private void btn_grisMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_grisMouseReleased
// Add your handling code here:
render.texturaActual = colores[6];
render.cambiarTextura("Base");
canvasRender.requestFocus();
}

private void btn_blancoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_blancoMouseReleased
// Add your handling code here:
render.texturaActual = colores[7];
render.cambiarTextura("Base");
canvasRender.requestFocus();
}

private void btn_azulMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_azulMouseReleased

render.texturaActual = colores[1];
render.cambiarTextura("Base");
canvasRender.requestFocus();
}

private void btn_rojoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_rojoMouseReleased

render.texturaActual = colores[0];
render.cambiarTextura("Base");
canvasRender.requestFocus();
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
render.salir = true;
render.stop();
render = null;
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new CarDemo().show();
}


private javax.swing.JButton btn_blanco;
private javax.swing.JButton btn_gris;
private javax.swing.JPanel pnl_Canvas;
private javax.swing.JButton btn_amarillo;
private javax.swing.JButton btn_negro;
private javax.swing.JButton btn_verde;
private javax.swing.JButton btn_azul;
private javax.swing.JButton btn_rojo;
private javax.swing.JButton btn_tomate;


}


can you give me a example
thanks a lot 
« Last Edit: March 25, 2009, 03:41:02 am by alex »

Offline fireside

  • double
  • *****
  • Posts: 607
    • View Profile
Re: [hurry]How to add Mouse Listener
« Reply #1 on: March 24, 2009, 03:52:56 am »
Code: [Select]
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class CarDemo extends JFrame implements MouseListener,MouseMotionListener{

        frame.addMouseListener(this);
        frame.addMouseMotionListener(this);

    public void mouseClicked(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseDragged(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseMoved(MouseEvent e) {
    //insert code
   }
}
« Last Edit: March 24, 2009, 03:55:15 am by fireside »
click here->Fireside 7 Games<-

Offline alex

  • byte
  • *
  • Posts: 19
    • View Profile
Re: [hurry]How to add Mouse Listener
« Reply #2 on: March 24, 2009, 04:50:07 am »
thanks
I know this method,I want to rotate or scale  the 3d Model which in the word  canvas  contained   by mouse event .
whether anyone have better method?
« Last Edit: March 24, 2009, 05:27:56 am by alex »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: [hurry]How to add Mouse Listener
« Reply #3 on: March 24, 2009, 09:43:03 am »
I don't understand your question and i consider the [hurry] to be quite impolite...anyway, whatever you do in a mouse- or keylistener, keep in mind that this is another thread than the rendering thread unless you are doing the rendering in paint/paintComponent, which you obviously don't do. If you rotate or modify an Object3D in any way directly into the listener's methods, you'll, without proper synchronization between the two threads, render inbetween states, which isn't what you want. I always suggest to set flags in the listeners that only signal what to do and do the actual operations in the rendering thread.

Offline alex

  • byte
  • *
  • Posts: 19
    • View Profile
Re: [hurry]How to add Mouse Listener
« Reply #4 on: March 24, 2009, 10:36:59 am »
I don't understand your question and i consider the [hurry] to be quite impolite...anyway, whatever you do in a mouse- or keylistener, keep in mind that this is another thread than the rendering thread unless you are doing the rendering in paint/paintComponent, which you obviously don't do. If you rotate or modify an Object3D in any way directly into the listener's methods, you'll, without proper synchronization between the two threads, render inbetween states, which isn't what you want. I always suggest to set flags in the listeners that only signal what to do and do the actual operations in the rendering thread.

sorry ,i  did not expressed the problem clearly.

Offline fireside

  • double
  • *****
  • Posts: 607
    • View Profile
Re: How to add Mouse Listener
« Reply #5 on: March 24, 2009, 11:40:42 am »
Thanks for the explanation, Egon.  I didn't know that.  It seems to work all right doing modifications in the listener from what I've experimented with, though.  I'd better rethink my code so I don't get in trouble with it.

alex: It seems pretty straight forward as far as changing rotation and scaling so I didn't think you were asking that.  If you are rotating or scaling, I would think you would have to keep track of the mouse position from the last cycle and rotate or scale it depending on the change. I would think you would use a mouse drag for something like that so it didn't do it all the time.  Basically, instead of changing the color, like I did in that other example, you rotate or scale.  Maybe I'm missing the question again.
« Last Edit: March 24, 2009, 12:08:02 pm by fireside »
click here->Fireside 7 Games<-

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: How to add Mouse Listener
« Reply #6 on: March 24, 2009, 12:40:48 pm »
I believe you can find what you are looking for in my Gears Demo source code (there is a link in the How to create an Applet thread.  That applet demonstrates listening to the mouse in both hardware-rendering mode (when there is a child Canvas object) and in software-rendering mode (when there is not a child Canvas object).  If I understand your question properly, you would want to look at what I am using for hardware-rendering mode (i.e. getting the mouse events from the Canvas).  If you need further explanation after looking at the source code, let me know.

i consider the [hurry] to be quite impolite...anyway
I suspect this is an "adaptation" to the hard reality that on most forums nobody ever seems to answer anyone's questions.  Rest assured, alex, that the jPCT forum is not like most forums - 99% of the time questions are answered very quickly and very thoroughly (I would say 100% of the time, but I am relatively new myself, so I can't speak with 100% certainty).

Offline alex

  • byte
  • *
  • Posts: 19
    • View Profile
Re: How to add Mouse Listener
« Reply #7 on: March 25, 2009, 01:42:59 am »
I believe you can find what you are looking for in my Gears Demo source code (there is a link in the How to create an Applet thread.  That applet demonstrates listening to the mouse in both hardware-rendering mode (when there is a child Canvas object) and in software-rendering mode (when there is not a child Canvas object).  If I understand your question properly, you would want to look at what I am using for hardware-rendering mode (i.e. getting the mouse events from the Canvas).  If you need further explanation after looking at the source code, let me know.

i consider the [hurry] to be quite impolite...anyway
I suspect this is an "adaptation" to the hard reality that on most forums nobody ever seems to answer anyone's questions.  Rest assured, alex, that the jPCT forum is not like most forums - 99% of the time questions are answered very quickly and very thoroughly (I would say 100% of the time, but I am relatively new myself, so I can't speak with 100% certainty).

thanks a lot for your words,
i just a new recruit,thank you again.As you said ,most forums nobody ever seems to answer anyone's questions,so I habitually added a "hurry".
I  apologize for this matter to EgonOlsen .

« Last Edit: March 25, 2009, 01:46:26 am by alex »

Offline alex

  • byte
  • *
  • Posts: 19
    • View Profile
Re: How to add Mouse Listener
« Reply #8 on: March 25, 2009, 03:45:35 am »
the problem has been solved .
Thank you everyone
Code: [Select]
frame = new JFrame("MouseTest");
frame.setSize(600, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseWheelListener(new MyMouseWheelListener(this));

frame.addMouseListener(new MyMouseListener(this));
frame.addMouseMotionListener(new MyMouseMotionListener(this));