awt - Java - IndexOutOfBoundsException -
i'm developing application in java, , i've finished writing 3 main class files - i've hit confusing error. i'm @ moment "attempting" draw pixels upon screen pixels array doesn't work.
i'm getting indexoutofboundsexception in render.java though i'm getting warnings or errors in code?
display.java
public class display extends canvas implements runnable { private static final long serialversionuid = 1l; public static final int width = 300; public static final int height = 190; public static final int scale = 3; public static dimension dimension = new dimension(width * scale, height * scale); public static final string title = "untitled project"; public int[] pixels; public boolean isrunning = false; public thread thread; public screen screen; public render render; public bufferedimage img; public display() { screen = new screen(width, height); img = new bufferedimage(width, height, bufferedimage.type_int_rgb); pixels = ((databufferint) img.getraster().getdatabuffer()).getdata(); } public void start(boolean isdebug) { if (isrunning) return; isrunning = true; thread = new thread(this); thread.start(); } public void stop() { if (!isrunning) return; isrunning = false; try { thread.join(); } catch (exception e) { e.printstacktrace(); system.exit(0); } } private void render() { bufferstrategy bs = this.getbufferstrategy(); if (bs == null) { createbufferstrategy(3); return; } screen.render(); (int = 0; < width * height; i++) pixels[i] = screen.pixels[i]; graphics g = bs.getdrawgraphics(); g.drawimage(img, 0, 0, width, height, null); g.dispose(); bs.show(); } private void tick() { } public void run() { while (isrunning) { render(); tick(); } } public static void main(string[] args) { display display = new display(); display.setminimumsize(dimension); display.setmaximumsize(dimension); display.setpreferredsize(dimension); jframe frame = new jframe(); frame.settitle(title); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(display, borderlayout.center); frame.pack(); frame.setlocationrelativeto(null); frame.setresizable(false); frame.setvisible(true); display.start(true); } } render.java
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; pixels = new int[width * height]; } public void draw(render render, int xoffset, int yoffset) { (int y = 0; y < render.height; y++) { int ypix = y + yoffset; (int x = 0; x < render.width; x++) { int xpix = x + xoffset; error --> pixels[xpix + ypix * width] = render.pixels[x + y * render.width]; } } } } screen.java
public class screen extends render { public render render; public random random; public screen(int width, int height) { super(width, height); render = new render(256, 256); random = new random(); (int = 0; < 256 * 256; i++) { render.pixels[i] = random.nextint(); } } public void render() { draw(render, 0, 0); } } error
exception in thread "thread-2" java.lang.arrayindexoutofboundsexception: 57000 @ com.willajhughes.java.graphics.render.draw(render.java:23) @ com.willajhughes.java.graphics.screen.render(screen.java:21) @ com.willajhughes.java.display.render(display.java:76) @ com.willajhughes.java.display.run(display.java:94) @ java.lang.thread.run(unknown source) thanks in advanced!
--
will
your pixels array of of size width * height. you're attempting access width + height * width. may imagine, that's not going go down mr. java.
Comments
Post a Comment