2017-04-05 14 views
-1

JPanelに問題があり、何が起こっているのかわかりません。ですから、私はGamePanelと呼ばれるカスタムJPanelを作成するinit関数を持つJFrameを持っています。そして、奇妙なことは、オブジェクト上で再描画を使用しても、paintComponents関数には決して行きません。ここでJava Swing JPanel paintComponentsが呼び出されなかった

私は(JFrameの中で)のJPanelを初期化するときに私のコードです:

this.gamePanel = new GamePanel(this.grid, this); 
this.panel.add(this.gamePanel, constraints); 

とJPanelの自身:

public class GamePanel extends JPanel { 

    private final int SQUARE_SIZE = 50; 

    private Grid grid; 
    private final GameView gameView; 

    public GamePanel(Grid grid, GameView gameView) { 
     this.gameView = gameView; 

     this.setPreferredSize(new Dimension(200, 200)); 
    } 

    public void setGrid(Grid grid) { 
     this.grid = grid; 
     this.setPreferredSize(new Dimension(grid.getSizeX() * SQUARE_SIZE, grid.getSizeY() * SQUARE_SIZE)); 
    } 

    @Override 
    public void paintComponents(Graphics g) { 
     System.out.println("test"); 

     if (this.grid != null) { 

      Graphics2D g2 = (Graphics2D) g; 

      double thickness = 3; 
      g2.setStroke(new BasicStroke((float) thickness)); 

      g2.setColor(Color.BLACK); 

      for (int i = 0; i < 3; i++) { 
       for (int j = 0; j < 3; j++) { 
        int x = SQUARE_SIZE * i; 
        int y = SQUARE_SIZE * j; 

        g2.drawRect(x, y, SQUARE_SIZE, SQUARE_SIZE); 

        if(this.grid.getSquareState(x, y) != 0) { 
         char[] tmp = ("" + this.grid.getSquareState(x, y)).toCharArray(); 
         g2.drawChars(tmp, 0, 1, x, y); 
        } 
       } 
      } 
     } 
    } 
} 

EDIT:(全体のJFrame)

public class GameView extends JFrame { 

    private CustomSocket socket; 
    private JPanel panel; 
    private GamePanel gamePanel; 
    private JLabel listPlayers; 
    private JLabel playerPlaying; 
    private Grid grid; 

    public GameView(CustomSocket socket) { 
     this.socket = socket; 
     this.setTitle("TicTacToe - Client"); 

     this.setSize(600, 480); 

     this.setLocationRelativeTo(null); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     this.init(); 
     this.pack(); 
     this.setVisible(true); 
     this.play(); 
    } 

    private void init() { 
     this.panel = new JPanel(); 
     this.panel.setLayout(new GridBagLayout()); 

     GridBagConstraints constraints =new GridBagConstraints(); 
     constraints.fill = GridBagConstraints.CENTER; 

     constraints.gridx = 0; 
     constraints.gridy = 0; 
     constraints.gridwidth = 1; 

     // Grid 
     this.gamePanel = new GamePanel(this.grid, this); 
     this.panel.add(this.gamePanel, constraints); 

     // Labels 
     constraints.gridy += 1; 
     this.listPlayers = new JLabel(); 
     this.panel.add(this.listPlayers, constraints); 

     constraints.gridy += 1; 
     this.playerPlaying = new JLabel(); 
     this.panel.add(this.playerPlaying, constraints); 

     this.setContentPane(this.panel); 
    } 

    private void play() { 
     String[] tmp = this.socket.getData().split(";"); 

     this.grid = new Grid(Integer.parseInt(tmp[0]), Integer.parseInt(tmp[1])); 

     String players = ""; 
     for(int i = 2; i < tmp.length; i++) { 
      players += tmp[i] + " "; 
     } 

     this.listPlayers.setText(players); 

     boolean notFinished = true; 
     while(notFinished) { 
      String[] gridData = this.socket.getData().split(";"); 
      for(int i = 1; i < gridData.length; i++) { 
       String[] gridRow = gridData[i].replace("(", "").replace(")", "").split(","); 


       for(int j = 0; j < gridRow.length; j++) { 
        this.grid.setSquareState(i - 1, j, Integer.parseInt(gridRow[j])); 
       } 
      } 

      this.gamePanel.repaint(); 

      String playerPlaying = this.socket.getData().split(";")[0]; 

      if(playerPlaying != this.socket.getUsername()) { 
      } 
      notFinished = true; 
     } 
    } 
} 

ありがとうございます。

+2

paintComponentsではなくpaintComponentを使用 – ControlAltDel

+0

私は両方の機能を試してみましたが残念ながらそれは動作しません –

+0

なぜあなたの[7質問](http://stackoverflow.com/users/6743005/florian-merle) **ただ一つ**は受け入れられた答えを持っていますか?あなたのために働いていないのですか? –

答えて

2
this.panel.add(this.gamePanel, constraints); 

パネルにコンポーネントを追加しますが、パネルには推奨サイズがありません。そのサイズは(0、0)なので、ペイントすることはないので、メソッドは呼び出されません。

すべてのSwingコンポーネントは、独自の推奨サイズを決定します。カスタムコンポーネントのgetPreferredSize()メソッドをオーバーライドします。レイアウトマネージャは、コンポーネントの適切なサイズ/位置を設定できます。

paintComponent(...)は、オーバーライドする適切な方法であり、背景がクリアされることを確認する最初のステートメントとしてsuper.paintComponent(...)を忘れないようにしてください。

+0

さて、getPreferredSize()メソッドを追加しても問題は解決しませんでしたが、ディメンションを変更するとウィンドウが大きくなります。 –

+0

編集:元の投稿のJFrameからコード全体を追加しました –

+0

@FlorianMerleあなたは私の最後のポイントを逃しました。また、あなたのソケットが問題を引き起こしているかどうかを知っている人。おそらく、コードが入力待ちをブロックしている可能性があります。ロジックフローが正しいかどうかを判断するには、さらにデバッグを行います。 – camickr

関連する問題