This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes slowing down movement in an applet.... doesn't have anything to do with the applet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "slowing down movement in an applet.... doesn Watch "slowing down movement in an applet.... doesn New topic
Author

slowing down movement in an applet.... doesn't have anything to do with the applet.

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
I need to slow the speed down of my UP keystroke. My code is:


The piece of code that I need to slow down is:



the html code is:


What it does now is it moves the arc to that position... I had it before that it moves by 1 everytime you hit the up key or keep it pushed. But all I want it to do is to move up to the position slowly with one push of the up button.

PS. I didn't know where to post the subject so I know it will probably get moved. Please help!! ^_^


Live And Let Learn.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
umm...use a timer

if you're in CS class look it up in your book..

when you set up the timer its like this

Timer timer = new Timer(100,new slimeListener());

now the 100 is in milliseconds i beilieve....so the bigger the slower...

then have your other listener start the timer...

hint:

Timer.start() && Timer.stop()


You down with OOP? Yeah you know me!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61105
    
  66

"Fox", please email me.

bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Whats wrong with the code for the timer... where exactly would everything be placed?

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Ok... this is the revised code with things in more proper spots. What else do I need?

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Does anybody know how to fix what I've done?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok when you set your timer, it requires two parameters.

1. how fast you want the listener to be called over and over again.

2. the listener of the timer, which has instructions for the timer.

then you have to make a new listener for the timers NEW Listener.

so say you have:

Timer timer = new Timer(100, new timeListener());

public class timeListener implements ActionListener
{
public void ActionPerformed(ActionEvent event)
{
X++;
Y++;
}
}

Then in your keylistener Listener implementation, you would have...

the timer.start() when you press the up key..

and then timer.stop() when you release the up key..

and remember...

when you implement KeyListener...

you have to define the keyPressed(){} keyReleased(){} and keyTyped(){}

and if you want to define what happens when you press or release the up key..

you have to atleast define an empty body for keyTyped(){}

I hope this helps a little bit..

-Justin-
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Justin... I'm lost. Sorry... I feel awful that I'm not understanding. I looked in my java book but there wasn't much in it about Timers and there was nothing on how to set start and stop. I set it up the best I could. With what I have... could you give me hints step through step as to what more I have to do. I learn better when I actually do it. Even if you could just point out my problems and comment the areas that I have to add to. It would make more sense to me. Please and thank you.
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Or, if u could even write a mini applet using the timer to move say... a square from left to right. That may help.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok, i'll get rignt on that ok, I might have to reference to certain

syntax, but I'll help ya out...

-Justin-
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Thanks!!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok here is the main class which loads the JPanel



here is the actual panel which uses the Timer



This should help you better understand how to use the timer...

as you can see, once you hit the start button, its going to start the timer, which in turn, is going to execute X++ and Y++ every 100 milliseconds..

hope this helps you out man

sorry it took so long, it was late when i saw your reply(s)

-Justin-
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
This is what I have now:



It compiles fine so I'm guessing everything is in the right spot... but when I push the up key in the applet it will reach y=330 but the slime and the ground continues to flash as if the start is trying to do something. What else do I have to do to get it to work? I'm pretty sure I have my classes right. I don't know where I missed the piece of information?
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Nevermind Justin! I added the y--; in the actionPerformed section and it works. Thank you so much for your help. I appreciate it. If ever I need anymore help I'll ask for you when I post... thank u!!
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Justin, what I want it to do is to fall back down to y=440 as soon as it hits y=330. What i did is added a second timer called timer2.



When it hits 330, it does go down, as long as another key is pushed. But I want it to go down on its own. What would I have to do?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok i tried your applet, and the up,down,L,R keys don't work, and i saw the

problem

you have to do this....



and if nothing is supposed to happen just define an empty method.

I've used keyAdapter, and it didn't seem to work for me, but even if you

use the key adapter...your supposed to extend it..

Like so:



and for your timers...

have the timer1 start your timer2 when it hits the 330...

so you'll press the up key, then timer1 will start until it hits 330,
then stop, and then timer1 will start the timer2, which will bring it back
up to 440, then timer 2 will stop.(in the timer2 class ofcourse)


so its like this...


hope this helps, and answers your questions...

-Justin-

[ April 21, 2006: Message edited by: Justin Fox ]
[ April 21, 2006: Message edited by: Justin Fox ]
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
It works... thanks again Justin!!

~Martin
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
your very welcome martin!

if you have any other questions feel free to ask

-Justin-
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Ok, Justin... this is what I've done. I have taken out the increasing x an y values and replaced it with more Timers. So the timers work good on start and stop but I have two problems. First: when you push the up key it goes up. But while it goes back down with the Down Timer and if for some mistake (which I want to fix) the controller pushes the up key again it stays on the one y value but can still move left or right, just not up or down. Second problem is this code:


When I would get up to the border of the applet and move diagonally on it, it would stop only when x == 0 or if y was increasing (moving down) but not when it would be moving up. Heres the rest of my code... everythings commented to find easier:

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Neverming about the first problem with the Up Key... I fixed it... wasn't thinking straight!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: slowing down movement in an applet.... doesn't have anything to do with the applet.