2016-05-23 9 views
3

私は、Cell:Inside Cellと呼ばれるJLabelを拡張するクラスが2つのメソッド、kill()revive()です。セルが復活すると、setBackground()を使って色が変わります。私はというJPanelを持っていて、これらのCellの行列を含んでいます。ランダムに塗りつぶされたJLabels

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.*; 
import javax.swing.*; 

public class LifeDriver { 
    public static void main(String[] args) { 
     System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"); 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() 
      { 
       JFrame frame = new JFrame("Life"); 
       frame.setSize(950, 800); 
       frame.setLocationRelativeTo(null); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setContentPane(new MainPanel(80, 100)); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

class MainPanel extends JPanel { 
    private LifePanel p; 
    private ControlPanel c; 
    private JScrollPane pane; 

    public MainPanel(int ro, int co) { 
     setLayout(new BorderLayout()); 
     p = new LifePanel(ro, co); 
     p.setPreferredSize(new Dimension(740, 740)); 
     pane = new JScrollPane(p); 
     pane.setBorder(null); 
     pane.setAutoscrolls(true); 
     add(pane, BorderLayout.CENTER); 
     c = new ControlPanel(p); 
     add(c, BorderLayout.NORTH); 
    } 
} 

class ControlPanel extends JPanel { 
    private JButton random; 
    private LifePanel lp; 

    public ControlPanel(LifePanel p) { 
     lp = p; 
     random = new JButton("Random"); 
     random.addActionListener(new Listener()); 
     add(random); 
    } 

    private class Listener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       double prob = Double.parseDouble(JOptionPane 
         .showInputDialog("Percentage Probability?")); 
       lp.randomize(prob); 
      } catch (Exception f) { 
      } 
     } 
    } 
} 

class LifePanel extends JPanel { 
    private Cell[][] grid; 
    private Timer t; 
    private static int nR, nC; 

    public LifePanel(int row, int column) { 
     nR = row; 
     nC = column; 
     setLayout(new GridLayout(nR, nC, 1, 1)); 
     grid = new Cell[nR][nC]; 
     for (int r = 0; r < nR; r++) 
      for (int c = 0; c < nC; c++) { 
       grid[r][c] = new Cell(); 
       add(grid[r][c]); 
      } 
    } 

    public void randomize(double percent) { 
     for (int r = 0; r < nR; r++) 
      for (int c = 0; c < nC; c++) 
       if (Math.random() < percent/100) 
        grid[r][c].revive(); 
       else 
        grid[r][c].kill(false); 
    } 
} 

class Cell extends JLabel { 
    private Color deadCellColor = new Color(245, 245, 245); 

    public Cell() { 
     this.setBackground(deadCellColor); 
     this.setOpaque(true); 
     this.setAlive(false); 
    } 

    public void kill(boolean removeCell) { 
     this.setAlive(false); 
    } 

    public void revive() { 
     this.setAlive(true); 
    } 

    public void setAlive(boolean arg) { 
     if (arg) { 
      this.changeColor(); 
     } else { 
      this.setBackground(deadCellColor); 
     } 
    } 

    private void changeColor() { 
     setBackground(Color.RED); 
    } 
} 

私はそれが期待する方法、それは(ランダムに各JLabelのの色を設定)作品、ランダムではなく、呼び出します。一度にすべてを表示するのではなく、各ラベルの色がランダムに表示されます(パワーポイントのディゾルブ効果に似ていますか?)。 forのループが示唆するように、色は行ごと、列ごとに表示されません。注文はあらかじめ決められていても完全にランダムに見えます。なぜこれをやっているの?

+1

作成し、[MCVE]投稿します。この赤ちゃんを自分でテストしましょう! –

+0

作業中...ほぼ完了 – RobotKarel314

+0

更新済み!私は、私が必要としなかったものを全て取り除いた。 – RobotKarel314

答えて

3

セルのステータスを変更している間に更新を避けるには、親パネルLifePanelの表示を変更するだけです。そのような何か:

public void randomize(double percent) { 
    // No updates while we change state 
    setVisible(false); 
    for (int r = 0; r < nR; r++) { 
     for (int c = 0; c < nC; c++) { 
      if (Math.random() < percent/100) 
       grid[r][c].revive(); 
      else 
       grid[r][c].kill(false); 
     } 
    } 
    // Update now 
    setVisible(true); 
} 

私はどんな汚い長方形(すなわち、画面に再描画必要なもの)を追跡されることが期待されるかもしれない方法で、実際の再描画の更新を注文していないと思われます。これは、すべてのセルを更新した後LifePanelの再描画を要求しても、期待されるように動作するように見える、ケースのようです:

public void randomize(double percent) { 
    for (int r = 0; r < nR; r++) { 
     for (int c = 0; c < nC; c++) { 
      if (Math.random() < percent/100) 
       grid[r][c].revive(); 
      else 
       grid[r][c].kill(false); 
     } 
    } 
    // Force redraw of whole panel 
    this.repaint(); 
} 
+0

両方のソリューションが機能しました!どうもありがとうございます! – RobotKarel314

関連する問題