When I run the game and try to use the Arrow keys to control the movement of the selection box, nothing happens. I'm pretty sure the game is redrawing itself properly because the ticker text is updating. Aswell, I tried doing:

selectVal++;

in the update function in TankShooter.Java, which made the box redraw and change position everytime.

Here's all my code.

-- Game.Java

package org.game.engine;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

public abstract class Game implements KeyListener, MouseListener, MouseMotionListener, MouseWheelListener {

    protected boolean over;
    protected String title = "My Game";
    protected int width=400, height=300;
    protected int delay = 30;

    public void init() {}
    abstract public void update();
    abstract public void draw(Graphics2D g);


    public boolean isOver() { return over; }
    public String getTitle() { return title; }
    public int getWidth() { return width; }
    public int getHeight() { return height; }
    public int getDelay() { return delay; }
    public void resize(int width, int height) {}

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseWheelMoved(MouseWheelEvent e) {
    }

}

-- GameApplication.Java

package org.game.engine;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class GameApplication {

    public static void start(final Game game) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame(game.getTitle());
                frame.setSize(game.getWidth(), game.getHeight());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                GameCanvas canvas = new GameCanvas(game);
                canvas.setGame(game);
                frame.add(canvas);
                frame.setVisible(true);
                canvas.requestFocus();
                new GameLoop(game, canvas).start();
            }
        });
    }
}

-- GameCanvas.Java

package org.game.engine;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JComponent;

@SuppressWarnings("serial")
public class GameCanvas extends JComponent implements ComponentListener {

    private Game game;

    public GameCanvas() {
    }

    public GameCanvas(Game game) {
        this.game = game;
        addKeyListener(game);
        addMouseListener(game);
        addMouseMotionListener(game);
        addMouseWheelListener(game);
        requestFocus();
        addComponentListener(this);
    }

    public void setGame(Game game) {
        this.game = game;
        addKeyListener(game);
        addMouseListener(game);
        addMouseMotionListener(game);
        requestFocus();
        addComponentListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        game.draw((Graphics2D)g);
    }

    @Override
    public void componentResized(ComponentEvent ce) {
        game.resize(ce.getComponent().getWidth(), ce.getComponent().getHeight());   
    }

    @Override
    public void componentMoved(ComponentEvent ce) {

    }

    @Override
    public void componentShown(ComponentEvent ce) {
        //game.resize(ce.getComponent().getWidth(), ce.getComponent().getHeight());
    }

    @Override
    public void componentHidden(ComponentEvent ce) {

    }

}

-- GameLoop

package org.game.engine;

import java.util.logging.Level;
import java.util.logging.Logger;

public class GameLoop extends Thread {

    private final Game game;
    private final GameCanvas canvas;
    private boolean stopped;
    private boolean paused;

    public GameLoop(Game game, GameCanvas canvas) {
        this.game = game;
        this.canvas = canvas;
        this.stopped = false;
        this.paused = false;
    }

    public void pauseGame() {
        this.paused = true;
    }

    public void resumeGame() {
        this.paused = false;
    }

    public void stopGame() {
        stopped = true;
    }

