Class 1
import java.awt.Canvas;
import java.awt.Color;
import java.awt.image.BufferStrategy;
import java.awt.Graphics;
import java.lang.Runnable;
import java.lang.Thread;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
public class Game extends JFrame implements Runnable{
private Canvas canvas = new Canvas();
private RenderHandler renderer;
private BufferedImage testImage;
private Rectangle testRectangle = new Rectangle(30, 90, 100, 100);
public Game() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0,0, 1000, 800);
setLocationRelativeTo(null);
add(canvas);
setVisible(true);
canvas.createBufferStrategy(3);
renderer = new RenderHandler(getWidth(), getHeight());
BufferedImage testImage = loadImage("Grass.jpg");
testRectangle.generateGraphics(12234);
}
private BufferedImage loadImage(String path){
try{
BufferedImage loadedImage = ImageIO.read(Game.class.getResource(path));
BufferedImage formattedImage = new BufferedImage(loadedImage.getWidth(),
loadedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
formattedImage.getGraphics().drawImage(loadedImage, 0, 0, null);
return formattedImage;
}
catch(IOException exception){
exception.printStackTrace();
return null;
}
}
public void render(){
BufferStrategy bufferStrategy = canvas.getBufferStrategy();
Graphics graphics = bufferStrategy.getDrawGraphics();
super.paint(graphics);
renderer.renderImage(testImage, 0, 0, 5, 5);
renderer.renderRectangle(testRectangle, 1, 1);
renderer.render(graphics);
graphics.dispose();
bufferStrategy.show();
}
public void run(){
BufferStrategy bufferStrategy = canvas.getBufferStrategy();
int i = 0;
int x = 0;
long lastTime = System.nanoTime(); //long 2^63
double nanoSecondConversion = 1000000000.0 / 60; //60 frames per second
double changeInSeconds = 0;
while(true){
long now = System.nanoTime();
changeInSeconds += (now - lastTime) / nanoSecondConversion;
while(changeInSeconds >= 1) {
changeInSeconds--;
}
render();
lastTime = now;
}
}
public static void main(String[] args){
Game game = new Game();
Thread gameThread = new Thread(game);
gameThread.start();
}
}
Class 2
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
public class RenderHandler{
private BufferedImage view;
private Rectangle camera;
private int[] pixels;
public RenderHandler(int width, int height) {
view = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
camera = new Rectangle(0, 0, width, height);
camera.x = -100;
camera.y = -30;
pixels = ((DataBufferInt) view.getRaster().getDataBuffer()).getData();
}
public void render(Graphics graphics){
for(int index = 0; index < pixels.length; index++) {
pixels[index] = (int)(Math.random() * 0xFFFFFF);
}
graphics.drawImage(view, 0, 0, view.getWidth(), view.getHeight(), null);
}
public void renderImage(BufferedImage image, int xPosition, int yPosition, int xZoom, int yZoom){
int[] imagePixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
renderArray(imagePixels, image.getWidth(), image.getHeight(),xPosition, yPosition, xZoom, yZoom);
}
public void renderRectangle(Rectangle rectangle, int xZoom, int yZoom){
int[] rectanglePixels = rectangle.getPixels();
if(rectanglePixels != null){
renderArray(rectanglePixels, rectangle.w, rectangle.h, rectangle.x, rectangle.y, xZoom, yZoom);
}
}
public void renderArray(int[] renderPixels,int renderWidth, int renderHeight,int xPosition,
int yPosition, int xZoom, int yZoom){
for(int y = 0; y < renderHeight; y++)
for(int x = 0; x < renderWidth; x++)
for(int yZoomPosition = 0; yZoomPosition <yZoom; yZoomPosition++)
for(int xZoomPosition = 0; xZoomPosition < xZoom; xZoomPosition++)
setPixel(renderPixels[x + y * renderWidth], (x * xZoom) + xPosition + xZoomPosition, ((y * yZoom) + yPosition + yZoomPosition));
}
private void setPixel(int pixel, int x, int y){
if(x >= camera.x && y >= camera.y && x <= camera.x + camera.w && y <= camera.y + camera.h){
}
int pixelIndex = (x - camera.x) + (y - camera.y) * view.getWidth();
if(pixels.length > pixelIndex){
pixels[pixelIndex] = pixel;
}
}
}