I'm developing a tennis game with multi ball, I got a problem when I wanted to move the paddle from right to left and vice versa, the paddle should be moving every time the left arrow is press or the right arrow and modifies its X axis when one of this key is pressed, but every time i press the key arrow the paddle move just one time, and after that it can't move on the same direction. I'm a beginner, please help me here is the code for the main class called MainApp
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainApp extends JFrame implements ActionListener {
JButton btn = new JButton("SAVE GAME");
JButton btn1 = new JButton("OPEN GAME");
ArrayList<Ball> ballList = new ArrayList<Ball>();
//Ball1 bal;
boolean state = true;
//obstMov om = new obstMov();
//ArrayList<obstMov> om = new ArrayList<obstMov>();
obstMov bk = new obstMov();
MainApp(){
addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
bk.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
bk.keyReleased(e);
}
});
setFocusable(true);
setSize(660, 580);
setLayout(null);
btn.setBounds(10, 10, 150, 30);
add(btn);
btn1.setBounds(10, 50, 150, 30);
add(btn1);
btn.addActionListener(this);
bk = new obstMov();
add(bk);
Thread td = new Thread(bk);
td.start();
for(int i=0; i<5; i++){
Ball b = new Ball(i+"", 5);
add(b);
ballList.add(b);
Thread t = new Thread(b);
t.start();
}
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MainApp();
}
public void actionPerformed(ActionEvent arg0) {
for (Ball b : ballList) {
b.pause = state;
}
state = !state;
}
}
and here is the class containing the object to moved(paddle) called obstMov
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class obstMov extends JPanel implements Runnable{
boolean cont;
//int newX, newY;
public static int x = 250;
public static int xa = 0;
private static final int y = 520;
obstMov(){
setBounds((int)(x),(int)(y),80,20);
cont = true;
}
public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0, 80, 20);
}
public void run() {
if (x + xa > 0 && x + xa < getWidth()-60)
x = x + xa;
}
public void keyReleased(KeyEvent e) {
xa = 0;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
xa = -3;
setBounds((int)(x - xa),(int)(y),80,20);
System.out.println(xa);
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = 3;
setBounds((int)(x + xa),(int)(y),80,20);
System.out.println(xa);
}
}