-1

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);
}



}
  • 1
    1. Don't move a component but rather an image or a sprite. This is much more light-weight. 2. Do all painting in one JPanel, your drawing JPanel. 3. Paint in the paintComponent method and call the super's method first thing. 4. Avoid direct use of threads as that is dangerous. Better to use Swing Timers, or just move on key press. 5. Prefer to use [Key Bindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) over KeyListeners for less focus issues. – Hovercraft Full Of Eels May 17 '22 at 10:54

0 Answers0