Issue with paintComponent() or KeyListener

Hi guys,

Over the past month or so I've been learning the foundations of Java / OOP and decided I'd tackle a fairly basic game: Boulder Dash. The general idea is to have a bulldozer run through the level, demolishing dirt and picking up gems (and avoiding the stones). My basic concept is to create a two dimensional array of 'Block' objects to draw my levels. These 'Blocks' are (for anyone who hasn't played boulder dash): stones, gems and dirt.

I've been able to create and fill an array of my Block() objects with no issues, and then initially draw the level onto the panel without issue. Once this happens, one of the objects (dozer) needs to be able to run around smashing through dirt, collecting gems, etc. Despite my best efforts, my KeyListener doesn't work, or I don't quite understand how to repaint correctly.

The following is the code for my block object (block.java), although I don't believe there are any gaping issues here causing my problems.

This is my panel (LevelGen.java). What it will do is create an array of my Block() objects, fill them with the correct information based on level input (just basic strings at this point for proof of concept), and then the paintComponent method will pull the relevant information out of the array to draw (and redraw) everything where appropriate. As I said, I think my main issue is with keyListener, which is intended to move around the bulldozer.

Re: Issue with paintComponent() or KeyListener

One problem with keylisteners could be that the component does not have the focus and is not able to receive the focus.
Make sure the component with the listener is able to get the focus. There are methods to call to enable that.