I have a custom class which extends JPanel and overrides the paintComponent() method. Here's what that looks like:
@Override
protected void paintComponent(Graphics g)
{
if(redrawRequested)
{
g.setColor(ETCH_BACKGROUND_COLOR);
g.fillRect(0, 0, owner.getAppWidth(), owner.getAppHeight());
redrawRequested = false;
}
g.setColor(Color.BLACK);
g.fillRect(pointX, pointY, PIXEL_SIZE, PIXEL_SIZE);
}
The same custom class also implements KeyListener. I am listening for some keys, and more specifically Space which calls a method requestRepaint(). Here's what that method looks like:
public void requestRepaint()
{
redrawRequested = true;
revalidate();
repaint();
}
My problem is, when the program starts, whats in the if block inside paintComponent get's fired but the fillRect doesn't work(?) or is not the right color. I have checked my custom color ETCH_BACKGROUND_COLOR and it isn't null and it carries the correct values. Also, the the redrawRequested boolean is true, like I said the if IS BEING FIRED.
I have tried forcing the panel to do a requestRepaint() after the frame is visible, I have tried doing it after a Thread.sleep() and neither accomplishes drawing the background color properly.
The ONLY way I have gotten this to work is when I use AWT's Robot and simulate a key press on the Space key.
Any ideas why this isn't working?