2011-10-26 2 views
1

私のGUIを更新するのにSwingWorkerを使用しようとしています。SwingWorker process()gui Genericsを更新します。

私が更新しようとしている部分は、GridLayout [50] [50]を持つJPanel(gridPanel)です。
GridLayoutの各グリッドには、カスタムのGridGraphic JComponentがあります。

私のSwingWorkerのdoInBackground()では、色を表す各GridGraphicを更新します。次に、process()がguiを更新するために使用するリストに公開します。しかし、guiは更新していません。

repaint()を呼び出さずにこれを行う方法はありますか?

guiが応答するようにSwingWorkerを修正する方法を教えてください。変更に応答するGridGraphicコンポーネントの[50] [50] GridLayoutであるgridPanelを返すものは何ですか?

SwingWorkerはstepButton.addActionListener(new ActionListener()...)で実行されます。 ........... SlimeGuiで

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import javax.swing.event.*; 


public class SlimeGui extends JFrame{ 

    private JPanel buttonPanel, populationPanel, velocityPanel; 
    private JPanel gridPanel = new JPanel(new GridLayout(50, 50)); 
    private JButton setupButton, stepButton, goButton; 
    private JLabel populationNameLabel, velocityNameLabel, populationSliderValueLabel, velocitySliderValueLabel; 
    private JSlider populationSlider, velocitySlider; 
    private GridGraphic [] [] gridGraphic; 
    private GridGraphic test; 
    private int agents = 125; 
    private int velocity = 500; 
    private boolean resetGrid; 


