-1
 public class Pong extends JPanel  {

    int x=0;
    int y=0;
    int a;
    int b;

    int border=30;
    boolean balldown=true;
    boolean ballright=true;
    int bounce=0;

    private void moveBall(){
        if (balldown==true){
            y++; 
            bounce++;
        }

        if (balldown==false){
            y--;
            bounce++;
        }

        if(y==getHeight()-border){
            balldown=false;
            bounce++;
        }

        if(y==0){
            balldown=true;
            bounce++;
        }

        if (ballright==true){
            x++;          
        }

        if (ballright==false){
            x--;
            bounce++;
        }

        if(x==getWidth()-30){
            ballright=false;
            bounce++;
        }
        if(x==0){
            ballright=true;
            bounce++;
        }   
    }

    @Override
    public void paint(Graphics g){

        super.paint(g);

        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 1080, 760);

        g.setColor(Color.WHITE);
        g.fillOval(x, y, 30, 30);

        g.setColor(Color.WHITE);
        g.fillRect(0, a, 30, 200);

        g.setColor(Color.WHITE);
        g.fillRect(980, b, 30, 200);

        g.fillRect(520, 0, 10, 760);
    }

    public  Pong() implements KeyListener {

     void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_UP) {
            a += 10;
            System.out.println("++++");
            return;
        }

        if (key == KeyEvent.VK_DOWN) {
            a -= 10;

        }
    }

}
public static void main(String[] args) throws InterruptedException {

    JFrame frame = new JFrame("Pong");
    frame.setSize(1024,760);
    frame.setVisible(true);
   // frame.createBufferStrategy(3);
    //BufferStrategy strategy=frame.getBufferStrategy();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Pong game=new Pong();
    frame.add(game);

    while(true){
        game.moveBall();
        game.repaint();
         Thread.sleep(1);
   }
 }

}

Im a beginner at jAVA I want to implement a keylistener that will change the coordonates of 2 rectangles but I cant seem to make it work. I get the compilation error that says "; expected " on the class that implements KeyListener. I know what the error means I dont know how to solve it in this case

azro
  • 53,056
  • 7
  • 34
  • 70
  • This line is completely invalid: `public Pong() implements KeyListener` - you need to move it to class declaration: `class Pong extends JPanel implements KeyListener` – Nowhere Man May 03 '20 at 15:01

1 Answers1

0

It looks like you've put the "implements KeyListener" into the Pong contructor. Move the implements to the class declaration and pull the KeyListener / keyPressed code out of the constructor Pong()

public class Pong extends JPanel implements KeyListener

DuncG
  • 12,137
  • 2
  • 21
  • 33
  • I did try that before posting here, it says "Pong is not abstract and does not override abstract method KeyReleased in KeyListener" If I make Pong abstract the whole thing stops working – JumboJet24 May 03 '20 at 15:39
  • You shouldn't add abstract - you just need to add the other calls that all implementors of KeyListener must have. Define "public void keyTyped(KeyEvent e) {} and "public void keyReleased(KeyEvent e) {}" – DuncG May 03 '20 at 15:59
  • I see ,thank you very much now there are no errors. But still my KeyPressed method does not change the coordonate a of my rectangle. Is there something wrong with the way I wrote it? @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_UP) { a += 10; System.out.println("++++"); return; } if (key == KeyEvent.VK_DOWN) { a =a-10; } – JumboJet24 May 03 '20 at 17:09
  • Have you linked the listener to the source object? Lookup details of an addListener(x) call suitable the components in your application. – DuncG May 03 '20 at 17:23
  • Yeah you I are right did not have that. I still dont know how to add a listener but thanks so much for the help – JumboJet24 May 03 '20 at 18:41
  • After changing your local variables the JPanel will need redrawing because it will show the old state. See other posts such as https://stackoverflow.com/questions/4392722/how-to-repaint-a-jpanel-after-have-drawn-on-it . Also I suggest you use a development environment such as Eclipse from eclipse.org - it will suggests auto-completion for finding the calls for adding listeners. – DuncG May 04 '20 at 08:58
  • Also check you have called this.addKeyListener(this) will makes the Pong object receive the events. See https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#addKeyListener(java.awt.event.KeyListener) – DuncG May 04 '20 at 09:10