    @Override
    public void run() {
        game.init();

        while (!game.isOver() && !stopped) {

            if (!paused) {
                game.update();
                canvas.repaint();
            }

            try {
                Thread.sleep(game.getDelay());
            } catch (InterruptedException ex) {
                Logger.getLogger(GameLoop.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

-- TankShooter.Java

package org.game.tankshooter;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.game.engine.Game;
import org.game.engine.GameApplication;

public class TankShooter extends Game {

    public static void main(String[] args) {
        GameApplication.start(new TankShooter());
    }

    //Variable Declarations
    int x;
    int y;
    int selModX;
    int selModY;
    int selectVal;
    int ticker = 0;

    BufferedImage GUI_BottomGUI;
    BufferedImage GUI_SelectGUI;

    public TankShooter() {
        title = "Tank Shooter";
        width = 950;
        height = 650;
        selModX = 0;
        selModY = 0;

    //Graphics Imports
        try {
            GUI_BottomGUI = ImageIO.read(new File("Resources/Images/BottomGUI.png"));
            GUI_SelectGUI = ImageIO.read(new File("Resources/Images/SelectGUI.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == ((KeyEvent.VK_UP) | (KeyEvent.VK_DOWN)) && (selectVal == 1) | (selectVal == 3)) {
            selectVal += 1;
        }
        if (e.getKeyCode() == ((KeyEvent.VK_DOWN) | (KeyEvent.VK_UP)) && (selectVal == 2) | (selectVal == 4)) {
            selectVal -= 1;
        }
        if (e.getKeyCode() == ((KeyEvent.VK_RIGHT) | (KeyEvent.VK_LEFT)) && (selectVal == 1) | (selectVal == 3)) {
            selectVal += 2;
        }
        if (e.getKeyCode() == ((KeyEvent.VK_RIGHT) | (KeyEvent.VK_LEFT)) && (selectVal == 2) | (selectVal == 4)) {
            selectVal -= 2;
        }
    }

    @Override
    public void update() {
        ticker++;
        if (selectVal == 1) {
            selModX = 0;
            selModY = 0;
        }
        else if (selectVal == 2) {
            selModX = 0;
            selModY = GUI_SelectGUI.getHeight();
        }
        else if (selectVal == 3) {
            selModX = GUI_SelectGUI.getWidth();
            selModY = 0;
        }
        else if (selectVal == 4) {
            selModX = GUI_SelectGUI.getWidth();
            selModY = GUI_SelectGUI.getHeight();
        }

        //selectVal++; 
        if (selectVal > 4) {
            selectVal = 0;
        }
    }

    @Override
    public void draw(Graphics2D g) {
        g.drawImage(GUI_BottomGUI, 0, 462, null);
        g.drawImage(GUI_SelectGUI, (13+selModX), (475+selModY), null);
        g.drawString(String.valueOf(selectVal), 5, 10);
        g.drawString(String.valueOf(ticker), 5, 20);
    }

    @Override
    public void init() {

    }

}

So I need to declare a new action, then where do I put the code for creating a new key binding?

PS. I forgot to mention that the keyboard stuff seemed to work when the

selectVal++;

was in the code. As in it would show more briefly on the right side when I continuously pressed the right arrow key. But not when holding it.

where do I put the code for creating a new key binding?

Where you are initialising the window - eg in your GameCanvas constructor?
I don't really understand your ps. Are you saying the keyboard listener is being called or not?

I believe it is, I don't see why it's not. This is in the code

public GameCanvas(Game game) {
    this.game = game;
    addKeyListener(game);
    addMouseListener(game);

And that's in GameCanvas.Java

It seems to me that e.getKeyCode() == ((KeyEvent.VK_UP) | (KeyEvent.VK_DOWN)) represents a shocking misunderstanding of Java operators on some fundamental level. When the event is for the "up" key, e.getKeyCode() will be equal to VK_UP. When the event is for the "down" key, e.getKeyCode() will be equal to VK_DOWN. I presume you are thinking of one or both of those two cases, but what you have written represents neither of those cases because VK_UP | VK_DOWN is a third value that isn't the same as either VK_UP or VK_DOWN, just as 1 + 2 is neither 1 nor 2. As it happens, VK_UP | VK_DOWN is VK_PERIOD.

The real problem comes from the fact that the == operator has only one value on each side. It can only be used to compare one single value against another single value. You seem to be trying to use it to compare e.getKeyCode() against two values, and unfortunately you have chosen to do it in a way that produces no compiler errors because the compiler recognizes your code as doing something entirely different from what you want.

So how would you recommend fixing it? Im really confused and it would be nice to have some code as to where to start.

I also am not sure as to where your getting VK_UP | VK_DOWN is equal to VK_PERIOD.

Edited 3 Years Ago by Doogledude123

Alright, so I revised the code to try to avoid all the conditions and such. Here it is.
Still the same problem.

package org.game.tankshooter;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.game.engine.Game;
import org.game.engine.GameApplication;

public class TankShooter extends Game {

    public static void main(String[] args) {
        GameApplication.start(new TankShooter());
    }

    //Variable Declarations
    int x;
    int y;
    int selModX;
    int selModY;
    int selectVal;
    int selectValKey;
    int ticker = 0;

    BufferedImage GUI_BottomGUI;
    BufferedImage GUI_SelectGUI;

    public TankShooter() {
        title = "Tank Shooter";
        width = 950;
        height = 650;
        selModX = 0;
        selModY = 0;
        selectVal = 1;

    //Graphics Imports
        try {
            GUI_BottomGUI = ImageIO.read(new File("Resources/Images/BottomGUI.png"));
            GUI_SelectGUI = ImageIO.read(new File("Resources/Images/SelectGUI.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        selectValKey = e.getKeyCode();
    }

    @Override
    public void update() {
        if (selectVal > 3) {
            selectVal = 0;
        }
        if (selectVal < 0) {
            selectVal = 0;
        }

        if (selectVal == 0) {
            selModX = 0;
            selModY = 0;
        }
        if (selectVal == 1) {
            selModX = 0;
            selModY = GUI_SelectGUI.getHeight();
        }
        if (selectVal == 2) {
            selModX = GUI_SelectGUI.getWidth();
            selModY = 0;
        }
        if (selectVal == 3) {
            selModX = GUI_SelectGUI.getWidth();
            selModY = GUI_SelectGUI.getHeight();
        }
        switch (selectValKey) {
        case KeyEvent.VK_LEFT:
            selectVal -= 2;
            break;
        case KeyEvent.VK_UP:
            selectVal += 1;
            break;
        case KeyEvent.VK_RIGHT:
            selectVal += 2;
            break;
        case KeyEvent.VK_DOWN:
            selectVal -= 1;
            break;
        }

    }

    @Override
    public void draw(Graphics2D g) {
        g.drawImage(GUI_BottomGUI, 0, 462, null);
        g.drawImage(GUI_SelectGUI, (13 + selModX), (475 + selModY), null);
    }

    @Override
    public void init() {

    }

}

The rest of the classes are the same.

Sorry for triple post, figured it out.
Final code:

package org.game.tankshooter;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.game.engine.Game;
import org.game.engine.GameApplication;

public class TankShooter extends Game {

    public static void main(String[] args) {
        GameApplication.start(new TankShooter());
    }

    //Variable Declarations
    int x;
    int y;
    int selModX;
    int selModY;
    int selectVal;
    int selectValKey;
    int ticker = 0;

    BufferedImage GUI_BottomGUI;
    BufferedImage GUI_SelectGUI;

    public TankShooter() {
        title = "Tank Shooter";
        width = 950;
        height = 650;
        selModX = 0;
        selModY = 0;
        selectVal = 1;

    //Graphics Imports
        try {
            GUI_BottomGUI = ImageIO.read(new File("Resources/Images/BottomGUI.png"));
            GUI_SelectGUI = ImageIO.read(new File("Resources/Images/SelectGUI.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        selectValKey = e.getKeyCode();
    }

    @Override
    public void update() {
        if (selectVal > 4) {
            selectVal = 4;
        }
        if (selectVal < 1) {
            selectVal = 1;
        }

        if (selectVal == 1) {
            selModX = 0;
            selModY = 0;
        }
        if (selectVal == 2) {
            selModX = 0;
            selModY = GUI_SelectGUI.getHeight();
        }
        if (selectVal == 3) {
            selModX = GUI_SelectGUI.getWidth();
            selModY = 0;
        }
        if (selectVal == 4) {
            selModX = GUI_SelectGUI.getWidth();
            selModY = GUI_SelectGUI.getHeight();
        }
        switch (selectValKey) {
        case KeyEvent.VK_LEFT:
            if ((selectVal == 3) | (selectVal == 4)) {
            selectVal -= 2;
            } else {
                //do nothing
            }
            break;
        case KeyEvent.VK_UP:
            if ((selectVal == 2) | (selectVal == 4)) {
            selectVal -= 1;
            } else {
                //do nothing
            }
            break;
        case KeyEvent.VK_RIGHT:
            if ((selectVal == 1) | (selectVal == 2)) {
            selectVal += 2;
            } else {
                //do nothing
            }
            break;
        case KeyEvent.VK_DOWN:
            if ((selectVal == 1) | (selectVal == 3)) {
            selectVal += 1;
            } else {
                //do nothing
            }
            break;
        }

    }

    @Override
    public void draw(Graphics2D g) {
        g.drawImage(GUI_BottomGUI, 0, 462, null);
        g.drawImage(GUI_SelectGUI, (13 + selModX), (475 + selModY), null);
        g.drawString(Integer.toString(selectVal), 5, 10);
    }

    @Override
    public void init() {

    }

}
This question has already been answered. Start a new discussion instead.