Thing is, the buttons were working perfectly before (drawn by calling paintComponents(g) on my JFrame ) but my program was a JPanel so I had all sorts of issues with things being drawn underneath the JFrame’s title bar and such. The thing is if I try to call the paintComponents() method woth my canvas I get a huge gray block although I can still see my background image around the edges (because the block is slightly smaller than the drawing surface) I don’t know if this bcause of some shortcomings in the canvas class or my coding.
Here’s some code if it helps:
public class MyGame extends Canvas implements Stage
{
private JFrame gameWindow;
private JPanel contentPane;
private JPanel activeLayer;
private BufferStrategy strategy;
public MyGame()
{
quitGame = false;
spriteCache = new SpriteCache();
gameWindow = new JFrame("Soccer");
contentPane = (JPanel)gameWindow.getContentPane();
activeLayer = new JPanel();
activeLayer.setBounds(0,30,Stage.WIDTH,Stage.HEIGHT);
activeLayer.setLayout(new FlowLayout(FlowLayout.LEFT));
setBounds(0,0,Stage.WIDTH,Stage.HEIGHT);
//Configure the contentPane
contentPane.setOpaque(false);
contentPane.setLayout(null);
contentPane.add(this);
contentPane.setPreferredSize(new Dimension(Stage.WIDTH,Stage.HEIGHT));
//Configure the main Window
gameWindow.setBounds(0,0,Stage.WIDTH,Stage.HEIGHT);
gameWindow.setVisible(true);
gameWindow.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
gameWindow.setResizable(false);
createBufferStrategy(2);
strategy = this.getBufferStrategy();
requestFocus();
NullRepaintManager.install();
}
public void menuSystem()
{
Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
MenuManager menuManager = new MenuManager(match, resources.getMenuGraphics());
activeLayer.setOpaque(false);
//This returns the JPanel containing the buttons
activeLayer.add(menuManager.getMenu(0));
contentPane.setOpaque(false);
contentPane.add(activeLayer);
gameWindow.validate();
boolean doDraw = true;
while(doDraw)
{
g.drawImage((BufferedImage)resources.getMenuGraphics().get(0),0,-29,null);
menuManager.paintMenu(g);
activeLayer.paintComponents(g);
strategy.show();
}
}
I cut a few methods out of the code because they’re irrelevant and I doubt many people would appreciate reading through 400+ lines.