1

Hello fellow hydrogen based life form. I learning how to make 3-Dimensional gaems from a top secret website (www.youtube.com) and I was learning from a very nice Youtuber. My project looks like this:

There are 3 classes: Main, Screen and Render I get this exception :( :

Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 65600
at Render.Draw(Render.java:20)
at Screen.render(Screen.java:19)
at Main.render(Main.java:74)
at Main.run(Main.java:59)
at java.lang.Thread.run(Unknown Source)

I shall upload my code here too below this. Please help me become good at this.

Main class:

import java.awt.Canvas;  
import java.awt.Graphics;  
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;  

public class Main extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;

public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Nexus Overload";

private Thread t;
private boolean Running = false;

@SuppressWarnings("unused")
private Render ren;
private Screen s;

private BufferedImage img;
private BufferStrategy bs;
private int[] pixels;

public Main() {
    s = new Screen(WIDTH, HEIGHT);
    img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
}

private void start() {
    if (Running)
        return;
    Running = true;

    t = new Thread(this);
    t.start();
}

@SuppressWarnings("unused")
private void stop() {
    if (!Running)
        return;
    Running = false;
    try {
        t.join();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}

public void run() {
    while (Running) {
        tick();
        render();
    }
}

private void tick() {

}

private void render() {
    bs = this.getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return; 
    }

    s.render();

    for (int i = 0; i < WIDTH * HEIGHT; i++) {
        pixels[i] = s.pixels[i];
    }

    Graphics g = bs.getDrawGraphics();
    g.drawImage(img, WIDTH, HEIGHT, null);
    g.dispose();
    bs.show();

}

public static void main(String[] args) {
    Main m = new Main();

    JFrame frame = new JFrame();
    frame.getContentPane().add(m);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(350, 100, WIDTH, HEIGHT);
    frame.setTitle(TITLE);
    frame.setVisible(true);

    m.start();
}
}

Render class:

public class Render {
public final int width;
public final int height;
public final int[] pixels;

public Render(int width, int height) {
    this.width = width;
    this.height = height;
    this.pixels = new int[width * height];
}

public void Draw (Render ren, int xOffset, int yOffset) {
    for (int y = 0; y < ren.height; y++) {
        int yPix = y + yOffset;

        for (int x = 0; x < ren.width; x++) {
            int xPix = x + xOffset;

            pixels[xPix + yPix * width] = ren.pixels[xPix + yPix * width];
        }
    }
}
}

Screen class:

import java.util.Random;

public class Screen extends Render{

private Render ren;

public Screen(int width, int height) {
    super(width, height);

    Random r = new Random();

    ren = new Render(256, 256);
    for (int i = 0; i < 256 * 256; i++) {
        ren.pixels[i] = r.nextInt();
    }
}

public void render() {
    Draw(ren, 0, 0);
}

}

For those who want image with eclipse debugging, here: With variables tab selected: enter image description here

John Smith
  • 37
  • 1
  • 12

1 Answers1

0

Your exception is pointing us to the Draw method in the Render class (NB: Java conventions state that method names should be lower case so this method should actually be called draw(Render ren, int xOffset, int yOffset). I would first try and set the int y in the outer for loop to 1 to see if that helps, you may also been to do the same for int x in the inner for loop...

coderwurst
  • 161
  • 3
  • 14