0

I am trying to put keylistener and move the "download.jpg" to the left and right. I put the ; but the error says that ; is needed. Did I do anything wrong?? What do I need to fix?? Do I need to add anything?

    public class GameScreen extends JPanel implements KeyListener    {

        public  BufferedImage bg;
        public  BufferedImage ci;
        public JLabel lab;
        int ciXp = 250;
        int ciYp = 665;
        int ciWidth = 330;
        int ciHeight = 745;

    public GameScreen()
    {
        gui();

    }

    public void gui()
    {
    try{
         bg = ImageIO.read(new File("supermario.jpg"));
         ci = ImageIO.read(new File("download.png"));
        }
        catch(Exception ex){
            }



    }


    public void paintComponent(Graphics g)
    {
        g.drawImage(bg,0,0,null);
        g.drawImage(ci,ciXp,ciYp,ciWidth,ciHeight,0,0,ci.getWidth(),ci.getHeight(),null);
    }

    public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
       int ciXp-10;
    }
    if (key == KeyEvent.VK_RIGHT) {
       int ciXp+10;
    }
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hyunseok Song
  • 21
  • 1
  • 6
  • What are you trying to accomplish with `int ciXp-10;`. Furthermore you did not implement at `KeyListener` at all. – Murat Karagöz May 19 '15 at 15:06
  • I want to move the download image left 10.. – Hyunseok Song May 19 '15 at 15:06
  • Your `addKeyListener` is missing from the code – Blip May 19 '15 at 15:07
  • 1
    *"..but the error says that ; is needed"* 1) Always copy/paste error and exception output! 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. .. – Andrew Thompson May 19 '15 at 15:17
  • Where have you added the `keyListener` to your `JPanel`? – Blip May 19 '15 at 15:19
  • 2
    .. 4) For Swing, we typically use [key bindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) rather than the lower level `KeyListener`. 5) `catch(Exception ex){}` should at the very least, be `catch(Exception ex){ ex.printStackTrace(); }` 6) `g.drawImage(bg,0,0,null);` should be `g.drawImage(bg,0,0,this);` 7) `public void paintComponent(Graphics g) {..` should be `public void paintComponent(Graphics g) { super.paintComponent(g); ..` – Andrew Thompson May 19 '15 at 15:19
  • have you got any line like `addKeyListener(this);` in you code? if so kindly post it where it is. – Blip May 19 '15 at 15:52
  • See [this](http://stackoverflow.com/questions/22741215/how-to-use-key-bindings-instead-of-key-listeners/). – user1803551 May 19 '15 at 21:35

1 Answers1

0

Add the following line in your gui() method,

addKeyListener(this);
Blip
  • 3,061
  • 5
  • 22
  • 50