java - JFrame background color not working -
my code
public static void main(string[] args) throws interruptedexception { jframe frame = new jframe("flappy bird"); frame.setsize(1200, 800); flappybird game = new flappybird(); frame.getcontentpane().setbackground(color.yellow); frame.add(game); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setresizable(false); while (true) { game.moveball(); game.gameover(); game.moverect(); game.repaint(); thread.sleep(14); } }
why isn't frame.getcontentpane().setbackground(color.yellow);
working?
i've tried rearrange order, setting color after making frame visible.
it works alright, cannot see background color because flappybird
instance drawn on top of it. can verify replacing game class empty canvas so:
public static void main(string[] args) throws interruptedexception { jframe frame = new jframe("flappy bird"); frame.setsize(1200, 800); //flappybird game = new flappybird(); canvas game = new canvas(); frame.getcontentpane().setbackground(color.yellow); frame.add(game); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setresizable(false); // while (true) { // game.moveball(); // game.gameover(); // game.moverect(); // game.repaint(); // thread.sleep(14); // } }
there 2 things can try:
- setting background color not of frame's content pane of
game
:
//frame.getcontentpane().setbackground(color.yellow); game.setbackground(color.yellow);
- making sure frame's background color shows through game instance making latter transparent:
game.setopaque(false);
Comments
Post a Comment