It's not showing the image in the terminal window or the jframe.
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{
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) {
//Create a BufferedImage that will represent our view.
view = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
camera = new Rectangle(0, 0, width, height);
camera.x = -100;
camera.y = -30;
//Create an array for pixels
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();
for(int y = 0; y < image.getHeight(); y++)
for(int x = 0; x < image.getWidth(); x++)
for(int yZoomPosition = 0; yZoomPosition <yZoom; yZoomPosition++)
for(int xZoomPosition = 0; xZoomPosition < xZoom; xZoomPosition++)
setPixel(imagePixels[x + y * image.getWidth()],
(x * xZoom) + xPosition + xZoomPosition,
((y * yZoom) + yPosition + yZoomPosition));
}
public void renderArray(int[] renderPixels,int renderWidth, int renderHeight,int xPosition, int yPosition, int xZoom, int yZoom){
}
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;
}
}
public void renderRectangle(Rectangle rectangle, int xZoom, int yZoom){
}
}
public class Rectangle{
public int x,y,w,h;
private int[] pixels;
Rectangle(int x, int y, int w, int h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
Rectangle(){
this(0,0,0,0);
}
public void generateGraphics(int color){
pixels = new int[w*h];
for(int y = 0; y < h; y++)
for(int x = 0; x < w; x++)
pixels [x + y * w] = color;
}
public int[] getPixels(){
if(pixels != null){
return null;
}
else{
System.out.println("Attempted to retrieve pixels from a Rectangle"
+ " without generated graphics.");
}
return null;
}
}
private Canvas canvas = new Canvas();
private RenderHandler renderer;
private BufferedImage testImage;
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");
}
public void update(){
}
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.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) {
update();
changeInSeconds--;
}
render();
lastTime = now;
}
//Bad loop
// while(true) {
// bufferStrategy = canvas.getBufferStrategy();
// Graphics graphics = bufferStrategy.getDrawGraphics();
// super.paint(graphics);
// //Painting the Backround
// graphics.setColor(Color.black);
// graphics.fillRect(0, 0, getWidth(), getHeight());
// //Painting the Oval
// graphics.setColor(Color.red);
// graphics.fillOval(x, 200, 50, 100);
// graphics.dispose();
// bufferStrategy.show();
// }
}
public static void main(String[] args)
{
Game game = new Game();
Thread gameThread = new Thread(game);
gameThread.start();
}
}