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;
    }
}

}

Dani AI

Generated

Two quick, concrete causes stand out in 's code and explain why the image never appears (and why a later read/exception may stop the loop): the instance field holding the image is never assigned (a local variable shadows it in the constructor), and the renderer overwrites the pixel buffer with random values right before the view is drawn. In other words, the line that is supposed to draw the image is the renderer.renderImage(...) call inside Game.render(), but either the image reference is null or the buffer is being clobbered after the image is written.

Fixes to apply (small, targeted changes):

  • Assign the loaded image to the instance field (do not declare a new local variable). Use a leading slash when the resource is at the classpath root. For example:

    testImage = loadImage("/Grass.jpg");
  • Make loadImage return a BufferedImage that uses an INT_* type (so the cast to DataBufferInt is valid). A robust pattern is to read via getResourceAsStream and convert to TYPE_INT_ARGB:

    private BufferedImage loadImage(String path) throws IOException {
        try (InputStream in = Game.class.getResourceAsStream(path.startsWith("/") ? path : "/" + path)) {
            BufferedImage src = ImageIO.read(in);
            BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics g = dst.createGraphics();
            g.drawImage(src, 0, 0, null);
            g.dispose();
            return dst;
        }
    }
  • Stop overwriting pixel data after drawing the image. Currently RenderHandler.render(...) fills pixels with random colors immediately before drawing the view; that must happen before renderImage/renderRectangle or be removed so the image written into the pixel array is not lost. Also fix the bounds check in setPixel so negative indexes cannot occur:

    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) return;
        int idx = (x - camera.x) + (y - camera.y) * view.getWidth();
        if (idx >= 0 && idx < pixels.length) pixels[idx] = pixel;
    }

Extra notes: avoid calling super.paint(graphics) when using BufferStrategy; ensure canvas.createBufferStrategy(...) runs after the canvas is displayable; check console for NullPointerException or ClassCastException (these will point straight to the image/load issues); and verify the image file is actually on the runtime classpath (or load with a File for quick local testing). These steps address the most likely causes without large structural changes.

Can you clear up which line that you feel should show an image and does not?

I might guess the line with Grass.jpg but as others replied "There is a lot of code without any explanation." I have to agree. I'd also want to know if this code is from https://marcusman.com as well is that yours and so on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.