Game of life not resetting properly - java

I'm trying to make my version of game of life reset when a reset button is clicked but I have an issue.
After clicking the button, everything successfully reset but not the main Jpanel where we see the generations moving.
I have two JLabel, one showing the number of the current generation and the other showing the amount of alive cells in that generation. They are both reseted successfully but the main JPanel just freezes and I cannot see the animation anymore.
GameOfLife class:
public class GameOfLife extends JFrame implements ActionListener {
private static class GameStep extends TimerTask {
static GameOfLife life = new GameOfLife();
#Override
public void run() {
updateLabels();
}
}
static JLabel aliveLabel = new JLabel("Alive:");
static JLabel GenerationLabel = new JLabel("Generation #");
static CellGrid body = new CellGrid();
static JPanel header = new JPanel();
static int genNumber = 1;
static JButton PlayToggleButton = new JButton("pause");
static JButton ResetButton = new JButton("reset");
static Boolean isPaused = false;
static GameStep game = new GameStep();
static Timer timer = new Timer();
public GameOfLife() {
super("Game of life");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700, 660);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
GenerationLabel.setName("GenerationLabel");
aliveLabel.setName("aliveLabel");
PlayToggleButton.setName("PlayToggleButton");
ResetButton.setName("ResetButton");
PlayToggleButton.addActionListener(this);
ResetButton.addActionListener(this);
PlayToggleButton.setIcon(new ImageIcon(play));
ResetButton.setIcon(new ImageIcon(reset));
PlayToggleButton.setPreferredSize(new Dimension(40,30));
ResetButton.setPreferredSize(new Dimension(40,30));
header.setLayout(new FlowLayout());
header.setPreferredSize(new Dimension(100, this.getHeight()));
header.add(PlayToggleButton);
header.add(ResetButton);
header.add(GenerationLabel);
header.add(aliveLabel);
body.setLayout(new BorderLayout());
body.setPreferredSize(new Dimension(500, this.getHeight()));
add(header, BorderLayout.WEST);
add(body, BorderLayout.CENTER);
setVisible(true);
}
public static void updateLabels(){
body.run();
GenerationLabel.setText("Generation #"+ genNumber++);
aliveLabel.setText("Alive: "+ body.totalAlive());
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("pause")){
pauseResume();
}
else if(e.getActionCommand().equals("reset")){
reset();
}
}
static void loopStep(){
timer.schedule(game, 0,1000);
}
static void pauseResume() {
if(!isPaused){
isPaused = true;
timer.cancel();
}
else{
isPaused = false;
timer = new Timer();
timer.schedule(new GameStep(), 0,1000);
}
}
static void reset() {
timer.cancel();
isPaused = false;
genNumber = 1;
header = new JPanel();
body = new CellGrid();
body.repaint();
timer = new Timer();
timer.schedule(new GameStep(), 0,1000);
}
public static void main(String[] args) {
loopStep();
}
}
CellGrid class:
public class CellGrid extends JPanel implements Runnable{
private static final int ROWS = 60;
private static final int COLS = 60;
private static final int CELL_WIDTH = 10;
private static Cell[][] cellGrid = new Cell[ROWS][COLS];
public CellGrid() {
for (int row = 0; row < cellGrid.length; row++) {
for (int col = 0; col < cellGrid[row].length; col++) {
int x = col * CELL_WIDTH;
int y = row * CELL_WIDTH;
cellGrid[row][col] = new Cell(x, y, CELL_WIDTH);
if (new Random().nextBoolean()) {
cellGrid[row][col].setAlive(true);
} else {
cellGrid[row][col].setAlive(false);
}
}
}
}
public int totalAlive(){
int totalAlive = 0;
for (Cell[] cells : cellGrid) {
for (int j = 0; j < cellGrid.length; j++) {
if (cells[j].isAlive())
totalAlive++;
}
}
return totalAlive;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Cell[] cellRow : cellGrid) {
for (Cell cell : cellRow) {
cell.draw(g2);
}
}
}
#Override
public void run() {
cellGrid = new GenerationMaker4().nextGeneration(cellGrid);
repaint();
}
}
any idea why it's happening ?

