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 2 of 2

Thread: ArrayIndexOutOfBounds Help

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default ArrayIndexOutOfBounds Help

    Can anyone help me and let me know why this is going out of bounds? Its throwing the exception on line 41
    The program is suppose to take a GImage and rotate it 90 degrees.
    /*
     * Mat2670 Homework 4 EX 5
     * 
     * Connor Moore
     * 03/07/2013
     * 
     * File: fliphorizontal.java
     * This program takes a .gif or .jpg image flips the image
     * horizontally and displays the image after the flip.
     */
    package mat2670;
     
    import acm.graphics.*;
    import acm.program.*;
    import javax.swing.*;
     
    public class RotateLeft extends GraphicsProgram {
     
        public static void main(String[] args) {
            (new RotateLeft()).start();
        }
     
        public void run() {
            //Declares an image to use
            JFileChooser chooser = new JFileChooser();
            String firefox = "firefox.gif";
     
            GImage image = new GImage(firefox);
     
            //Flips the image then shows it to the user.
            add(rotateLeft(image));
        }
    //Function that takes a Gimage and flips it horizontally
     
        private GImage rotateLeft(GImage image) {
            int[][] array = image.getPixelArray();
            int height = array.length;
            int width = array[0].length;
            for (int row = 0; row < width; row++) {
                for (int col = 0; col < height; col++) {
                    int temp = array[col][row];
                    array[row][col] = temp;            
                }
            }
            return new GImage(array);
        }
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: ArrayIndexOutOfBounds Help

    Step back from the program a minute and think about what you are trying to do.

    If the number of columns is not equal to the number of rows, you must create a new array.

    For example, suppose the original has 600 rows and 800 columns, Then the rotated image will have 800 rows and 600 columns. You can not simply exchange values the elements of the rows and columns of the original array. Period.

    Now go back to your program and see why it absolutely must create an array out-of-bounds exception for a non-square image. Every single time.

    So...

    Create a new array such that the height of the new array is equal to the width of the original and the width of the new array is equal to the height of the original.

    Then copy.


    Cheers!

    Z

Similar Threads

  1. I need help with ArrayIndexOutOfBounds exception!
    By jameschristian in forum Exceptions
    Replies: 6
    Last Post: November 3rd, 2012, 01:32 AM