Hi guys!
So I’ve been wanting to change the way my menu is going to be initialized and all - basically just remove all swing components, apart from maybe the jframe holding the canvas? Anyway, I’ve run into a snag, that is the Graphics2D object won’t draw whatever I want it to draw. I think the problem lies in it only being painted once? - something that shouldn’t matter? Here is the code linked to my problem.
private void init() {
GUI gui = new GUI();
handler = new InputHandler(gui);
gameFrame = new JFrame("ShootEm UP");
gameFrame.setLayout(null);
gameFrame.setPreferredSize(new Dimension(CONST_WIDTH, CONST_HEIGHT));
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setResizable(false);
gameFrame.setIgnoreRepaint(true);
designMenuBar();
canvas = new Canvas();
canvas.setBounds(0, 0, CONST_WIDTH, CONST_HEIGHT);
canvas.setIgnoreRepaint(true);
canvas.addMouseMotionListener(handler);
canvas.addMouseListener(handler);
canvas.addKeyListener(handler);
gameFrame.add(canvas);
gameFrame.pack();
canvas.createBufferStrategy(2);
canvas.requestFocus();
render();
gameFrame.setVisible(true);
}
public void render() {
try {
BufferStrategy bf = canvas.getBufferStrategy();
Graphics2D g = (Graphics2D) bf.getDrawGraphics();
test(g);
g.dispose();
bf.show();
Toolkit.getDefaultToolkit().sync();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void test(Graphics2D g) {
g.setColor(Color.GREEN);
g.drawString("LOL", 100, 100);
g.fillRect(500, 500, 100, 100);
}
It’s safe to rule out the gui variable, the handler variable and designMenuBar() function. Anyone got any ideas?