Your reset() method:
static void reset() {
timer.cancel();
isPaused = false;
genNumber = 1;
header = new JPanel();
body = new CellGrid();
body.repaint();
timer = new Timer();
timer.schedule(new GameStep(), 0,1000);
}
The problem is a common newbie mistake -- you think that changing a variable reference will change the prior object that the variable originally referenced.
Specifically, you have body = new CellGrid(); and what this does is have the body variable refer to a new CellGrid object, but (and here is the important part), it *does nothing to the CellGrid object that is currently displayed in your GUI, that the body variable previously referred to.
Several alternative solutions:
Add the new CellGrid object now referred to in the body variable to the GUI in the same BorderLayout position, covering the previous one
Better still is to not create a new CellGrid object but instead to create a way to set the current CellGrid back to its initial state.
For example, perhaps if you changed CellGrid to...
public class CellGrid extends JPanel implements Runnable{
private static final int ROWS = 60;
private static final int COLS = 60;
private static final int CELL_WIDTH = 10;
private Cell[][] cellGrid = new Cell[ROWS][COLS]; // make this non-static
public CellGrid() {
reset();
}
public void reset() {
cellGrid = new Cell[ROWS][COLS];
for (int row = 0; row < cellGrid.length; row++) {
for (int col = 0; col < cellGrid[row].length; col++) {
int x = col * CELL_WIDTH;
int y = row * CELL_WIDTH;
cellGrid[row][col] = new Cell(x, y, CELL_WIDTH);
if (new Random().nextBoolean()) {
cellGrid[row][col].setAlive(true);
} else {
cellGrid[row][col].setAlive(false);
}
}
}
}
// ..... more code below
Then all you need to do is to call reset() on the current CellGrid object, and then call repaint().
Other issues:
You grossly over-use the static modifier. Nothing in this program should be static, other than the main method, your constants, and that's it. This may not be important for this small program, but it will become important later when you try to do unit testing or extend or enhance this program, or add it to another larger program.
You use java.util.Timer and java.util.TimerTask to run an animation loop in a Swing GUI program, and this is not safe to do, since these classes are not Swing thread-safe. Much better to use a javax.swing.Timer or "Swing Timer" in place of both of these classes to run the animation since this is thread-safe for this GUI library.

Related

Java repaint() not working when called from a different class

My repaint works when called in the same class but not from another class. I haven't been able to find this issue elsewhere. I have put my code below. Thank you! The code is for making a calculator in a JFrame with 2 JPanels, one showing the user's input and one with all the buttons. I want to call repaint so the drawString() method changes as the user enters their input.
public class Calculator
{
public static void main(String[] args)
{
Calculator c = new Calculator();
}
public Calculator()
{
JFrame frame = new JFrame("Calculator");
frame.setSize(800, 800);
frame.setResizable(false);
Buttons b = new Buttons();
Display d = new Display();
frame.setLayout(new GridLayout(2, 1));
frame.add(d);
frame.add(b);
frame.setVisible(true);
}
public class Buttons extends JPanel implements ActionListener
{
private int z;
public JButton[] buttons;
public Display d;`enter code here`
public String[] values;
public String clickedButton;
public Buttons()
{
setBackground(Color.BLACK);
setLayout(new GridLayout(5, 4));
values = new String[100];
for(int i = 0; i < values.length; i++)
{
values[i] = new String("");
}
addButtons();
}
public void addButtons()
{
Font courier = new Font("Courier", Font.BOLD, 20);
buttons = new JButton[20];
for(int i = 0; i < buttons.length; i++)
{
buttons[i] = new JButton(Integer.toString(i));
buttons[i].setBackground(Color.BLUE);
buttons[i].setForeground(Color.WHITE);
buttons[i].setFont(courier);
buttons[i].setFocusable(false);
buttons[i].addActionListener(this);
buttons[i].setBorder(BorderFactory.createLineBorder(new Color(0, 100, 175, 255)));
add(buttons[i]);
}
buttons[10].setVisible(false);
buttons[10].setEnabled(false);
buttons[11].setVisible(false);
buttons[11].setEnabled(false);
buttons[12].setText("C");
buttons[13].setText("+");
buttons[14].setText("-");
buttons[15].setText("*");
buttons[16].setText("/");
buttons[17].setText("+/-");
buttons[18].setText("^");
buttons[19].setText("=");
}
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
d = new Display();
for(int i = 0; i < 10; i++)
{
if(action.equals(Integer.toString(i)))
{
values[d.i]+=Integer.toString(i);
System.out.println("should be repainting");
d.repaint();
}
}
}
}
public class Display extends JPanel
{
public Buttons b;
public Font courier;
public int i;
public Display()
{
i = 0;
b = new Buttons();
setBackground(Color.BLACK);
courier = new Font("Courier", Font.BOLD, 50);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLUE);
g.setFont(courier);
g.drawString(b.values[i], 50, 50);
repaint();
}
}
}
You seem to be creating a new Display every time and telling that to repaint instead of the real one. Move your Display d variable into the Calculator class as a field, and don't declare new ones.
You create the original Display object as a local variable so it can't be accessed from elsewhere, so make this part use the class field instead:
Display d = new Display();
Also, this line in actionPerformed is creating a new instance and should be removed:
d = new Display();
Don't call repaint from within a paint method. This will cause the API to schedule anther paint request, which will eventually consume all your CPU cycles
You actually have two instances of Display, one which is on the screen, one which is not
While there are a few ways to fix this, one of the simpler would be to simply pass the instance of Display created to in Calculators constructor to Buttons, for example...
public class Calculator {
public static void main(String[] args) {
Calculator c = new Calculator();
}
public Calculator() {
JFrame frame = new JFrame("Calculator");
frame.setSize(800, 800);
frame.setResizable(false);
Display d = new Display();
Buttons b = new Buttons(d);
frame.setLayout(new GridLayout(2, 1));
frame.add(d);
frame.add(b);
frame.setVisible(true);
}
public class Buttons extends JPanel implements ActionListener {
private int z;
private JButton[] buttons;
private String[] values;
private String clickedButton;
private Display d;
public Buttons(Display d) {
this.d = d;
setBackground(Color.BLACK);
setLayout(new GridLayout(5, 4));
values = new String[100];
for (int i = 0; i < values.length; i++) {
values[i] = new String("");
}
addButtons();
}
Then buttons can use that instance to display what ever it needs to display
private int index = 0;
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
for (int i = 0; i < 10; i++) {
if (action.equals(Integer.toString(i))) {
values[index] += Integer.toString(i);
d.setValue(values[index]);
index++;
}
}
}
}
public class Display extends JPanel {
public Font courier;
private String value;
public Display() {
setBackground(Color.BLACK);
courier = new Font("Courier", Font.BOLD, 50);
}
public void setValue(String value) {
this.value = value;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.setFont(courier);
if (value != null) {
g.drawString(value, 50, 50);
}
}
}
}

