Results 1 to 4 of 4
  1. #1
    ThatPinkSock is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Exclamation My picture is out of bounds?

    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import java.text.*;
    import java.util.*;
    import java.util.List; // resolves problem with java.awt.List and java.util.List

    public class Picture extends SimplePicture
    {


    public static void main(String[] args)
    {

    String fileName = FileChooser.pickAFile();
    Picture pictObj = new Picture(fileName);
    pictObj.makeGrid(2);
    pictObj.show();

    }


    public Picture makeGrid(int size)
    {
    Picture pictObj = new Picture(this.getHeight()*size, this.getWidth()*size);
    //declares both the source pixel and target pixel and sets it to null
    Pixel sourcePixel = null;
    Pixel targetPixel = null;
    int targetX = 0;
    int targetY = 0;
    //loops through the source picture columns
    for (int sourceX = 0; sourceX < pictObj.getWidth(); sourceX++)
    {
    //loops through the source picture rows
    for (int sourceY = 0; sourceY < pictObj.getHeight(); sourceY++)
    {
    //set sourcepixel = correct pixel
    sourcePixel = this.getPixel(sourceX,sourceY);
    //gets the 3 colors
    for (int indexX = 0; indexX < size; indexX++)
    {
    for (int indexY = 0; indexY < size; indexY++)
    {


    targetX = (sourceX * size);
    targetY = (sourceY * size);
    targetPixel = pictObj.getPixel(targetX,targetY);
    //set pixel = correct pixel
    int red = targetPixel.getRed();
    int green = targetPixel.getGreen();
    int blue = targetPixel.getBlue();

    //{//if row is even
    if (sourceY%2==1 ) {
    // negates the colors


    targetPixel.setColor(new Color(255 - red, 255 - green, 255
    - blue));
    //gets the source and target pixels
    }
    else targetPixel.setColor(sourcePixel.getColor());

    //}

    }
    }
    }
    }

    return pictObj;
    }

    }


    Someone else was on these forums with this code and I was curious to see if anyone figured out the problem. I was working on it for a few hours and I couldn't figure it out. This error always comes up:

    java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
    at sun.awt.image.IntegerInterleavedRaster.getDataElem ents(Unknown Source)
    at java.awt.image.BufferedImage.getRGB(Unknown Source)
    at SimplePicture.getBasicPixel(SimplePicture.java:300 )
    at Pixel.getRed(Pixel.java:94)
    at Picture.makeGrid(Picture.java:180)
    at Picture.main(Picture.java:147)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

  2. #2
    ThatPinkSock is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default okay I removed the error but...

    now I'm having trouble applying the method to the picture. When I run the program I want the picture to be in a grid and have every other row negated.

    public class Picture extends SimplePicture
    {


    public static void main(String[] args)
    {

    String fileName = FileChooser.pickAFile();
    Picture pictObj = new Picture(fileName);
    pictObj.makeGrid(3);
    pictObj.show();

    }


    public Picture makeGrid(int size)
    {
    Picture pictObj = new Picture(this.getWidth()*size,
    this.getHeight()*size);
    //declares both the source pixel and target pixel and sets it to null
    Pixel sourcePixel = null;
    Pixel targetPixel = null;
    int targetX = 0;
    int targetY = 0;
    //loops through the source picture columns
    for (int sourceX = 0; sourceX < this.getWidth(); sourceX++)
    {
    //loops through the source picture rows
    for (int sourceY = 0; sourceY < this.getHeight(); sourceY++)
    {
    //set sourcepixel = correct pixel
    sourcePixel = this.getPixel(sourceX,sourceY);
    //gets the 3 colors
    for (int indexX = 0; indexX < size; indexX++)
    {
    for (int indexY = 0; indexY < size; indexY++)
    {


    targetX = sourceX + this.getWidth() * indexX;
    targetY = sourceY + this.getHeight()* indexY;
    //System.out.println(" target pixel "+ targetX + " "+ targetY);
    targetPixel = pictObj.getPixel(targetX,targetY);
    //set pixel = correct pixel
    int red = sourcePixel.getRed();
    int green = sourcePixel.getGreen();
    int blue = sourcePixel.getBlue();

    //if row is even
    if (sourceY%2==1 ) {
    // negates the colors


    targetPixel.setColor(new Color(255 - red, 255 - green, 255- blue));
    //gets the source and target pixels
    }
    else targetPixel.setColor(sourcePixel.getColor());



    }
    }
    }
    }
    return pictObj;
    }

    }

    Help! When I run it - the untouched picture appears.

  3. #3
    Gpcompscinoob is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default any solution to the problem?

    hey im working on the same assignment and im stuck with the same problem as you did you figure out what you were doing wrong?

  4. #4
    ThatPinkSock is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    {
    String fileName = FileChooser.pickAFile();
    Picture origPicture = new Picture(fileName);
    Picture scaledPicture = origPicture.makeGrid();
    scaledPicture.show();

    }

    public Picture makeGrid(int size)
    {
    Picture pictObj = new Picture(this.getWidth()*size,
    this.getHeight()*size);
    Pixel sourcePixel = null;
    Pixel targetPixel = null;
    int targetX = 0;
    int targetY = 0;
    for (int sourceX = 0; sourceX < this.getWidth(); sourceX++)
    {
    for (int sourceY = 0; sourceY < this.getHeight(); sourceY++)
    {
    sourcePixel = this.getPixel(sourceX,sourceY);
    for (int indexX = 0; indexX < size; indexX++)
    {
    for (int indexY = 0; indexY < size; indexY++)
    {
    targetX = sourceX + this.getWidth() * indexX;
    targetY = sourceY + this.getHeight()* indexY;
    targetPixel = pictObj.getPixel(targetX,targetY);
    int red = sourcePixel.getRed();
    int green = sourcePixel.getGreen();
    int blue = sourcePixel.getBlue();
    if (indexY%2==1 ) {
    targetPixel.setColor(new Color(255 - red, 255 - green, 255
    - blue));
    }
    else
    targetPixel.setColor(sourcePixel.getColor());
    }
    }
    }
    }
    return pictObj;
    }

Similar Threads

  1. Bounds problem...
    By licka in forum New To Java
    Replies: 6
    Last Post: 10-21-2010, 09:38 PM
  2. array going out of bounds?
    By jabo in forum New To Java
    Replies: 9
    Last Post: 04-02-2010, 10:08 AM
  3. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 05:11 AM
  4. [SOLVED] out of bounds exception help
    By soxfan714 in forum New To Java
    Replies: 21
    Last Post: 11-11-2008, 08:16 AM
  5. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 01:20 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •