Results 1 to 4 of 4
Thread: My picture is out of bounds?
- 10-27-2010, 03:34 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
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)
- 10-27-2010, 04:30 AM #2
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
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.
- 10-28-2010, 09:43 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
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?
- 10-29-2010, 01:40 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
{
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
-
Bounds problem...
By licka in forum New To JavaReplies: 6Last Post: 10-21-2010, 09:38 PM -
array going out of bounds?
By jabo in forum New To JavaReplies: 9Last Post: 04-02-2010, 10:08 AM -
Array Index out of bounds
By leapinlizard in forum New To JavaReplies: 5Last Post: 04-29-2009, 05:11 AM -
[SOLVED] out of bounds exception help
By soxfan714 in forum New To JavaReplies: 21Last Post: 11-11-2008, 08:16 AM -
why is my array out of bounds?
By Phobos0001 in forum New To JavaReplies: 3Last Post: 03-24-2008, 01:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks