From 5ae8ade3cd3db1ba83cbb69961f9c73a347823cd Mon Sep 17 00:00:00 2001 From: Kevin Li Date: Mon, 14 Jan 2013 09:44:27 -0500 Subject: [PATCH 1/2] Moved Renderer --- src/Render.java | 156 +++++++++++++++++++++++++++++++++++++++++------------- src/Renderer.java | 120 ----------------------------------------- src/Screen.java | 6 ++- 3 files changed, 124 insertions(+), 158 deletions(-) delete mode 100644 src/Renderer.java diff --git a/src/Render.java b/src/Render.java index 9388130..9804fa3 100644 --- a/src/Render.java +++ b/src/Render.java @@ -1,37 +1,119 @@ -package edu.stuy.goldfish; - - -import java.awt.Color; - -public class Render { - public Render (int width, int height) { - } - - public Render () { - this(100,100); - } - - - - /** - * Render a color to a position - * - * This should draw to a buffer, which should be - * rendered with render() - * - * @param x The x coordinate - * @param y The y coordinate - * @param c The color to draw - */ - public void draw (int x, int y, Color c) { - - } - - /** - * Renders the stored buffer to the screen. - */ - public void render () { - - } - -} + +import java.awt.Canvas; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.image.BufferStrategy; +import java.awt.image.BufferedImage; +import java.awt.image.DataBufferInt; + +import javax.swing.JFrame; + +public class Renderer extends Canvas implements Runnable { + private static final long serialVersionUID = 1L; + + public static int width = 320; + public static int height = 240; + public static int scale = 2; + + public String title = "Title"; + + private Thread thread; + private JFrame frame; + private boolean running = false; + + private Screen screen; + + private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); + + public Renderer(int width, int height) { + System.out.println(width); + } + + public Renderer() { + Dimension size = new Dimension(width*scale, height*scale); + setPreferredSize(size); + screen = new Screen(width, height); + + 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(); + } + } + + public void run() { + long lastTime = System.nanoTime(); + long timer = System.currentTimeMillis(); + double delta = 0; + int frames = 0; + int updates = 0; + long now; + 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(); + } + + public void update() { + } + + public void render() { + BufferStrategy bs = getBufferStrategy(); + if (bs == null) { + createBufferStrategy(3); + return; + } + + screen.clear(); + screen.render(); + + for (int i = 0; i < pixels.length; i++) { + pixels[i] = screen.pixels[i]; + } + + Graphics g = bs.getDrawGraphics(); + g.drawImage(image, 0, 0, getWidth(), getHeight(), null); + g.dispose(); + bs.show(); + } + + public static void main(String[] args) { + Renderer render = new Renderer(); + 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(); + } +} diff --git a/src/Renderer.java b/src/Renderer.java deleted file mode 100644 index 65c0d4c..0000000 --- a/src/Renderer.java +++ /dev/null @@ -1,120 +0,0 @@ - -import java.awt.Canvas; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.image.BufferStrategy; -import java.awt.image.BufferedImage; -import java.awt.image.DataBufferInt; - -import javax.swing.JFrame; - -public class Renderer extends Canvas implements Runnable { - private static final long serialVersionUID = 1L; - - public static int width = 320; - public static int height = 240; - public static int scale = 2; - - public String title = "Title"; - - private Thread thread; - private JFrame frame; - private boolean running = false; - - private Screen screen; - - private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); - private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); - - public Renderer(int width, int height) { - System.out.println(width); - } - - public Renderer() { - Dimension size = new Dimension(width*scale, height*scale); - setPreferredSize(size); - screen = new Screen(width, height); - - 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(); - } - } - - public void run() { - long lastTime = System.nanoTime(); - long timer = System.currentTimeMillis(); - double delta = 0; - int frames = 0; - int updates = 0; - long now; - 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(); - } - - public void update() { - } - - public void render() { - BufferStrategy bs = getBufferStrategy(); - if (bs == null) { - createBufferStrategy(3); - return; - } - - screen.clear(); - screen.render(); - - for (int i = 0; i < pixels.length; i++) { - pixels[i] = screen.pixels[i]; - } - - Graphics g = bs.getDrawGraphics(); - g.drawImage(image, 0, 0, getWidth(), getHeight(), null); - g.dispose(); - bs.show(); - } - - public static void main(String[] args) { - Renderer render = new Renderer(); - 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.screen.draw(100,100,0xFFFFFF); - } -} diff --git a/src/Screen.java b/src/Screen.java index 1e00d30..3f76d3e 100644 --- a/src/Screen.java +++ b/src/Screen.java @@ -1,3 +1,4 @@ +import java.util.Random; public class Screen { @@ -5,6 +6,8 @@ public class Screen { public int[] pixels; private int[] _grid; + Random random = new Random(); + public Screen(int width, int height) { this.width = width; this.height = height; @@ -22,7 +25,8 @@ public class Screen { public void render() { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { - pixels[x + y * width] = _grid[x + y * width]; + pixels[x+y*width] = random.nextInt(); + //pixels[x + y * width] = _grid[x + y * width]; } } } From 22d1af5b205414f9bc077e3ec0074806c744a0e1 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 14 Jan 2013 09:48:40 -0500 Subject: [PATCH 2/2] Adding some methods because Josh. --- src/Grid.java | 12 ++++++++++-- src/Patch.java | 21 +++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Grid.java b/src/Grid.java index 1fd1052..8943dd1 100644 --- a/src/Grid.java +++ b/src/Grid.java @@ -18,11 +18,19 @@ public class Grid { } } - public Patch getPatch (int x, int y) { + public int getWidth() { + return _grid.length; + } + + public int getHeight() { + return _grid[0].length; + } + + public Patch getPatch(int x, int y) { return _grid[x][y]; } - public void setPatch (int x, int y, Patch p) { + public void setPatch(int x, int y, Patch p) { _grid[x][y] = p; } diff --git a/src/Patch.java b/src/Patch.java index 264220f..b2faf4c 100644 --- a/src/Patch.java +++ b/src/Patch.java @@ -1,18 +1,31 @@ package edu.stuy.goldfish; public class Patch { - private int _pxcor, _pycor; + private int _pxcor, _pycor, _state; private String _plabel; private Grid _myGrid; - public Patch(Grid grid, int xcor, int ycor) { + public Patch(Grid grid, int xcor, int ycor, int state) { _myGrid = grid; _pxcor = xcor; _pycor = ycor; + _state = state; } - public Patch () { - this(new Grid(), 0, 0); + public Patch() { + this(new Grid(), 0, 0, 0); + } + + public Grid getGrid() { + return _grid; + } + + public int getX() { + return _pxcor; + } + + public int getY() { + return _pycor; } public String toString() {