0

I am making ping pong using java.I am not very familiar with java yet as i started only a few months back. I added a KeyListener which checks whether the user has pressed the UP key and DOWN key and also when these keys are released.But the Keylistener isn't working.I added a System.out.print() to print the keycode but that doesnt happen either.this is my code(The main part of it)

import java.applet.Applet;
import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;



public class pingpong extends Applet implements Runnable,KeyListener{

    Thread thread;

    final int WIDTH = 700 ,HEIGHT = 500;

    HumanPaddle p1;

    Player2Paddle p2;

    Ball ball;

    boolean game;

    Graphics G;

    Image img;



   public void init(){

       this.resize(WIDTH,HEIGHT);

       game = false;

       this.addKeyListener(this);

       p1 = new HumanPaddle(1);



       ball = new Ball();

       p2 = new Player2Paddle(2,ball);

       img = createImage(WIDTH,HEIGHT);

       G = img.getGraphics();

       thread = new Thread(this);

       thread.start();

   }

   public void paint(Graphics g){

       G.setColor(Color.black);

       G.fillRect(0,0,WIDTH,HEIGHT);

       if(ball.getX() < -10 || ball.getX()>710){

           G.setColor(Color.blue);

           G.drawString("Game over",350,250);

       }

       else{

       p1.draw(G);

       ball.draw(G);

       p2.draw(G);

       p2.move(); 

       }

       g.drawImage(img,0,0,this);

   }

   public void update(Graphics g){

       paint(g);

   }





    public void run() {

        for(;;){ 

            if(game){

            ball.move();

            p1.move();

            ball.paddleHit(p1,p2);

            repaint();
            }

            try {

                Thread.sleep(10);

                        } catch (InterruptedException e) {

                            e.printStackTrace();
             }

        }
    }



    public void keyPressed(KeyEvent e) {

        if(e.getKeyCode() == KeyEvent.VK_UP){

        p1.setUpAccel(true);

        System.out.println("check!"+e.getKeyCode());

    }

    else if(e.getKeyCode() == KeyEvent.VK_ENTER){

        game = true;

    }

}



public void keyReleased(KeyEvent e) {



    if(e.getKeyCode() == KeyEvent.VK_UP){

        p1.setUpAccel(false);

    }

    else if(e.getKeyCode() == KeyEvent.VK_DOWN){

        p1.setDownAccel(false);

    }   

}

}

I am using NetBeans 8.2 and jdk 1.8. Can someone help me please

वरुण
  • 1,237
  • 3
  • 18
  • 50
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web). 3) Why use AWT? .. – Andrew Thompson Jul 17 '17 at 08:21
  • .. See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. – Andrew Thompson Jul 17 '17 at 08:21
  • So you are suggesting JFrame in place of Applet? –  Jul 17 '17 at 09:51
  • Yep. You seem to have got both pieces of advice neatly wrapped up in one thought. :) – Andrew Thompson Jul 17 '17 at 09:57
  • Ok thanks...so how should my code look like? ( I don't know how to convert all this into the public static void main() thing ) –  Jul 17 '17 at 10:49
  • 1) The `PacManComponent` in [this example](https://stackoverflow.com/a/14437899/418556) shows how to animate and how to custom paint in a panel. 2) That last example was displayed in a `JOptionPane` for simplicity, [this example](https://stackoverflow.com/a/44139133/418556) uses a `JFrame` as the top level component. Some notes: 1) The code in the `init()` method needs to go in the constructor (or a method called from the constructor). .. – Andrew Thompson Jul 17 '17 at 11:42
  • .. 2) We recommend against overriding the paint method of a top level container. For Swing, we'd add a `JPanel` and override the `paintComponent()` method of the panel instead. 3) When custom painting, always call the `super` method first. 4) When making a custom component, override the `getPreferredSize()` method to return an appropriate value. That way, we can `pack()` the top level container to the exact size it needs to be in order to display the components it holds. .. – Andrew Thompson Jul 17 '17 at 11:42
  • .. 5) Don't use a `Thread` for animation. In Swing we have the `Timer` class that can invoke code on a regular basis. Better, that code is invoked on the Event Dispatch Thread, where it needs to occur for a GUI. 6) The [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) tutorial is a good resource. - Hopefully you can take the hint that since it took three 'comment blocks' to convey those tips, that if you need any more information, it should be in a dedicated question! – Andrew Thompson Jul 17 '17 at 11:43

0 Answers0