Issue with calling a method and updating graphics

67 views Asked by At

I have a KeyListener that works, but when I try to call a method such as getdx(), I'm not seeing any change. goal is to make a map move with arrows.

I've searched google and stackoverflow in various places, chopped my code all up and it's now pretty messy. what am I doing wrong?

public class GuiPanel extends JPanel implements ActionListener 
{
    private static final long serialVersionUID = 1L;
    private Timer timer;
    private DrawMap drawmap = new DrawMap();
    private DrawChar drawchar = new DrawChar();
    KeyBoard keyboard = new KeyBoard();
    boolean change = false;


    public GuiPanel()
    {

        KeyListener listener = new KeyBoard();
        addKeyListener(listener);
        initGuiPanel();
    }

    private void initGuiPanel() 
    {

        setFocusable(true);
        setBackground(Color.BLACK);
        setDoubleBuffered(true);

        timer = new Timer(1000, this);
        timer.start();
        new GameLogic ("gamelogic").start();
    }

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

        draw(g);

        Toolkit.getDefaultToolkit().sync();
    }

    private void draw(Graphics g) 
    {

        Graphics2D g2d = (Graphics2D) g;

        drawmap.drawmap(g2d);
        drawchar.drawchar(g2d);
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        keyboard.getdx();
        keyboard.getdy();
        drawmap.move(keyboard.getdx(),keyboard.getdy());

        repaint();
    }
}

public class KeyBoard extends KeyAdapter 
{
    private int dx;
    private int dy;
    private int angle=1;
    private boolean change = false;
    KeyBoardLogic keyboardlogic = new KeyBoardLogic();

    public int getdx()//does not return proper value
    {
        return keyboardlogic.getdx();
    }

    public void keyPressed(KeyEvent e) 
    {
        keyboardlogic.keypressed(e);
    }

    public void keyReleased(KeyEvent e) 
    {
        keyboardlogic.keyreleased(e);
    }
}

public class KeyBoardLogic 
{

    private int dx=0;

    public int getdx()
    {
        return dx;
    }

    public void keypressed(KeyEvent e)
    {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) 
        {
            dx = -1;//does not update when getdx() is called

            System.out.println("left");//works
        }
    }

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

        if (key == KeyEvent.VK_LEFT) 
        {
            dx = -1;//just to know that something happened...
        }
    }
}
2

There are 2 answers

0
Nomadyn On BEST ANSWER

Got it. after a week of troubleshooting. dx and dy needed to be static. also a number of other coding errors masked the main problem.

3
Prerak Sola On

Initialize dx in the constructor of KeyBoardLogic like this:

public class KeyBoardLogic
{
    private int dx;

    public keyBoardLogic()
    {
        this.dx = 0;
    }

    //Remaining methods....
}