java - How do I solve this array out of bounds exception? -
this question has answer here:
hello fellow hydrogen based life form. learning how make 3-dimensional gaems top secret website (www.youtube.com) , learning nice youtuber. project looks this:
there 3 classes: main, screen , render exception :( :
exception in thread "thread-2" java.lang.arrayindexoutofboundsexception: 65600 @ render.draw(render.java:20) @ screen.render(screen.java:19) @ main.render(main.java:74) @ main.run(main.java:59) @ java.lang.thread.run(unknown source) i shall upload code here below this. please me become @ 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(); (int = 0; < 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) { (int y = 0; y < ren.height; y++) { int ypix = y + yoffset; (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); (int = 0; < 256 * 256; i++) { ren.pixels[i] = r.nextint(); } } public void render() { draw(ren, 0, 0); } } for want image eclipse debugging, here:
variables tab selected: 
your exception pointing draw method in render class (nb: java conventions state method names should lower case method should called draw(render ren, int xoffset, int yoffset). first try , set int y in outer loop 1 see if helps, may been same int x in inner loop...
Comments
Post a Comment