import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Vector;
import javax.swing.JFrame;
public class EasyRenderFrame extends JFrame implements Runnable {
/**
*
*/
private static final long serialVersionUID = 2079014996055778535L;
private static final int NUMBER_OF_OBJECTS = 1; // Anzahl der Kreise
private static final double UPDATE_HERTZ = 30;
private static final double TIME_BETWEEN_UPDATES = 1000000000 / UPDATE_HERTZ;
private static final int MAX_UPDATES_BEFORE_RENDER = 100;
private static final double TARGET_FPS = 60;
private static final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
private int width, height;
private Canvas canvas;
private BufferStrategy bufferStrategy;
private BufferedImage offScreenImage;
private Graphics g;
private Graphics2D g2d;
private int fps = 0;
private int frameCount = 0;
private boolean running;
private boolean pause;
private Color[] colors = new Color[] {Color.black, Color.blue, Color.green, Color.magenta, Color.orange, Color.pink, Color.red};
private Vector<Sprite> sprites;
public EasyRenderFrame(int width, int height) {
this.width = width;
this.height = height;
initialize();
setUp();
Thread animator = new Thread(this);
animator.start();
}
private void initialize() {
setIgnoreRepaint(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Easy Render");
canvas = new Canvas();
canvas.setSize(width, height);
canvas.setIgnoreRepaint(true);
add(canvas);
pack();
setLocationRelativeTo(null);
setVisible(true);
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
offScreenImage = gc.createCompatibleImage(width, height);
g = null;
g2d = null;
running = true;
pause = false;
}
private void setUp() {
sprites = new Vector<>();
Random rand = new Random();
for(int i = 0; i < NUMBER_OF_OBJECTS; i++) {
int x = rand.nextInt(539) + 31;
int y = rand.nextInt(339) + 31;
int width = rand.nextInt(20) + 10;
int height = rand.nextInt(20) + 10;
// double xSpeed = rand.nextDouble() * 6; // Random Geschwindigkeiten, oder selber aussuchen!
// double ySpeed = rand.nextDouble() * 6;
double xSpeed = 5;
double ySpeed = 0;
Color color = colors[rand.nextInt(colors.length)];
sprites.add(new Sprite(x, y, width, height, xSpeed, ySpeed, color));
}
}
private void update() {
for(int i = 0; i < sprites.size(); i++) {
Sprite a = sprites.get(i);
a.updateLogic();
a.updatePosition();
}
}
private void render(float interpolation) {
try {
g2d = offScreenImage.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, width, height);
// User Objects
for(int i = 0; i < sprites.size(); i++) {
sprites.get(i).drawSprite(g2d, interpolation);
}
// User Objects end
g = bufferStrategy.getDrawGraphics();
g.drawImage(offScreenImage, 0, 0, null);
if(!bufferStrategy.contentsLost()) {
bufferStrategy.show();
}
} finally {
if( g != null ) {
g.dispose();
}
if( g2d != null ) {
g2d.dispose();
}
}
}
@Override
public void run() {
double lastUpdateTime = System.nanoTime();
double lastRenderTime = System.nanoTime();
int lastSecondTime = (int) (lastUpdateTime / 1000000000);
while(running) {
double now = System.nanoTime();
int updateCount = 0;
if(!pause) {
while(now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER) {
update();
lastUpdateTime += TIME_BETWEEN_UPDATES;
updateCount++;
}
if (now - lastUpdateTime > TIME_BETWEEN_UPDATES) {
lastUpdateTime = now - TIME_BETWEEN_UPDATES;
}
float interpolation = Math.min(1.0f, (float) ((now - lastUpdateTime) / TIME_BETWEEN_UPDATES) );
render(interpolation);
lastRenderTime = now;
int thisSecond = (int) (lastUpdateTime / 1000000000);
if (thisSecond > lastSecondTime) {
fps = frameCount;
frameCount = 0;
lastSecondTime = thisSecond;
}
// while (now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS && now - lastUpdateTime < TIME_BETWEEN_UPDATES) {
// Thread.yield();
//
// //This stops the app from consuming all your CPU. It makes this slightly less accurate, but is worth it.
// //You can remove this line and it will still work (better), your CPU just climbs on certain OSes.
// //FYI on some OS's this can cause pretty bad stuttering. Scroll down and have a look at different peoples' solutions to this.
// try {
// Thread.sleep(10);
// } catch(Exception e) {}
//
// now = System.nanoTime();
// }
} else {
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
new EasyRenderFrame(600, 400);
}
}