Author |
Message |
Tewg
|
Posted: Sun Jun 15, 2008 10:38 pm Post subject: Java Mouse Event |
|
|
Just a few question about Mouse events.
I'm making a basic paint program and am having problem with spray paint function.
It's in a frame construct and was wondering if there was a type of event or method to discover that state of a mouse button such as when it is pressed and held but not movedso it would have the initial mousePressed method but beyond that it would stop the spray paint with only drawing 2 dots, but i can not use a loop because there is no way to my knowlege to have the mouseRelaesed method activate when in a loop
Any tip or pointer on where my logic in this is off please post.
Tewg |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Reality Check
|
Posted: Sun Jun 15, 2008 10:47 pm Post subject: Re: Java Mouse Event |
|
|
When you invoke mousePressed, set a boolean variable to true. While it is true, call your spray method and draw random dots according to mouse location (achieve through the mouseMoved method) and size of spray. I assume you have already done this. When mouse is released (mouseReleased), set the variable back to false thus it will not invoke the spray method anymore. This way you won't need to use mouseDragged and can do your drawing through the moved, pressed, and released methods. |
|
|
|
|
|
Tewg
|
Posted: Sun Jun 15, 2008 10:55 pm Post subject: Re: Java Mouse Event |
|
|
Yes, I though of that but when working with that it doesn't recognize the mouse released method (such as putting it in a while (boolean isPressed)) it just never exits is there a certain place to put this loop that it would recognize other event.... sorry im only a week into frames and events first day using mouse events. |
|
|
|
|
|
Reality Check
|
Posted: Sun Jun 15, 2008 11:28 pm Post subject: Re: Java Mouse Event |
|
|
I don't see why this should be happening. Don't do any drawing or calculations in the mouse events. Here is a basic example.
code: |
public void mousePressed (MouseEvent e) {
isPressed = true;
}
public void mouseReleased (MouseEvent e) {
isPressed = false;
}
public void mouseMoved (MouseEvent e) {
e.getX () = mx;
e.getY () = my;
}
|
In your paint method somewhere and if isPressed is true, keep calling repaint (). Now you can use an 'if' instead of a 'while' structure so mouseReleased is recognized:
code: |
if (isPressed == true) {
doSomething;
}
.
.
.
.
if (isPressed == true) {
repaint ()
}
|
|
|
|
|
|
|
Tewg
|
Posted: Sun Jun 15, 2008 11:32 pm Post subject: Re: Java Mouse Event |
|
|
thanks I'll try it out and reply back. |
|
|
|
|
|
Tewg
|
Posted: Sun Jun 15, 2008 11:46 pm Post subject: Re: Java Mouse Event |
|
|
where would i call the paint method
if (isPressed)
repaint ();
in the init method or where would it be put to be called every time. |
|
|
|
|
|
Reality Check
|
Posted: Sun Jun 15, 2008 11:54 pm Post subject: Re: Java Mouse Event |
|
|
I'd put it at the end of the paint method itself. |
|
|
|
|
|
Reality Check
|
Posted: Sun Jun 15, 2008 11:55 pm Post subject: Re: Java Mouse Event |
|
|
Also, remember the init is only for initialization. You can't put a condition such as yours in there. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|