Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package core; | ||
|
||
import java.awt.Color; | ||
|
||
import gui.Renderer; | ||
import gui.Screen; | ||
|
||
public class ViewController implements Runnable{ | ||
|
||
private final int FPS = 60; | ||
|
||
private Renderer renderer; | ||
private Screen screen; | ||
|
||
|
||
public ViewController(Renderer r) | ||
{ | ||
this.renderer = r; | ||
this.screen = new Screen(1000,700); | ||
|
||
Thread t = new Thread(this); | ||
t.start(); | ||
|
||
} | ||
|
||
private void gameLoop(double dt) | ||
{ | ||
this.renderer.render(this.screen.getBackgroundBuffer()); | ||
this.screen.getBackgroundBuffer().setColor(Color.RED); | ||
this.screen.getBackgroundBuffer().drawString("FPS: "+this.currentFPS, 0, 15); | ||
this.screen.render(); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
// FPS-meter | ||
double timeLastFrame; | ||
|
||
double timeThisSecond; | ||
int framesThisSecond; | ||
int currentFPS; | ||
|
||
|
||
// Run method | ||
|
||
@Override | ||
public void run() { | ||
this.timeLastFrame = System.nanoTime(); | ||
while(!Thread.interrupted()) { | ||
try { | ||
Thread.sleep(1000/FPS); | ||
|
||
double thisTime = System.nanoTime(); | ||
double dt = thisTime - this.timeLastFrame; | ||
this.timeLastFrame = thisTime; | ||
|
||
if (this.timeThisSecond > 1000000000.0) { | ||
this.currentFPS = this.framesThisSecond; | ||
this.framesThisSecond = 0; | ||
this.timeThisSecond = 0; | ||
System.out.println("FPS: "+this.currentFPS); | ||
} else { | ||
this.timeThisSecond += dt; | ||
this.framesThisSecond++; | ||
} | ||
|
||
this.gameLoop(dt); | ||
|
||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package gui; | ||
|
||
import java.awt.Graphics; | ||
|
||
public interface Renderer { | ||
|
||
public void render(Graphics g); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package gui; | ||
|
||
import java.awt.Color; | ||
import java.awt.Graphics; | ||
import java.awt.image.BufferedImage; | ||
import java.util.Random; | ||
|
||
public class RendererV1 implements Renderer{ | ||
|
||
private DrawableObject2[] drawables; | ||
|
||
public RendererV1() | ||
{ | ||
|
||
int width = 100; | ||
int height = 70; | ||
int tileWidth = 1000/width; | ||
int tileHeight = 700/height; | ||
|
||
this.drawables = new DrawableObject2[width*height]; | ||
for (int i = 0; i < width; i++) { | ||
for (int j = 0; j < height; j++) { | ||
drawables[i*height+j] = new DrawableObject2(i*tileWidth, j*tileHeight, tileWidth, tileHeight); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void render(Graphics g) { | ||
|
||
for (DrawableObject2 dO : this.drawables) { | ||
dO.draw(g); | ||
} | ||
} | ||
|
||
public class DrawableObject1 { | ||
|
||
private BufferedImage i; | ||
|
||
public int x; | ||
public int y; | ||
public int width; | ||
public int height; | ||
|
||
public DrawableObject1(int x, int y, int width, int height) | ||
{ | ||
this.x = x; | ||
this.y = y; | ||
this.width = width; | ||
this.height = height; | ||
|
||
this.i = Colors.getRandomImage(); | ||
} | ||
|
||
public void draw(Graphics g) { | ||
g.drawImage(this.i, x,y,width,height,null); | ||
} | ||
} | ||
public class DrawableObject2 { | ||
|
||
private BufferedImage i; | ||
|
||
public int x; | ||
public int y; | ||
public int width; | ||
public int height; | ||
|
||
public DrawableObject2(int x, int y, int width, int height) | ||
{ | ||
this.x = x; | ||
this.y = y; | ||
this.width = width; | ||
this.height = height; | ||
|
||
this.i = Colors.getImages(); | ||
} | ||
|
||
public void draw(Graphics g) { | ||
g.drawImage(this.i, x,y,width,height,null); | ||
} | ||
} | ||
public static class Colors{ | ||
|
||
private static BufferedImage bi; | ||
private static BufferedImage[] images; | ||
private static Random r; | ||
|
||
public static BufferedImage getRandomImage() | ||
{ | ||
bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); | ||
Graphics g = bi.getGraphics(); | ||
|
||
Random r = new Random(); | ||
g.setColor(new Color(r.nextInt(200), r.nextInt(200), r.nextInt(200))); | ||
g.fillRect(0, 0, 10, 10); | ||
return bi; | ||
} | ||
|
||
public static BufferedImage getImages() | ||
{ | ||
int numberOfImages = 10; | ||
|
||
if (images == null) { | ||
|
||
r = new Random(); | ||
images = new BufferedImage[numberOfImages]; | ||
|
||
for (int i = 0; i < numberOfImages; i++) { | ||
|
||
bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); | ||
Graphics g = bi.getGraphics(); | ||
|
||
Random r = new Random(); | ||
g.setColor(new Color(r.nextInt(200), r.nextInt(200), r.nextInt(200))); | ||
g.fillRect(0, 0, 10, 10); | ||
|
||
images[i] = bi; | ||
} | ||
} | ||
|
||
return images[r.nextInt(numberOfImages)]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package gui; | ||
|
||
import java.awt.Color; | ||
import java.awt.Dimension; | ||
import java.awt.Graphics; | ||
import java.awt.image.BufferedImage; | ||
|
||
import javax.swing.JFrame; | ||
import javax.swing.WindowConstants; | ||
|
||
@SuppressWarnings("serial") | ||
public class Screen extends JFrame{ | ||
|
||
private BufferedImage offScreen; | ||
private Graphics backgroundBuffer; | ||
|
||
public Screen(int width, int height) | ||
{ | ||
this.getContentPane().setPreferredSize(new Dimension(width,height)); | ||
|
||
this.getContentPane().setPreferredSize(new Dimension(width, height)); | ||
|
||
this.offScreen = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | ||
this.backgroundBuffer = offScreen.getGraphics(); | ||
|
||
this.pack(); | ||
|
||
this.setVisible(true); | ||
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | ||
} | ||
|
||
public Graphics getBackgroundBuffer() | ||
{ | ||
return this.backgroundBuffer; | ||
} | ||
public void render() | ||
{ | ||
Graphics g = this.getContentPane().getGraphics(); | ||
g.drawImage(this.offScreen, 0, 0, this.getWidth(), this.getHeight(), null); | ||
|
||
this.backgroundBuffer.setColor(Color.WHITE); | ||
this.backgroundBuffer.fillRect(0, 0, this.getWidth(), this.getHeight()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main; | ||
|
||
import core.ViewController; | ||
import gui.*; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
if (args[0].equals("m1")) { | ||
System.out.println("Method 1"); | ||
new ViewController(new RendererV1()); | ||
} else if (args[0].equals("m2")) { | ||
System.out.println("Method 2"); | ||
} if (args[0].equals("m3")) { | ||
System.out.println("Method 3"); | ||
} | ||
} | ||
|
||
} |