So I am working on a Java game using a combination of swing components, and Graphics2D.
I have an AbstractLevelView class which contains all of the elements of the game inside. This LevelView component uses a BorderLayout to display a context bar on the left, and a JPanel containing the game grid in the center.
My context bar and gridPanel all work fine, but what I want to do is paint "messages" directly to the AbstractLevelView, but I can't get any painting to work. This is presumably because there is nowhere to paint due to the nested components.
I've included an abbreviated version of my AbstractLevelView, as well as a test paintComponent method. I have tried using paintComponents, but it doesn't even get called.
Does anyone know what I can do so I can paint on top of my nested components?
public abstract class AbstractLevelView extends AbstractGameView {
private JPanel gridPanel;
private ContextBar contextBar;
public AbstractLevelView(){
gridPanel = new JPanel();
add(gridPanel, BorderLayout.CENTER);
contextBar = new ContextBar();
add(contextBar, BorderLayout.WEST);
}
@Override
public void paintComponent(Graphics g){
System.out.println("foo");
Image img = ImageManager.getImage("img.jpg");
g.drawImage(img, 0, 0, 300, 300, this);
}
}