Pārlūkot izejas kodu

Removed ItemListener, added Random button

master
Lee pirms 12 gadiem
vecāks
revīzija
52c640d3dd
2 mainītis faili ar 21 papildinājumiem un 10 dzēšanām
  1. +1
    -5
      src/Goldfish.java
  2. +20
    -5
      src/Render.java

+ 1
- 5
src/Goldfish.java Parādīt failu

@@ -84,11 +84,7 @@ public class Goldfish {
}
}
} else if (rule.equals("Brian's Brain")) {
for(int i = 0; i < _grid.getHeight(); i++) {
for (int j = 0; j < _grid.getWidth(); j++) {
_grid.getPatch(i,j).setState(random.nextInt(3));
}
}
_render.randomize();
}
}


+ 20
- 5
src/Render.java Parādīt failu

@@ -6,8 +6,10 @@ import java.awt.image.*;
import javax.swing.*;
import java.util.Random;
public class Render extends Canvas implements Runnable, MouseListener,
MouseMotionListener, ActionListener, ItemListener {
MouseMotionListener, ActionListener {
private static final long serialVersionUID = 1L;
public static String title = "Goldfish";
@@ -30,6 +32,8 @@ public class Render extends Canvas implements Runnable, MouseListener,
private JFrame _frame;
private JButton pauseButton;
private Random random = new Random();
public Render(int width, int height, Grid g, String[] rules) {
addMouseListener(this);
@@ -94,6 +98,11 @@ public class Render extends Canvas implements Runnable, MouseListener,
menuBar.add(resetButton);
resetButton.addActionListener(this);
JButton randomButton = new JButton("Random");
randomButton.setActionCommand("random");
menuBar.add(randomButton);
randomButton.addActionListener(this);
JButton clearButton = new JButton("Clear");
clearButton.setActionCommand("clear");
menuBar.add(clearButton);
@@ -169,6 +178,14 @@ public class Render extends Canvas implements Runnable, MouseListener,
}
}
}
public void randomize() {
for(int i = 0; i < _grid.getHeight(); i++) {
for (int j = 0; j < _grid.getWidth(); j++) {
_grid.getPatch(i,j).setState(random.nextInt(Goldfish.getMaxStates(rule)));
}
}
}
@Override
public void mouseDragged(MouseEvent e) {
@@ -223,14 +240,12 @@ public class Render extends Canvas implements Runnable, MouseListener,
} else if ("reset".equals(event.getActionCommand())) {
clear();
reset = true;
} else if ("random".equals(event.getActionCommand())) {
randomize();
} else if ("clear".equals(event.getActionCommand())) {
clear();
} else {
rule = event.getActionCommand();
}
}
@Override
public void itemStateChanged(ItemEvent event) {
}
}

Notiek ielāde…
Atcelt
Saglabāt