Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catching a mouse click.

Status
Not open for further replies.

thebarslider

Programmer
Dec 21, 2001
80
GB
I am using Java2 with OpenGl to draw a triangle of vertices red, blue and green. I want to be able to press R and then click on the applet to resize the red vertex of my triangle. So far i have a KeyListener and a MouseListener but so far i have to click then press a key to resize the triangle. I want to be able to press a key then click to resize the triangle. How do i make the program wait for a mouse event before drawing the triangle?

case KeyEvent.VK_R:
message = "Set Red Vertex with Mouse";

// I want to get the program to wait for a mouse event at this point. How do i do that?

vertex[0] = x;
vertex[1] = y;
break;

I already have the below mouseClicked method:

public void mouseClicked(MouseEvent e) {
lastClick = e.getPoint();
// gets the last click in screen coords
}

I am really quite stuck on this problem, any help would be gladly accepted.

Mark.
 
I only have time to theorize here, but one thought would be to add a mouselistener to the red vertex on the fly. In other words, when the user clicks R, add the mouselistener to the red vertex object and have it listen for mouse events on itself. The red vertext object of course would have to be accesible from the same object that the key listener is listening on:

case KeyEvent.VK_R:
message = "Set Red Vertex with Mouse";
MouseAdapter ma = new MouseAdapter(){
//this has to be an inner anonymous class because
//MouseAdapter is abstract
public void mouseClicked(MouseEvent e)
{
//code to resize vertex here
}
};
RedVertexObject.addMouselistener(ma);

This code could get kind of clunky. What I would do is either have your vertex objects extend MouseAdapter or implement the MouseListener interface (if your vertex objects are (probably) already extending another class) and make it a listener of itself and have it look for a flag being set when the user presses R.

I don't know, just a thought.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top