yeah, but don't know how to redraw the paddle., that's why iam posted this .
someone told me that I must have "Timer" or something like that to keep moving the paddle
and i don't know what a timer is!!
The Timer class is just something that can trigger a piece of code to run after a certain period of time. I think you'll need it for moving the ball in your game but not the paddle.
So in the test class (Class names should start with an uppercase FYI), you are drawing the blue paddle within the paint method. You've set the position of the paddle to be x and y. Notice how you are drawing one Image on the canvas but have 3 components - background, blocks and paddle. You might want to change this as you are going to have to update things and it will be expensive to redraw everything rather than just what you need.
I can't remember the exact API calls to do what you need to do but fundamentally on an arrow key press:
- Figure out if the paddle actually needs to move i.e. if it's already as far left as it can move you don't need to redraw anything
- set variables x and y on the breakout object (i'm assuming that's where the values are coming from for your paddle - your code is not clear)
- Ask the canvas to update
I think that you are going to need a timer to control paddle movement also. Otherwise the speed of movement is going to depend upon variable factors such as processor speed or key repeat rate. You need to control this timing by only checking the keypress and moving the paddle at well-defined intervals. This has the additional advantage that you can then have variable speeds under program control.
At a glance and without too much thought, I'm not sure keyReleased should be resetting the xnew and ynew variables as there is potential for a key to be pressed and released before actionPerformed is finished?