Multithread timer not working correctly

Again, please help me!
In the following code, I want to get started by pushing a timer thread at the push of a button and typing it in a Label. Each press of the button should start a new thread and mark it on each label. But unfortunately, the same timer is written for each label. Can you help us get it right? If you can tell me the mistake, what do I mean?
public class TimerThreads implements ActionListener{
JFrame jFrame = new JFrame();
JLabel[] labels;
int second = 0;
int minute = 0;
String s = "";
String m = "";
int l = 0;
public TimerThreads(){
JLabel one = new JLabel();
JLabel two = new JLabel();
JLabel three = new JLabel();
JLabel four = new JLabel();
labels = new JLabel[]{one, two, three, four};
jFrame.setLayout(new GridLayout(0, 2, 5, 5));
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JButton start = new JButton("Start");
start.addActionListener(this);
JButton stop = new JButton("Stop");
stop.addActionListener(this);
jFrame.add(start);
jFrame.add(stop);
jFrame.setVisible(true);
jFrame.pack();
}
public static void main(String[] args) {
new TimerThreads();
}
#Override
public void actionPerformed(ActionEvent e) {
String select = e.getActionCommand();
switch(select){
case "Start":
jFrame.add(labels[l]);
jFrame.revalidate();
jFrame.repaint();
TimerThread t = new TimerThread(labels[l]);
t.start();
l++;
break;
case "Stop":
//
break;
}
}
class TimerThread extends Thread{
JLabel jLabel;
public TimerThread(JLabel jLabel) {
this.jLabel = jLabel;
}
#Override
public void run() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
second++;
if(String.valueOf(second).length() == 1){
s = "0";
}
else{
s = "";
}
if(second == 60){
second = 0;
s = "0";
minute++;
}
if(String.valueOf(minute).length() == 1){
m = "0";
}
jLabel.setText(m + String.valueOf(minute) + ":" + s + String.valueOf(second));
}
},0, 1000);
}
}
}
The cause of your bug is here:
public class TimerThreads implements ActionListener {
JFrame jFrame = new JFrame();
JLabel[] labels;
// ***** these fields below
int second = 0;
int minute = 0;
String s = "";
String m = "";
// ***** these fields above
int l = 0;
The four fields are instance fields of the class and are shared by every TimerTask instance that you create, and so all will show the same exact time.
The solution is to make these fields local to the nested class:
public class TimerThreads implements ActionListener {
JFrame jFrame = new JFrame();
JLabel[] labels;
// int second = 0;
// int minute = 0;
// String s = "";
// String m = "";
int l = 0;
public TimerThreads() {
//.....
}
// ....
class TimerThread extends Thread {
JLabel jLabel;
public TimerThread(JLabel jLabel) {
this.jLabel = jLabel;
}
#Override
public void run() {
java.util.Timer timer = new java.util.Timer();
timer.scheduleAtFixedRate(new TimerTask() {
// ***** add these fields here
int second = 0;
int minute = 0;
String s = "";
String m = "";
Having said this, you've got risky code since you're using the wrong Timer, a java.util.Timer, when you should be using a Swing Timer or javax.swing.Timer. This is very important since the latter Timer works well with the Swing event model and prevents thread clashes. Please check out the Swing Timer Tutorial
Other problems: using a fixed size array, you risk index out of bounds exception if the user wants more than 4 threads running. Use an ArrayList instead.

Java - adding JButton array to JFrame

I am trying to add a 2D JButton array to a JFrame, I don't get any errors, just the JButtons don't show up.
Creating the JButtons:
public class TTTGrid {
private static JFrame frame;
private static int[][] coords;
private static int width, height;
public TTTGrid(JFrame frame,int[][] coords, int width, int height){
this.frame = frame;
this.coords = coords;
this.width = width;
this.height = height;
}
static JButton map[][] = new JButton[3][3];
public void Draw(){
for(int i = 0; i<coords.length; i++){
for(int j = 0; j<coords[i].length; j++){
map[i][j] = new JButton();
map[i][j].setBounds(i*100, j*100, width, height);
frame.add(map[i][j]);
}
}
}
}
Where the draw method is called:
public class TTTthread extends TTT implements Runnable {
int[][] map = new int[3][3];
TTTGrid grid = new TTTGrid(frame, map, 100, 100);
#Override
public void run() {
try {
while (true) {
grid.Draw();
Thread.sleep(20);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
If your code is running like I think it's running, you appear to be trying to add 9 JButtons to your GUI 50 times a second! That's a heck of a lot of buttons -- are you sure that this is what you want to be doing? Your code also runs far afoul of Swing threading rules by making Swing calls (a lot of Swing calls!) off of the Swing event thread.
Your main solution is likely to
Add your 9 JButtons to a JPanel that uses a GridLayout(3, 3)
Do this only once not 50 times a second
Then add that JPanel to your GUI, BorderLayout.CENTER and to be sure not to use null layouts.
Not try to set the bounds, size or locations of these JButtons, but rather to let the layout managers to the work
Get rid of that while loop and instead change your code to be more event-driven using Swing's event driven model.
Strive to use mostly non-static variables and methods so that your classes become true OOPS-compliant classes, allowing them to take advantage of the benefits of OOPS programming, including reducing program complexity and interconnectedness (reduce coupling).
For example
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class MyTttFoo extends JPanel {
// it's OK for constants to be static
private static final long serialVersionUID = 1L;
private static final int ROWS = 3;
// use a larger Font to make buttons larger
private static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 60);
private static final String BLANK = " ";
private static final String X = "X";
private static final String O = "O";
// but not most variables
private JButton[][] buttonGrid = new JButton[ROWS][ROWS];
public MyTttFoo() {
setBackground(Color.black);
// use layout managers to help you create your GUI
setLayout(new GridLayout(ROWS, ROWS, 1, 1));
ActionListener btnListener = new ButtonListener();
// create your buttons and add them only **once**
for (int row = 0; row < buttonGrid.length; row++) {
for (int col = 0; col < buttonGrid[row].length; col++) {
JButton button = new JButton(BLANK);
button.setFont(BTN_FONT);
button.addActionListener(btnListener);
add(button); // add button to a gridlayout using component
buttonGrid[row][col] = button; // and assign into the array
}
}
}
private class ButtonListener implements ActionListener {
private boolean xTurn = true;
#Override
public void actionPerformed(ActionEvent e) {
AbstractButton btn = (AbstractButton) e.getSource();
String txt = btn.getText();
if (txt.equals(BLANK)) {
if (xTurn) {
btn.setText(X);
} else {
btn.setText(O);
}
xTurn = !xTurn;
}
}
}
private static void createAndShowGui() {
MyTttFoo mainPanel = new MyTttFoo();
JFrame frame = new JFrame("MyTttFoo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

Fixing method latency in Java

Another problem, same program:
The following is MainGUI.java
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class MainGUI extends JFrame implements ActionListener
{
private static final long serialVersionUID = 4149825008429377286L;
private final double version = 8;
public static int rows;
public static int columns;
private int totalCells;
private MainCell[] cell;
public static Color userColor;
JTextField speed = new JTextField("250");
Timer timer = new Timer(250,this);
String generationText = "Generation: 0";
JLabel generationLabel = new JLabel(generationText);
int generation = 0;
public MainGUI(String title, int r, int c)
{
rows = r;
columns = c;
totalCells = r*c;
System.out.println(totalCells);
cell = new MainCell[totalCells];
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Timer set up
timer.setInitialDelay(Integer.parseInt(speed.getText()));
timer.setActionCommand("timer");
//set up menu bar
JMenuBar menuBar = new JMenuBar();
JMenu optionsMenu = new JMenu("Options");
JMenu aboutMenu = new JMenu("About");
menuBar.add(optionsMenu);
menuBar.add(aboutMenu);
JMenuItem helpButton = new JMenuItem("Help!");
helpButton.addActionListener(this);
helpButton.setActionCommand("help");
aboutMenu.add(helpButton);
JMenuItem aboutButton = new JMenuItem("About");
aboutButton.addActionListener(this);
aboutButton.setActionCommand("about");
aboutMenu.add(aboutButton);
JMenuItem colorSelect = new JMenuItem("Select a Custom Color");
colorSelect.addActionListener(this);
colorSelect.setActionCommand("colorSelect");
optionsMenu.add(colorSelect);
JMenuItem sizeChooser = new JMenuItem("Define a Custom Size");
sizeChooser.addActionListener(this);
sizeChooser.setActionCommand("sizeChooser");
optionsMenu.add(sizeChooser);
//Create text field to adjust speed and its label
JPanel speedContainer = new JPanel();
JLabel speedLabel = new JLabel("Enter the speed of a life cycle (in ms):");
speedContainer.add(speedLabel);
speedContainer.add(speed);
speedContainer.add(generationLabel);
Dimension speedDim = new Dimension(100,25);
speed.setPreferredSize(speedDim);
//Create various buttons
JPanel buttonContainer = new JPanel();
JButton randomizerButton = new JButton("Randomize");
randomizerButton.addActionListener(this);
randomizerButton.setActionCommand("randomize");
buttonContainer.add(randomizerButton);
JButton nextButton = new JButton("Next"); //forces a cycle to occur
nextButton.addActionListener(this);
nextButton.setActionCommand("check");
buttonContainer.add(nextButton);
JButton startButton = new JButton("Start");
startButton.addActionListener(this);
startButton.setActionCommand("start");
buttonContainer.add(startButton);
JButton stopButton = new JButton("Stop");
stopButton.addActionListener(this);
stopButton.setActionCommand("stop");
buttonContainer.add(stopButton);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(this);
clearButton.setActionCommand("clear");
buttonContainer.add(clearButton);
//holds the speed container and button container, keeps it neat
JPanel functionContainer = new JPanel();
BoxLayout functionLayout = new BoxLayout(functionContainer, BoxLayout.PAGE_AXIS);
functionContainer.setLayout(functionLayout);
functionContainer.add(speedContainer);
speedContainer.setAlignmentX(CENTER_ALIGNMENT);
functionContainer.add(buttonContainer);
buttonContainer.setAlignmentX(CENTER_ALIGNMENT);
//finish up with the cell container
GridLayout cellLayout = new GridLayout(rows,columns);
JPanel cellContainer = new JPanel(cellLayout);
cellContainer.setBackground(Color.black);
int posX = 0;
int posY = 0;
for(int i=0;i<totalCells;i++)
{
MainCell childCell = new MainCell();
cell[i] = childCell;
childCell.setName(String.valueOf(i));
childCell.setPosX(posX);
posX++;
childCell.setPosY(posY);
if(posX==columns)
{
posX = 0;
posY++;
}
cellContainer.add(childCell);
childCell.deactivate();
Graphics g = childCell.getGraphics();
childCell.paint(g);
}
//make a default color
userColor = Color.yellow;
//change icon
URL imgURL = getClass().getResource("images/gol.gif");
ImageIcon icon = new ImageIcon(imgURL);
System.out.println(icon);
setIconImage(icon.getImage());
//add it all up and pack
JPanel container = new JPanel();
BoxLayout containerLayout = new BoxLayout(container, BoxLayout.PAGE_AXIS);
container.setLayout(containerLayout);
container.add(cellContainer);
container.add(functionContainer);
add(menuBar);
setJMenuBar(menuBar);
add(container);
pack();
}
private void checkCells()
{
//perform check for every cell
for(int i=0;i<totalCells;i++)
{
cell[i].setNeighbors(checkNeighbors(i));
}
//use value from check to determine life
for(int i=0;i<totalCells;i++)
{
int neighbors = cell[i].getNeighbors();
if(cell[i].isActivated())
{
System.out.println(cell[i].getName()+" "+neighbors);
if(neighbors==0||neighbors==1||neighbors>3)
{
cell[i].deactivate();
}
}
if(cell[i].isActivated()==false)
{
if(neighbors==3)
{
cell[i].activate();
}
}
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("randomize"))
{
Random rn = new Random();
for(int i=0;i<totalCells;i++)
{
cell[i].deactivate();
if(rn.nextInt(6)==0)
{
cell[i].activate();
}
}
}
//help button, self-explanatory
if(e.getActionCommand().equals("help"))
{
JOptionPane.showMessageDialog(this, "The game is governed by four rules:\nFor a space that is 'populated':"
+ "\n Each cell with one or no neighbors dies, as if by loneliness."
+ "\n Each cell with four or more neighbors dies, as if by overpopulation."
+ "\n Each cell with two or three neighbors survives."
+ "\nFor a space that is 'empty' or 'unpopulated':"
+ "\n Each cell with three neighbors becomes populated."
+ "\nLeft click populates cells. Right click depopulates cells.","Rules:",JOptionPane.PLAIN_MESSAGE);
}
//shameless self promotion
if(e.getActionCommand().equals("about"))
{
JOptionPane.showMessageDialog(this, "Game made and owned by *****!"
+ "\nFree usage as see fit, but give credit where credit is due!\nVERSION: "+version,"About:",JOptionPane.PLAIN_MESSAGE);
}
//clears all the cells
if(e.getActionCommand().equals("clear"))
{
timer.stop();
generation = 0;
generationText = "Generation: "+generation;
generationLabel.setText(generationText);
for(int i=0;i<totalCells;i++)
{
cell[i].deactivate();
}
}
//starts timer
if(e.getActionCommand().equals("start"))
{
if(Integer.parseInt(speed.getText())>0)
{
timer.setDelay(Integer.parseInt(speed.getText()));
timer.restart();
}
else
{
JOptionPane.showMessageDialog(this, "Please use a value greater than 0!","Rules:",JOptionPane.ERROR_MESSAGE);
}
}
//stops timer
if(e.getActionCommand().equals("stop"))
{
timer.stop();
}
//run when timer
if(e.getActionCommand().equals("timer"))
{
generation++;
generationText = "Generation: "+generation;
generationLabel.setText(generationText);
timer.stop();
checkCells();
timer.setInitialDelay(Integer.parseInt(speed.getText()));
timer.restart();
}
//see checkCells()
if(e.getActionCommand().equals("check"))
{
generation++;
generationText = "Generation: "+generation;
generationLabel.setText(generationText);
checkCells();
}
//color select gui
if(e.getActionCommand().equals("colorSelect"))
{
userColor = JColorChooser.showDialog(this, "Choose a color:", userColor);
if(userColor==null)
{
userColor = Color.yellow;
}
}
//size chooser!
if(e.getActionCommand().equals("sizeChooser"))
{
SizeChooser size = new SizeChooser();
size.setLocationRelativeTo(null);
size.setVisible(true);
}
}
private int checkNeighbors(int c)
{
//if a LIVE neighbor is found, add one
int neighbors = 0;
if(cell[c].getPosX()!=0&&cell[c].getPosY()!=0)
{
if(c-columns-1>=0)
{
if(cell[c-columns-1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c-columns-1].getName());
neighbors++;
}
}
}
if(cell[c].getPosY()!=0)
{
if(c-columns>=0)
{
if(cell[c-columns].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c-columns].getName());
neighbors++;
}
}
}
if(cell[c].getPosX()!=columns-1&&cell[c].getPosY()!=0)
{
if(c-columns+1>=0)
{
if(cell[c-columns+1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c-columns+1].getName());
neighbors++;
}
}
}
if(cell[c].getPosX()!=0)
{
if(c-1>=0)
{
if(cell[c-1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c-1].getName());
neighbors++;
}
}
}
if(cell[c].getPosX()!=columns-1)
{
if(c+1<totalCells)
{
if(cell[c+1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c+1].getName());
neighbors++;
}
}
}
if(cell[c].getPosX()!=0&&cell[c].getPosY()!=rows-1)
{
if(c+columns-1<totalCells)
{
if(cell[c+columns-1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c+columns-1].getName());
neighbors++;
}
}
}
if(cell[c].getPosY()!=rows-1&&cell[c].getPosY()!=rows-1)
{
if(c+columns<totalCells)
{
if(cell[c+columns].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c+columns].getName());
neighbors++;
}
}
}
if(cell[c].getPosX()!=columns-1&&cell[c].getPosY()!=rows-1)
{
if(c+columns+1<totalCells)
{
if(cell[c+columns+1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c+columns+1].getName());
neighbors++;
}
}
}
return neighbors;
}
}
The following is MainCell.java:
public class MainCell extends JPanel implements MouseListener
{
//everything here should be self-explanatory
private static final long serialVersionUID = 1761933778208900172L;
private boolean activated = false;
public static boolean leftMousePressed;
public static boolean rightMousePressed;
private int posX = 0;
private int posY = 0;
private int neighbors = 0;
private URL cellImgURL_1 = getClass().getResource("images/cellImage_1.gif");
private ImageIcon cellImageIcon_1 = new ImageIcon(cellImgURL_1);
private Image cellImage_1 = cellImageIcon_1.getImage();
private URL cellImgURL_2 = getClass().getResource("images/cellImage_2.gif");
private ImageIcon cellImageIcon_2 = new ImageIcon(cellImgURL_2);
private Image cellImage_2 = cellImageIcon_2.getImage();
private URL cellImgURL_3 = getClass().getResource("images/cellImage_3.gif");
private ImageIcon cellImageIcon_3 = new ImageIcon(cellImgURL_3);
private Image cellImage_3 = cellImageIcon_3.getImage();
public MainCell()
{
Dimension dim = new Dimension(17, 17);
setPreferredSize(dim);
addMouseListener(this);
}
public void activate()
{
setBackground(MainGUI.userColor);
System.out.println(getName()+" "+posX+","+posY+" activated");
setActivated(true);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if(getPosX()==MainGUI.columns-1&&getPosY()==0)
{
//do nothing
}
else if(getPosY()!=0&&getPosX()!=MainGUI.columns-1)
{
g.drawImage(cellImage_1,0,0,null);
}
else if(getPosY()==0)
{
g.drawImage(cellImage_2,0,0,null);
}
else if(getPosX()==MainGUI.columns-1)
{
g.drawImage(cellImage_3,0,0,null);
}
}
public void setActivated(boolean b)
{
activated = b;
}
public void deactivate()
{
setBackground(Color.gray);
System.out.println(getName()+" "+posX+","+posY+" deactivated");
setActivated(false);
}
public boolean isActivated()
{
return activated;
}
public void setNeighbors(int i)
{
neighbors = i;
}
public int getNeighbors()
{
return neighbors;
}
public int getPosX()
{
return posX;
}
public void setPosX(int x)
{
posX = x;
}
public int getPosY()
{
return posY;
}
public void setPosY(int y)
{
posY = y;
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
if(leftMousePressed&&SwingUtilities.isLeftMouseButton(e))
{
activate();
}
if(rightMousePressed&&SwingUtilities.isRightMouseButton(e))
{
deactivate();
}
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
if(SwingUtilities.isRightMouseButton(e)&&!leftMousePressed)
{
deactivate();
rightMousePressed = true;
}
if(SwingUtilities.isLeftMouseButton(e)&&!rightMousePressed)
{
activate();
leftMousePressed = true;
}
}
public void mouseReleased(MouseEvent e)
{
if(SwingUtilities.isRightMouseButton(e))
{
rightMousePressed = false;
}
if(SwingUtilities.isLeftMouseButton(e))
{
leftMousePressed = false;
}
}
}
The following is SizeChooser.java:
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
public class SizeChooser extends JFrame
{
private static final long serialVersionUID = -6431709376438241788L;
public static MainGUI GUI;
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JTextField rowsTextField = new JTextField(String.valueOf((screenSize.height/17)-15));
JTextField columnsTextField = new JTextField(String.valueOf((screenSize.width/17)-10));
private static int rows = screenSize.height/17-15;
private static int columns = screenSize.width/17-10;
public SizeChooser()
{
setResizable(false);
setTitle("Select a size!");
JPanel container = new JPanel();
BoxLayout containerLayout = new BoxLayout(container, BoxLayout.PAGE_AXIS);
container.setLayout(containerLayout);
add(container);
JLabel rowsLabel = new JLabel("Rows:");
container.add(rowsLabel);
container.add(rowsTextField);
JLabel columnsLabel = new JLabel("Columns:");
container.add(columnsLabel);
container.add(columnsTextField);
JButton confirmSize = new JButton("Confirm");
confirmSize.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
GUI.setVisible(false);
GUI = null;
if(Integer.parseInt(rowsTextField.getText())>0)
{
rows = Integer.parseInt(rowsTextField.getText());
}
else
{
JOptionPane.showMessageDialog(rootPane, "Please use a value greater than 0!","Rules:",JOptionPane.ERROR_MESSAGE);
}
if(Integer.parseInt(columnsTextField.getText())>0)
{
columns = Integer.parseInt(columnsTextField.getText());
}
else
{
JOptionPane.showMessageDialog(rootPane, "Please use a value greater than 0!","Rules:",JOptionPane.ERROR_MESSAGE);
}
GUI = new MainGUI("The Game of Life!", rows, columns);
GUI.setLocationRelativeTo(null);
GUI.setVisible(true);
setVisible(false);
}
});
container.add(confirmSize);
URL imgURL = getClass().getResource("images/gol.gif");
ImageIcon icon = new ImageIcon(imgURL);
System.out.println(icon);
setIconImage(icon.getImage());
pack();
}
public static void main(String[]args)
{
GUI = new MainGUI("The Game of Life!", rows, columns);
GUI.setLocationRelativeTo(null);
GUI.setVisible(true);
}
}
So the problem now is, when the randomize button is pressed, or a large number of cells exist and then the timer is started, the cells aren't as snappy as they would be with less active cells. For example, with 100 columns and 50 rows, when the randomize button is pressed, one cell activates, then the next, then another, and so forth. Can I have them all activate at exactly the same time? Is this just a problem with too many things calculated at once? Would concurrency help?
QUICK EDIT: Is the swing timer the best idea for this project?
I havent read through your code completely but I am guessing that your listener methods are fairly computationally intensive and hence the lag in updating the display.
This simple test reveals that your randomize operation should be fairly quick:
public static void main(String args[]) {
Random rn = new Random();
boolean b[] = new boolean[1000000];
long timer = System.nanoTime();
for (int i = 0; i < b.length; i++) {
b[i] = rn.nextInt(6) == 0;
}
timer = System.nanoTime() - timer;
System.out.println(timer + "ns / " + (timer / 1000000) + "ms");
}
The output for me is:
17580267ns / 17ms
So this leads me into thinking activate() or deactivate() is causing your UI to be redrawn.
I am unable to run this because I don't have your graphical assets, but I would try those changes to see if it works:
In MainGUI#actionPerformed, change:
if(e.getActionCommand().equals("randomize"))
{
Random rn = new Random();
for(int i=0;i<totalCells;i++)
{
cell[i].deactivate();
if(rn.nextInt(6)==0)
{
cell[i].activate();
}
}
}
to:
if(e.getActionCommand().equals("randomize"))
{
Random rn = new Random();
for(int i=0;i<totalCells;i++)
{
// This will not cause the object to be redrawn and should
// be a fairly cheap operation
cell[i].setActivated(rn.nextInt(6)==0);
}
// Cause the UI to repaint
repaint();
}
Add this to MainCell
// You can specify those colors however you like
public static final Color COLOR_ACTIVATED = Color.RED;
public static final Color COLOR_DEACTIVATED = Color.GRAY;
And change:
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if(getPosX()==MainGUI.columns-1&&getPosY()==0)
{
//do nothing
}
to:
protected void paintComponent(Graphics g)
{
// We now make UI changes only when the component is painted
setBackground(activated ? COLOR_ACTIVATED : COLOR_DEACTIVATED);
super.paintComponent(g);
if(getPosX()==MainGUI.columns-1&&getPosY()==0)
{
//do nothing
}

Update JTextField from another thread in Java

I'm making a game client/server and I'm having a new thread update some info on the client GUI (which is using Swing). I'm trying to use SwingUtilities but it's not working. Also, I heard SwingUtilities creates a new thread everytime you use it so I'm looking for a new way as well (I have 10 or so JTextFields to be updated). Is there a way to do it without using SwingUtilities? Here is what I have right now.
SwingUtilities.invokeLater(
new Runnable() {
public void run()
{
Client.status.setText("status = "+status);
}
});
Interestingly, just recently I encountered a similar issue, so In order to get over this I used SwingUtilities.invokeAndWait(Runnable runnable), that's what made the SSCCE I was creating to work as expected, though if I change all calls to invokeAndWait() with invokeLater(), one can clearly see the difference between the two things.
A quote from Java Doc says :
Causes doRun.run() to be executed synchronously on the AWT event dispatching thread.
This call blocks until all pending AWT events have been processed and
(then) doRun.run() returns. This method should be used when an application thread
needs to update the GUI.
This is a small program I had made as an SSCCE to represent a Bubble Sort Animation :
import javax.swing.*;
public class BubbleSortFrame extends JFrame
{
private BubbleSortView contentPane;
private void displayGUI()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new BubbleSortView();
setContentPane(contentPane);
pack();
setLocationByPlatform(true);
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new BubbleSortFrame().displayGUI();
}
});
}
}
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
public class BubbleSortView extends JPanel
{
private JLabel sizeLabel;
private JTextField sizeField;
private JTextField[] vField;
private JLabel[] vLabel;
private JButton startButton, createButton;
private int size;
private JPanel createPanel, animationPanel;
private BubbleSort bubbleSort;
public BubbleSortView()
{
size = 5;
displayAndCreateGUI();
}
private void displayAndCreateGUI()
{
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setOpaque(true);
setBackground(Color.WHITE);
JPanel basePanel = new JPanel();
basePanel.setLayout(new GridLayout(2, 1, 5, 5));
basePanel.setOpaque(true);
basePanel.setBackground(Color.WHITE);
JPanel topPanel = new JPanel();
topPanel.setOpaque(true);
topPanel.setBackground(Color.WHITE);
topPanel.setBorder(
BorderFactory.createTitledBorder("Input : "));
topPanel.setLayout(new GridLayout(2, 1, 5, 5));
/*
* This will act as the area
* for taking the input for
* number of elements in an Array.
*/
JPanel sizePanel = new JPanel();
sizePanel.setOpaque(true);
sizePanel.setBackground(Color.WHITE);
sizeLabel = new JLabel("Enter Number of Elements : ");
sizeField = new JTextField(10);
createButton = new JButton("CREATE");
/*
* This will act as the area
* where we will specify the values
* for each index in an Array.
*/
createPanel = new JPanel();
createPanel.setOpaque(true);
createPanel.setBackground(Color.WHITE);
createPanel.setBorder(
BorderFactory.createTitledBorder("Please Enter values for an Array : "));
createPanel.setVisible(false);
animationPanel = new JPanel();
animationPanel.setOpaque(true);
animationPanel.setBackground(Color.WHITE);
animationPanel.setBorder(
BorderFactory.createTitledBorder("Animation : "));
animationPanel.setVisible(false);
createButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
if (sizeField.getDocument().getLength() > 0)
{
size = Integer.parseInt(sizeField.getText());
vField = new JTextField[size];
createPanel.setVisible(true);
for (int i = 0; i < size; i++)
{
vField[i] = new JTextField(5);
/*
* Adding the Listener to the
* last JTextField on the Right
* Side.
*/
if (i == (size - 1))
{
vField[i].addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
animationPanel.setLayout(
new GridLayout(1, size, 2, 2));
animationPanel.setVisible(true);
vLabel = new JLabel[size];
for (int i = 0; i < size; i++)
{
vLabel[i] = new JLabel(
vField[i].getText(), JLabel.CENTER);
vLabel[i].setOpaque(true);
vLabel[i].setBackground(Color.YELLOW);
vLabel[i].setForeground(Color.RED);
animationPanel.add(vLabel[i]);
}
animationPanel.revalidate();
animationPanel.repaint();
bubbleSort = new BubbleSort(vLabel, size);
Thread t = new Thread(bubbleSort);
t.start();
}
});
}
createPanel.add(vField[i]);
}
createPanel.revalidate();
createPanel.repaint();
createButton.setEnabled(false);
}
else
size = 5;
}
});
sizePanel.add(sizeLabel);
sizePanel.add(sizeField);
sizePanel.add(createButton);
/*
* Initializing JTextField Array
* so that it can be first presented
* to the USER to take input for
* 5 values.
*/
//for (int i = 0; i < size; i++)
// vField[i] = new JTextField(5);
topPanel.add(sizePanel);
topPanel.add(createPanel);
basePanel.add(topPanel);
basePanel.add(animationPanel);
add(basePanel);
}
private class BubbleSort implements Runnable
{
private int[] arr;
private JLabel[] vLabel;
private int size;
private int pass;
public BubbleSort(JLabel[] label, int size)
{
vLabel = label;
this.size = size;
pass = 1;
for (int i = 0; i < size; i++)
System.out.print("" + vLabel[i].getText() + "\t");
System.out.println("");
}
#Override
public void run()
{
try
{
bubbleSorting();
}
catch (InvocationTargetException ite)
{
ite.printStackTrace();
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
private void bubbleSorting()
throws InterruptedException, InvocationTargetException
{
while (pass < size)
{
for (int i = 0; i < (size - pass); i++)
{
final int j = i;
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run()
{
vLabel[j].setBackground(Color.RED);
vLabel[j].setForeground(Color.WHITE);
vLabel[j + 1].setBackground(Color.RED);
vLabel[j + 1].setForeground(Color.WHITE);
}
});
try
{
Thread.sleep(1500);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
int left = Integer.parseInt(vLabel[i].getText());
int right = Integer.parseInt(vLabel[i + 1].getText());
if (left > right)
{
String temp = vLabel[i].getText();
vLabel[i].setText(vLabel[i + 1].getText());
vLabel[i + 1].setText(temp);
}
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run()
{
vLabel[j].setBackground(Color.YELLOW);
vLabel[j].setForeground(Color.RED);
vLabel[j + 1].setBackground(Color.YELLOW);
vLabel[j + 1].setForeground(Color.RED);
}
});
}
System.out.println("Pass : " + pass + "\tSize : " + size);
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run()
{
vLabel[size - pass].setBackground(Color.GREEN);
vLabel[size - pass].setForeground(Color.BLUE);
}
});
pass++;
}
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run()
{
vLabel[0].setBackground(Color.GREEN);
vLabel[0].setForeground(Color.BLUE);
}
});
}
}
}
I don't know where you heard about "SwingUtilities creates a new thread" from, but I think you've either misunderstood or being informed incorrectly. SwingUtilities.invokeLater places the Runnable onto the end of the Event Dispatcher's queue. The queue then processes this event within the it's own thread context (in time), calling Run, there is no "new" thread created for this process.
As to your question.
You may need to call validate() (and possibly repaint()) on the fields parent container to encourage it update ;)
And no, there isn't any other way to sync the UI across threads, that's the reason for SwingUtilities

Categories