0

I'm creating program which has JFrame, welcome state JPanel, and game JPanel. This is short method, that switch JPanels in JFrame, from welcomeState JPanel, to game JPanel.

public static void startGameState(){
    if (!Preferences.gameView) return;
    /*
     * Remove menu state, and set game state
     */
    gameState = new GameBoard();


    frame.setContentPane(gameState);
    frame.invalidate();
    frame.validate();
    /*
     * Run Server Connection Thread
     */
    connectionHandler.start();
    /*
     * Run Game engine
     * and go to Game view
     */
    gameState.start();
}

Everything works fine. My Problem starts here. I have changed GameBoard class. It is no longer extending JPanel class. Now it is extending Canvas. My question is - how to remove first JPanel, and add Canvas GameBoard class instead when Im switching program states?

Calum
  • 1,889
  • 2
  • 18
  • 36
G.Spansky
  • 852
  • 6
  • 17
  • 32
  • 3
    Use a [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) to swap views. Side issue 1: Why does GameBoard extend Canvas and not the more appropriate JPanel? Side issue 2: Your code looks to be doing too much within the static world, why is this? – Hovercraft Full Of Eels Sep 26 '15 at 21:54
  • Because I want to set BufferedStrategy instead of overwriting JPanel's paintComponent method. – G.Spansky Sep 26 '15 at 21:57
  • I guess that's fair (I am not familiar with use of BufferedStrategy), but my recommendation to use a CardLayout to swap views remains. There are many similar questions on this site including [this one](http://stackoverflow.com/questions/10694584/whats-so-special-about-cardlayout-vs-manual-adding-removal-of-jpanels) as well as [this one](http://stackoverflow.com/questions/23314005/switch-two-panels-in-on), both answered by me. – Hovercraft Full Of Eels Sep 26 '15 at 22:00
  • Ok, Thank You, I will check thoose liks :) – G.Spansky Sep 26 '15 at 22:10

0 Answers0