User inputs and simple anim.

I know this is a bit basic, but I'm having trouble getting this animation to respond to user inputs. I want the circle to start near the centre of the screen and move up until it hits Y less than 10. Here, I've started the animation thread as a response to the user input, I know this probably isn't the best way to do it.

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Color;

import java.awt.*;

import java.awt.event.*;

publicclass basicAnimationextends Appletimplements Runnable, KeyListener

{Thread aThread;

publicint Y;

publicvoid start()

{

if (aThread ==null)

{aThread =new Thread(this);

aThread.start();}

}

publicvoid stop()

{

if (aThread !=null)

{aThread =null;}

}

publicvoid run()

{

while (true)

{

for(Y=350; Y<10; Y--)

{repaint();

try{ Thread.sleep(100);}

catch (InterruptedException e){}}

}

}

publicvoid init()

{Y=350;}

publicvoid paint(Graphics g)

{g.fillOval (415, Y, 10, 10);}

publicvoid keyPressed(KeyEvent e)

{

int keyCode = e.getKeyCode();

switch(keyCode)

{case KeyEvent.VK_SPACE:

aThread.start();

break;}

}

publicvoid keyTyped(KeyEvent e){}

publicvoid keyReleased(KeyEvent e){}

}

[3667 byte] By [Hard_Wireda] at [2007-11-26 16:35:24]
# 1

In the method that your thread is executing, you need to have a line that looks something like this:

if( Y > 10 )

{

// decrease Y in some way that you think is best

// example

Y--;

}

This will allow the circle to move upwards and once it reaches Y == 10, it will stop. This seems to be the only thing you are omitting.

pogaloga at 2007-7-8 23:00:15 > top of Java-index,Security,Cryptography...
# 2

Sorry, now that I have gotten a better look at your run() method. You need to replace what you have with the code that I gave you in the previous post. If you run a for loop that is inside the thread, you will change Y from being at the center to Y == 10 in one thread iteration. This is much too fast for what you want to do.

Don't forget that the thread is executing something like 1000 times every second (depending on how you set it up and the speed of your machine). So if you put a for loop inside the thread you are running the for loop about 1000 times a second when all you meant to do is run through one time.

pogaloga at 2007-7-8 23:00:15 > top of Java-index,Security,Cryptography...