-2

My program has 3 java files, namely Frame, Dude(which contains the character) and Board(which implements the actionListener). My program is not throwing any error and the images(background and character) are rendering good. But the character is not moving forward.


import javax.swing.*;

public class Frame {
    public static void main(String[] args){
        JFrame frame= new JFrame("2D Game");
        frame.add(new Board());
        frame.setVisible(true);
        frame.setSize(1200, 600);
    }
}

import java.awt.*;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Dude {
    int x, dx, y;
    Image still;

    public Dude(){
        ImageIcon i = new ImageIcon("/home/amitabh/Pictures/man1.jpg");
        still= i.getImage();
        x=10;
        y=172;
        }

    public void move(){
        x=x+dx;
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public Image getImage(){
        return still;
    }

    public void keyPressed(KeyEvent e){
        int key= e.getKeyCode();

        if(key== KeyEvent.VK_LEFT);
        dx= -1;

        if(key== KeyEvent.VK_RIGHT);
        dx= 1;
    }

    public void keyReleased(KeyEvent e){
        int key= e.getKeyCode();

        if(key==KeyEvent.VK_LEFT);
        dx=0;

        if(key==KeyEvent.VK_RIGHT);
        dx=0;
    }
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel implements ActionListener{
    Image img;
    Timer time;
    Dude p;

    public Board(){
        p= new Dude();
        addKeyListener(new AL());
        setFocusable(true);
        ImageIcon i= new ImageIcon("/home/amitabh/Pictures/game1.png");
        img= i.getImage();
        time= new Timer(5,this);
        time.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        p.move();
        repaint();

    }

    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2d= (Graphics2D)g;

        g2d.drawImage(img, 0,0, null);
        g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
    }

    public class AL extends KeyAdapter{
        public void keyReleased(KeyEvent e){
            p.keyReleased(e);
        }
        public void KeyPressed(KeyEvent e){
            p.keyPressed(e);
        }
    }
}
Vaibhav Bajaj
  • 1,934
  • 16
  • 29
Amitabh
  • 1
  • 1
  • Are you sure the `KeyListener` is be called? – MadProgrammer Jan 30 '17 at 05:02
  • thats the exact program...if I missed it ...pls help@MadProgrammer – Amitabh Jan 30 '17 at 05:05
  • yeah the keylistener is in the Board class constructor@MadProgrammer – Amitabh Jan 30 '17 at 05:09
  • You need to start learning to debug, `KeyListener` is fickel and sometimes won't trigger events, how ever, in your case, that's not the primary problem – MadProgrammer Jan 30 '17 at 05:13
  • how to learn debugging...any inputs that can help me@MadProgrammer – Amitabh Jan 30 '17 at 05:17
  • Use `System.out.println` to print messages to the console at various points in your code to verify. If you're using of the Java IDE's they have the capabilities to allow you to place breakpoints in the code (and when in debug mode) inspect the state of the variables and allow you to step through your code, it's a bit of black magic, but a lot of fun to – MadProgrammer Jan 30 '17 at 05:23

1 Answers1

2

Start by talking a very close look at:

if (key == KeyEvent.VK_LEFT);

Does that look funny to you?

if (key == KeyEvent.VK_LEFT); // <--- What's the ; doing here?

Change it to be more like...

if (key == KeyEvent.VK_LEFT) {
    dx = 0;
}

And, yes, this is why you're encouraged to use { ... }

Next, take a closer look at...

public void KeyPressed(KeyEvent e) {

See anything wrong there? Why does it start with an uppercase K, that's not the correct method signature

Change to something more like...

@Override
public void keyReleased(KeyEvent e) {
    p.keyReleased(e);
}

@Override
public void keyPressed(KeyEvent e) {
    p.keyPressed(e);
}

Yes, this is why you're encouraged to use @Override ;)

And finally change your paint method to paintComponent

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

You are encouraged to override paintComponent when performing custom painting, it tends to cause less issues

I'd also encorage you to have a look at the Key Bindings API and favour it over KeyListener as it provides better control of the focus level required to trigger the key events

I'd also encourage your to override getPreferredSize of the Board and return your preferred size from there, rather then setting the frame's size. The frame size includes the frame's decorations, so you content is smaller then the frame size

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366