Browse Source

Combined Screen and Render

How are we going to mash this together
master
Lee 12 years ago
parent
commit
13f108f22c
2 changed files with 53 additions and 61 deletions
  1. +53
    -24
      src/Render.java
  2. +0
    -37
      src/Screen.java

+ 53
- 24
src/Render.java View File

@@ -1,3 +1,4 @@
package edu.stuy.goldfish;
import java.awt.Canvas;
import java.awt.Dimension;
@@ -6,8 +7,12 @@ import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Random;
import javax.swing.JFrame;
import edu.stuy.goldfish.rules.*;
public class Render extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
@@ -21,31 +26,31 @@ public class Render extends Canvas implements Runnable {
private JFrame frame;
private boolean running = false;
private Screen screen;
private BufferedImage image;
private Grid _grid;
private int[] pixels;
Random random = new Random();
public Render(int width, int height) {
this.width = width;
this.height = height;
Render.width = width;
Render.height = height;
_grid = new Grid(width, height);
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);
screen = new Screen(width, height);
frame = new JFrame();
}
public Render() {
width = 320;
height = 240;
_grid = new Grid(10, 10);
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);
screen = new Screen(width, height);
frame = new JFrame();
}
@@ -64,13 +69,18 @@ public class Render extends Canvas implements Runnable {
}
}
long lastTime;
long timer;
double delta;
int frames;
int updates;
long now;
public void run() {
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
double delta = 0;
int frames = 0;
int updates = 0;
long now;
lastTime = System.nanoTime();
timer = System.currentTimeMillis();
delta = 0;
frames = 0;
updates = 0;
while (running) {
now = System.nanoTime();
delta += (now - lastTime) * 6.0 / 100000000.0;
@@ -93,30 +103,50 @@ public class Render extends Canvas implements Runnable {
}
public void update() {
//TODO: make it run.
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());
}
}
// System.out.println(_grid);
// _grid = Conway.run(_grid);
// System.out.println("------------");
// System.out.println(_grid);
}
BufferStrategy bs;
Graphics g;
public void render() {
BufferStrategy bs = getBufferStrategy();
bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
clear();
update();
screen.clear();
screen.render();
for (int i = 0; i < pixels.length; i++) {
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
public void clear() {
for (int x = 0; x < pixels.length; x++) {
pixels[x] = 0;
}
}
public void draw(int x, int y, int color) {
pixels[x + y * width] = color;
}
public static void main(String[] args) {
Render render = new Render(160,120);
Render render = new Render(320,240);
render.frame.setResizable(false);
render.frame.setTitle(render.title);
render.frame.add(render);
@@ -124,7 +154,6 @@ public class Render extends Canvas implements Runnable {
render.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
render.frame.setLocationRelativeTo(null);
render.frame.setVisible(true);
render.start();
}
}

+ 0
- 37
src/Screen.java View File

@@ -1,37 +0,0 @@
import java.util.Random;
public class Screen {
private int width, height;
public int[] pixels;
private int[] _grid;
Random random = new Random();
public Screen(int width, int height) {
this.width = width;
this.height = height;
pixels = new int[width * height];
_grid = new int[width * height];
}
public void clear() {
for (int x = 0; x < _grid.length; x++) {
pixels[x] = 0;
}
}
public void render() {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
pixels[x+y*width] = random.nextInt();
//pixels[x + y * width] = _grid[x + y * width];
}
}
}
public void draw(int x, int y, int color) {
_grid[x + y * width] = color;
}
}

Loading…
Cancel
Save