Browse Source

Oh look it works

master
Lee 12 years ago
parent
commit
b98001bf82
2 changed files with 85 additions and 111 deletions
  1. +39
    -36
      src/Goldfish.java
  2. +46
    -75
      src/Render.java

+ 39
- 36
src/Goldfish.java View File

@@ -1,36 +1,39 @@
package edu.stuy.goldfish;


import edu.stuy.goldfish.rules.*;

public class Goldfish {

private Grid _grid;
private Render _render;

public Goldfish () {
_render = new Render(640, 480);
_grid = new Grid(10, 10);
}

public void run () {
//TODO: make it run.
_grid.getPatch(1,0).setState(1);
_grid.getPatch(2,1).setState(1);
_grid.getPatch(2,2).setState(1);
_grid.getPatch(1,2).setState(1);
_grid.getPatch(0,2).setState(1);
System.out.println(_grid);

for (int i = 0; i < 10; i++) {
_grid = LifeWithoutDeath.run(_grid);
System.out.println("------------");
System.out.println(_grid);
}
}

public static void main (String[] args) {
Goldfish g = new Goldfish();
g.run();
}
}
package edu.stuy.goldfish;
import edu.stuy.goldfish.rules.*;
public class Goldfish {
private Grid _grid;
private Render _render;
public Goldfish () {
_render = new Render(96, 96);
_grid = new Grid(96, 96);
}
public void run () {
//TODO: make it run.
_grid.getPatch(1,0).setState(1);
_grid.getPatch(2,1).setState(1);
_grid.getPatch(2,2).setState(1);
_grid.getPatch(1,2).setState(1);
_grid.getPatch(0,2).setState(1);
System.out.println(_grid);
_grid = LifeWithoutDeath.run(_grid);
System.out.println("------------");
System.out.println(_grid);
_render.setGrid(_grid);
_render.run();
}
public static void main (String[] args) {
Goldfish g = new Goldfish();
while(true) {
g.run();
}
}
}

+ 46
- 75
src/Render.java View File

@@ -11,121 +11,93 @@ import java.util.Random;
import javax.swing.JFrame; import javax.swing.JFrame;
import edu.stuy.goldfish.rules.*;
public class Render extends Canvas implements Runnable { public class Render extends Canvas implements Runnable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static int width; public static int width;
public static int height; public static int height;
public static int scale = 2; public static int scale = 2;
public String title = "Title";
private Thread thread;
public String title = "Goldfish";
private JFrame frame; private JFrame frame;
private boolean running = false;
private BufferedImage image; private BufferedImage image;
private Grid _grid; private Grid _grid;
private int[] pixels; private int[] pixels;
Random random = new Random(); Random random = new Random();
public Render(int width, int height, Grid g) {
Render.width = width;
Render.height = height;
_grid = g;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
setFrame();
}
public Render(int width, int height) { public Render(int width, int height) {
Render.width = width; Render.width = width;
Render.height = height; Render.height = height;
_grid = new Grid(width, height); _grid = new Grid(width, height);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
Dimension size = new Dimension(width*scale, height*scale);
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size); setPreferredSize(size);
frame = new JFrame();
setFrame();
} }
public Render() { public Render() {
width = 320; width = 320;
height = 240; height = 240;
_grid = new Grid(10, 10); _grid = new Grid(10, 10);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
Dimension size = new Dimension(width*scale, height*scale);
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size); setPreferredSize(size);
frame = new JFrame(); frame = new JFrame();
} }
public synchronized void start() {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
private void setFrame() {
frame = new JFrame();
frame.setResizable(false);
frame.setTitle(title);
frame.add(this);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} }
long lastTime;
long timer;
double delta;
int frames;
int updates;
long now;
public void run() { public void run() {
lastTime = System.nanoTime();
timer = System.currentTimeMillis();
delta = 0;
frames = 0;
updates = 0;
while (running) {
now = System.nanoTime();
delta += (now - lastTime) * 6.0 / 100000000.0;
lastTime = now;
while (delta >= 1) {
update();
updates++;
delta--;
render();
frames++;
}
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
frame.setTitle(title + " | updates: " + updates + " | frames: "+ frames);
frames = 0;
updates = 0;
}
}
stop();
render();
} }
public void update() { public void update() {
//TODO: make it run.
for (int i = 0; i < width; i++) { for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
// _grid.getPatch(i,j).setState(1);
draw(i,j,random.nextInt());
// //draw(i,j,_grid.getPatch(i,j).getState());
for (int j = 0; j < height; j++) {
// draw(i,j,random.nextInt());
// Patch x = new Patch();
// x.setState(0x13bd76);
// _grid.setPatch(i,j,x);
draw(i, j, _grid.getPatch(i, j).getState() * 0xffffff);
} }
} }
// System.out.println(_grid);
// _grid = Conway.run(_grid);
// System.out.println("------------");
// System.out.println(_grid);
} }
BufferStrategy bs; BufferStrategy bs;
Graphics g; Graphics g;
public void render() { public void render() {
bs = getBufferStrategy(); bs = getBufferStrategy();
if (bs == null) { if (bs == null) {
createBufferStrategy(3); createBufferStrategy(3);
return; return;
} }
clear(); clear();
update(); update();
@@ -140,20 +112,19 @@ public class Render extends Canvas implements Runnable {
pixels[x] = 0; pixels[x] = 0;
} }
} }
public void setGrid(Grid g) {
_grid = g;
}
public void draw(int x, int y, int color) { public void draw(int x, int y, int color) {
pixels[x + y * width] = color; pixels[x + y * width] = color;
} }
public static void main(String[] args) { public static void main(String[] args) {
Render render = new Render(320,240);
render.frame.setResizable(false);
render.frame.setTitle(render.title);
render.frame.add(render);
render.frame.pack();
render.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
render.frame.setLocationRelativeTo(null);
render.frame.setVisible(true);
render.start();
Render render = new Render(320, 240);
while (true) {
render.run();
}
} }
} }

Loading…
Cancel
Save