Преглед на файлове

Dekker's algorithm so randomize()/clear() don't clash with run().

master
Ben Kurtovic преди 12 години
родител
ревизия
38091d3d07
променени са 3 файла, в които са добавени 36 реда и са изтрити 5 реда
  1. +2
    -0
      src/Goldfish.java
  2. +2
    -3
      src/Grid.java
  3. +32
    -2
      src/Render.java

+ 2
- 0
src/Goldfish.java Целия файл

@@ -23,6 +23,7 @@ public class Goldfish {
_render.reset = false;
}
if (!_render.paused) {
_render.acquireLock(0);
String rule = _render.rule;
if (rule.equals("Conway"))
_grid = Conway.run(_grid);
@@ -32,6 +33,7 @@ public class Goldfish {
_grid = LifeWithoutDeath.run(_grid);
else if (rule.equals("Brian's Brain"))
_grid = BriansBrain.run(_grid);
_render.releaseLock(0);
}
_render.setGrid(_grid);
_render.run();


+ 2
- 3
src/Grid.java Целия файл

@@ -1,7 +1,6 @@
package edu.stuy.goldfish;

public class Grid {

private Patch[][] _grid;

public Grid() {
@@ -20,8 +19,8 @@ public class Grid {
}
}

public Grid (int x, int y) {
this(x,y,true);
public Grid(int x, int y) {
this(x, y, true);
}

private int normalizeX(int x) {


+ 32
- 2
src/Render.java Целия файл

@@ -1,6 +1,8 @@
package edu.stuy.goldfish;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.awt.*;
import java.awt.event.*;
@@ -23,6 +25,8 @@ public class Render extends Canvas implements Runnable, MouseListener,
public String rule;
public boolean reset;
private AtomicBoolean[] _flags;
private AtomicInteger _turn;
private Grid _grid;
private int[] _pixels;
private BufferedImage _image;
@@ -43,6 +47,10 @@ public class Render extends Canvas implements Runnable, MouseListener,
paused = false;
reset = false;
rule = rules[0];
_flags = new AtomicBoolean[2];
for (int i = 0; i < _flags.length; i++)
_flags[i] = new AtomicBoolean();
_turn = new AtomicInteger();
_grid = g;
_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
_pixels = ((DataBufferInt) _image.getRaster().getDataBuffer()).getData();
@@ -78,8 +86,8 @@ public class Render extends Canvas implements Runnable, MouseListener,
private void setFrame() {
JMenuBar menuBar = new JMenuBar();
JMenu menuAlgo = new JMenu("Algorithms");
menuAlgo.setFont(new Font("Arial",1,12));
menuAlgo.setPreferredSize(new Dimension(75,0));
menuAlgo.setFont(new Font("Arial", 1, 12));
menuAlgo.setPreferredSize(new Dimension(75, 0));
for (String rule : _rules) {
JMenuItem menuAlgoItem = new JMenuItem(rule);
menuAlgo.add(menuAlgoItem);
@@ -163,6 +171,24 @@ public class Render extends Canvas implements Runnable, MouseListener,
}
}
public void acquireLock(int thread) {
int other = (thread == 0 ? 1 : 0);
_flags[thread].set(true);
while (_flags[other].get() == true) {
if (_turn.get() != thread) {
_flags[thread].set(false);
while (_turn.get() != thread) {}
_flags[thread].set(true);
}
}
}
public void releaseLock(int thread) {
int other = (thread == 0 ? 1 : 0);
_turn.set(other);
_flags[thread].set(false);
}
public void run() {
BufferStrategy bs;
Graphics g;
@@ -203,19 +229,23 @@ public class Render extends Canvas implements Runnable, MouseListener,
}
public void clear() {
acquireLock(1);
for (int i = 0; i < _grid.getHeight(); i++) {
for (int j = 0; j < _grid.getWidth(); j++) {
_grid.getPatch(i, j).setState(0);
}
}
releaseLock(1);
}
public void randomize() {
acquireLock(1);
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)));
}
}
releaseLock(1);
}
private void mouseDraw(MouseEvent e) {


Зареждане…
Отказ
Запис