Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Jumping Image Effect

807589Jul 9 2008 — edited Jul 10 2008
I have a picture of a little stick figure man and i want to make it appear as if he is jumping.
The code I have to do that is this:
		switch(e.getKeyCode()){
			case KeyEvent.VK_UP:
				for(int i=0; i<=10; i=i+1){
					y = y+1;  //the int y represents the images position of the y-axis
					repaint();
					try{Thread.sleep(200);}
						catch(InterruptedException ie){}
						
					if(i == 10){
						for(int j=0; j<=10; j=j+1){
							y = y-1;
							repaint();
							try{Thread.sleep(200);}
							catch(InterruptedException ie){}
						}
						break;
					}
				}
				break;
but for some reason the image just stays in one place and does not have the jumping effect I wanted.
(proabably has to do with the Thread.sleep(200); method I called, but I dont know how to fix it).

Thanks in advance!

Comments

807589
Since you're looking at the key code of a KeyEvent, I'll assume you're in a key pressed event handle method. This method is called by the Event Dispatch Thread (EDT). Calls to paint are also done on the EDT, so repaint has no effect until after this method ends. So the calls to Thread.sleep() cause your whole application to freeze. Want you want to do is create a swing Timer that you can use to update your stick figure every 200 milliseconds. You then stop the timer when you've updated the figures position 11 times.
807589
Perhaps another way to do this is to have you're repaint and algo statment in a class that extends thread, and start that thread from your program
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
Runner run = new Runner(component_you_want_to_repaint);
run.start();
break;
}
Then you have your thread class, and you use the SwingUtilities.invoke later to repaint your screen and the sleep() method to sleep the thread
public class Runner extends Thread{
Component comp;
public Runner(Component c){
comp = c;
}
public void run(){

				for(int i=0; i<=10; i=i+1){
					y = y+1;  //the int y represents the images position of the y-axis
                                                        Runnable doLater = new Runnable(){
                                                         public void run(){
                                                         comp.repaint(); 
                                                         }
                                                       };
							SwingUtilities.invokeLater( doLater );
					sleep(200);
						
					if(i == 10){
						for(int j=0; j<=10; j=j+1){
							y = y-1;
                                                        Runnable doLater = new Runnable(){
                                                         public void run(){
                                                         comp.repaint(); 
                                                         }
                                                       };
							SwingUtilities.invokeLater( doLater );
							sleep(200);
						}

}
Hopefully something like this will solve your problem. If you use the code, double check the syntax, and it'll probably need other mods specific for your program
807589
I vote for gte42e's recommendation about using a Swing Timer. You could also do this in a background thread as sierratech recommends, but why do this if you're not doing heavy calculations or some other time-hogging computation? Also, if you do go this route, I strongly advise you not to extend Thread and instead either implement Runnable or use a SwingWorker object. Finally, you should look into using key bindings to capture your keyboard input rather than keylisteners.
807589
Thanks to all of you who responded!!!

What is a key binding by the way?
807589
I tried to use a timer like this:
	public void keyPressed(KeyEvent e){
		switch(e.getKeyCode()){
			case KeyEvent.VK_DOWN:
				y += 10;
				break;
			case KeyEvent.VK_UP:
				for(int i=0; i<=10; i=i+1){
				
					timer.start();					
					y = y+1;
						for(int j=0; j<=10; j=j+1){
							y = y-1;
						}
						timer.stop();
						break;
				}
				break;
but it didn't work.
What did I do wrong??
807589
clamm wrote:
but it didn't work.
What did I do wrong??
I don't see anywhere where you are setting the Timer's delay or ActionListener.
To be perfectly honest, it looks like you you're just throwing a timer object in there, calling start and stop, and haven't bothered yet looked at the Swing Timer API or tutorial which is bound to fail. I suggest you look at both. Same for key binding. Good luck!
807589
Sorry I forgot to put my code in about setting the delay and the actionListener.
Here is my new code, but still doesn't work:
	Timer timer;
	
	public myClass(){
		timer = new Timer(1000, new TimerAction());
	}

	public void keyPressed(KeyEvent e){
		switch(e.getKeyCode()){
			case KeyEvent.VK_DOWN:
				y += 10;
				break;
			case KeyEvent.VK_UP:
				timer.start();
				
				System.out.println("Initial:"+y);
				
				for(int i=1; i<=10; i=i+1){
					y=y-1;
					System.out.println(y);
				}
				for(int i=1; i<=10; i=i+1){
					y=y+1;
					System.out.println(y);
				}
				timer.stop();
				break;
                 }
class TimerAction implements ActionListener {
	
	public void actionPerformed(ActionEvent event){
		repaint();
	}
}
Whats wrong??
807589
You're still not using your Timer correctly. Please read the timer tutorial and go through the examples. It's all explained there.
807589
Right now you start a timer that will repaint your window, but then you do all your changes to y and then stop your timer. The timer never gets to run.

You're should be trying to replace your for loops with the timer. Each call to actionPerformed should run one iteration of one of your for loops. You'll need a counter to keep track of which for loop you're in, and when the timer should end. Call timer.stop when the last change to y is complete.
1 - 9
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 7 2008
Added on Jul 9 2008
9 comments
63 views