Мой графический интерфейс - это GridLayout 50x50, который обновляется с помощью SwingWorker.
Каждая сетка в GridLayout имеет компонент GridGraphic с определенной интенсивностью.
Интенсивность по умолчанию = 0, то есть просто черный компонент,Если интенсивность = 5, то сетка выглядит черной с желтой точкой.
Когда нажата кнопка Step, StepManager должен пройти через все сетки и обновить их интенсивность, а затем перерисовать все сразу, ноStepManager прекращает выполнение после первого GridGraphic, который имеет значение с интенсивностью = 5.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Gui extends JFrame{
private JPanel buttonPanel, populationPanel, velocityPanel, gridPanel;
private JButton setupButton, stepButton, goButton;
private JLabel populationNameLabel, velocityNameLabel, populationSliderValueLabel, velocitySliderValueLabel;
private JSlider populationSlider, velocitySlider;
private GridGraphic [] [] gridGraphic;
private int agents = 125;
private int velocity = 500;
private boolean resetGrid;
private StepManager step;
public Gui() {
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);
public GridGraphic getGridGraphic(int n1, int n2){
return gridGraphic[n1][n2];
}
/**
* 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.
Gui gui = new Gui();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
gui.pack();
gui.setVisible(true);
gui.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();
}
});
}
}