    public SlimeGui() { 

     setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 


     //Set up JButtons 
     buttonPanel = new JPanel(); 
     setupButton = new JButton("Setup"); 
     stepButton = new JButton("Step"); 
     goButton = new JButton("Go"); 

     buttonPanel.add(setupButton); 
     buttonPanel.add(stepButton); 
     buttonPanel.add(goButton); 

     c.gridx = 0; 
     c.gridy = 1; 
     c.gridwidth = 3; 
     add(buttonPanel, c); 


     //Set up population JSlider  
     populationPanel = new JPanel(); 
     populationNameLabel = new JLabel("  Population"); 
     populationSliderValueLabel = new JLabel(Integer.toString(agents)); 
     populationSlider = new JSlider(JSlider.HORIZONTAL,0, 1000, 125); 
     populationSlider.setMajorTickSpacing(125); 
     populationSlider.setPaintTicks(true); 
     populationSlider.addChangeListener(new PopulationSliderListener()); 

     populationPanel.add(populationNameLabel); 
     populationPanel.add(populationSlider); 
     populationPanel.add(populationSliderValueLabel); 

     c.gridx = 0; 
     c.gridy = 2; 
     add(populationPanel, c);  


     //Set up veolicty JSlider 
     velocityPanel = new JPanel(); 
     velocityNameLabel = new JLabel("  Velocity"); 
     velocitySliderValueLabel = new JLabel(Integer.toString(velocity)); 
     velocitySlider = new JSlider(JSlider.HORIZONTAL,0, 1000, 500); 
     velocitySlider.setMajorTickSpacing(125); 
     velocitySlider.setPaintTicks(true); 
     velocitySlider.addChangeListener(new VelocitySliderListener()); 

     velocityPanel.add(velocityNameLabel); 
     velocityPanel.add(velocitySlider); 
     velocityPanel.add(velocitySliderValueLabel); 

     c.gridx = 0; 
     c.gridy = 3; 
     add(velocityPanel, c); 


     //Set up grid with GridGraphic objects 
     gridGraphic = new GridGraphic[50][50]; 

     for(int i = 0; i < 50; i++){ 
      for(int j = 0; j < 50; j++){ 
       gridGraphic[i][j] = new GridGraphic(); 
       gridPanel.add(gridGraphic[i][j]); 
      } 
     } 

     c.gridx = 0; 
     c.gridy = 0; 
     c.gridwidth = 3; 

     add(gridPanel, c); 


     //Set up ActionListener for the 'Setup' JButton 
     setupButton.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       int n1=0; 
       int n2=0; 

       //resets the grid so there are no agents 
       if(resetGrid){ 
        for(int i = 0; i < 50; i++){ 
         for(int j = 0; j < 50; j++){ 
          gridGraphic[i][j].setDefault(); 
         } 
        } 
       } 

       //sets a random number of positions for GridGraphics 
       for (int numOfAgenets = 0; numOfAgenets < agents; numOfAgenets++){ 
        int lowerB = 0; 
        int upperB = 50;      

        n1 = (lowerB + (int)(Math.random()*(upperB-lowerB))); //random number 1 
        n2 = (lowerB + (int)(Math.random()*(upperB-lowerB))); //random number 2 

        System.out.println("Choosing random agent "+(numOfAgenets+1)+": "+n1 +" "+n2); 

        //sets the GridGraphic to an agent if it's available 
        if (gridGraphic[n1][n2].getIntensity() == 0) 
         gridGraphic[n1][n2].setAgent(); 

        //if the GridGraphic is already an agent, it continues to search 
        else if(gridGraphic[n1][n2].getIntensity() == 5){ 
         while(gridGraphic[n1][n2].getIntensity() == 5){ 
          n1 = (lowerB + (int)(Math.random()*(upperB-lowerB))); 
          n2 = (lowerB + (int)(Math.random()*(upperB-lowerB))); 
         } 
         gridGraphic[n1][n2].setAgent(); 
        }     
       } 

       repaint(); 
       resetGrid = true; 
      } 
     }); 


     //Set up ActionListener for the 'Step' JButton 
     stepButton.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       StepManager step = new StepManager(SlimeGui.this); 
       step.execute(); 
       //repaint(); 
      } 
     }); 

    } 


    class PopulationSliderListener implements ChangeListener{ 
     public void stateChanged(ChangeEvent e){ 
      agents = ((JSlider)e.getSource()).getValue(); 

      populationSliderValueLabel.setText(Integer.toString(agents)); 
      System.out.println("Population of agents: " + agents); 
     } 
    } 


    class VelocitySliderListener implements ChangeListener{ 
     public void stateChanged(ChangeEvent e){ 
      velocity = ((JSlider)e.getSource()).getValue(); 

      velocitySliderValueLabel.setText(Integer.toString(velocity)); 
      System.out.println("Velocity(ms) of agents: " + velocity); 
     } 
    } 


    public Integer getVelocity(){ 
     return velocity; 
    } 

    public GridGraphic getGridGraphic(int n1, int n2){ 
     return gridGraphic[n1][n2]; 
    } 

    public GridGraphic [][] getGridGraphic(){ 
     return gridGraphic; 
    } 

    public void setGridGraphicArray(GridGraphic xxx, int n1, int n2){ 
     gridGraphic[n1][n2] = xxx; 
    } 

    public void setGridPanel(GridGraphic[][] xxx){ 
     for(int i = 0; i < 50; i++){ 
      for(int j = 0; j < 50; j++){ 
       gridPanel.add(xxx[i][j]); 
      } 
     } 
    } 



    /** 
    * Create the GUI and show it. For thread safety, 
    * this method should be invoked from the 
    * event-dispatching thread. 
    */ 
    private static void createAndShowGUI() { 
     //Create and set up the window. 
     SlimeGui slime = new SlimeGui(); 
     slime.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Display the window. 
     slime.pack(); 
     slime.setVisible(true); 
     slime.setResizable(false); 
    } 

    public static void main(String[] args) { 
     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

マイSwingWorkerの

import java.util.List; 
import java.awt.*; 
import javax.swing.*; 
import java.util.concurrent.ExecutionException; 

public class StepManager extends SwingWorker<Void, GridGraphic>{ 

    private SlimeGui gui; 

    public StepManager(SlimeGui sg){ 
     gui=sg; 
    } 

    @Override 
    protected Void doInBackground() throws Exception{ 
     for(int i = 0; i < 50; i++){ 
      for(int j = 0; j < 50; j++){ 

       if(gui.getGridGraphic(i,j).getIntensity()==5){ 
        if (i==0){ 
         gui.getGridGraphic(i,j).setDefault(); 
         gui.getGridGraphic(49,j).setAgent(); 
        } 
        else{ 
         gui.getGridGraphic(i,j).setDefault(); 
         gui.getGridGraphic(i-1,j).setAgent(); 
        } 
       } 
       publish(gui.getGridGraphic(i,j)); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void process(List <GridGraphic> gg){     
     int k=0; 
     for (int i = 0; i < 50; i++){ 
      for(int j = 0; j < 50; j++){ 
       gui.setGridGraphicArray(gg.get(k),i,j); 
       k++; 
      } 
     } 
     gui.setGridPanel(gui.getGridGraphicArray()); 
     System.out.println("process has completed"); 
    } 

    @Override 
    protected void done(){   
     System.out.println("doInBackground has completed"); 
    } 
} 

マイGridGraphic

答えて

3

スタックトレースの行番号は、例外が発生する場所を示します。 StepManagergui属性がnullです。初期化されていません。

+0

他の属性としてコンストラクタに渡すことによって:StepManager step = new StepManager(gridGraphic、gridPanel、SlimeGui.this); –

+0

SwingWorkerはまだGUIを更新していませんが、ランタイムエラーを修正しました。私はdoInBackground()が各GridGraphicコンポーネントを更新し、process()がguiを更新するために使用するリストにそれを公開すると考えました。 SwingWorkerがこれらのメソッドを正しく使用する方法と、GUIが応答しない理由を理解していますか? – blitzeus

関連する問題