Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: [solved] Array out of bounds

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default [solved] Array out of bounds

    Hello again. I am working with canvas and attempting to fill the screen with an area of pixels, of which I can color through layered for loops seen below. My problem is that when targeting the pixels to color, my loops always seem to lead to a higher value than the size of my array.

    The size of pixels is 48600 when printed out, but yet the index 48600 would be out of bounds... i.e. if it had an index of 48600 the size would be 48601.

    Take a look and see if you notice anything... Any hints appreciated

    Game.java
    import java.awt.Canvas;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferInt;
     
    import javax.swing.JFrame;
     
    import audboy.project.graphics.View;
     
    public class Game extends Canvas implements Runnable {
     
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
     
    	public static int width = 300;
    	public static int height = width / 16 * 9;
    	public static int scale = 3;
     
    	private boolean running = false;
     
    	private View view;
    	private JFrame jframe;
    	private Thread thread;
    	private BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    	private int[] pixels = ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
     
    	public Game(){
    		Dimension size = new Dimension(width * scale, height * scale);
    		setPreferredSize(size);
     
    		view = new View(width, height);
     
    		jframe = new JFrame();
    	}
     
    	public synchronized void start(){
    		running = true;
    		thread = new Thread(this, "Display Thread");
    		thread.start();
    	}
     
    	public synchronized void stop(){
    		running = false;
    		try {
    			thread.join();
    		} catch (InterruptedException e){
    			e.printStackTrace();
    		}
    	}
     
    	public void run() {
    		while(running){
    			tick();
    			render();
    		}
    	}
     
    	public void tick(){
     
    	}
     
    	public void render(){
    		BufferStrategy bs = getBufferStrategy();
    		if(bs == null){
    			createBufferStrategy(3);
    			return;
    		}
     
    		view.render();
     
    		for(int i = 0; i < pixels.length; i++){
    			pixels[i] = view.pixels[i];
    		}
     
    		Graphics g = bs.getDrawGraphics();
    		g.drawImage(bi, 0, 0, null);
    		g.dispose();
    		bs.show();
    	}
     
    	public static void main(String[] args){
    		Game game = new Game();
    		game.jframe.setResizable(false);
    		game.jframe.setTitle("Project");
    		game.jframe.add(game);
    		game.jframe.pack();
    		game.jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		game.jframe.setLocationRelativeTo(null);
    		game.jframe.setVisible(true);
     
    		game.start();
    	}
    }

    View.java
    public class View {
     
    	private int width, height;
    	public int[] pixels;
     
    	public View(int width, int height){
    		this.width = width;
    		this.height = height;
    		pixels = new int[width * height];
    	}
     
    	public void render(){
    		for(int y = 0; y < height; y++){
    			for(int x = 0; x < width; y++){
    				pixels[x + y * width] = 0xff00ff; //hex pink
    			}
    		}
    	}
     
    }

    Error:
    Exception in thread "Display Thread" java.lang.ArrayIndexOutOfBoundsException: 48600
    at audboy.project.graphics.View.render(View.java:17)
    at audboy.project.Game.render(Game.java:76)
    at audboy.project.Game.run(Game.java:61)
    at java.lang.Thread.run(Thread.java:680)
    Last edited by AlexHail; August 10th, 2013 at 02:04 PM. Reason: solved


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,290
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Array out of bounds

    Don't think you meant to increment y in the second for loop in View.

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Array out of bounds

    /facepalm. Well that part is solved. But it only fills a quarter block my screen with pink, and I need it to fill the entire applet :< Thanks though

    -----UPDATE-----

    Fixed that too. Thanks

Similar Threads

  1. Array Index Out of bounds?
    By blazeking in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2012, 02:44 AM
  2. [SOLVED] Array Index out of bounds Exception
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2012, 04:03 PM
  3. Can't Fix Array Out of Bounds
    By Apocalypse in forum Collections and Generics
    Replies: 5
    Last Post: October 31st, 2011, 08:37 AM
  4. Array exception out of bounds
    By gabberlt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 17th, 2011, 09:05 AM
  5. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 07:11 AM