The Pixel and Photograph classes

I have been given a project involving those and for some reason I can't understand how this works.I need to be able to duplicate a picture exactly and return a separate copy of the same data. any protips?
[226 byte] By [Yin_Absola] at [2007-11-27 9:35:00]
# 1
> ...> any protips?Probably not since there are no Pixel and Photograph classes in the standard Java API.
prometheuzza at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 2
hmm...how about using this link as a guide for helping me to answer my question? http://www.cs.umd.edu/class/summer2007/cmsc131/Projects/P3/proj3.html
Yin_Absola at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 3
Oh great. Just and the "do my homework" posts for the Spring term fade away, along comes the summer session :-(
BigDaddyLoveHandlesa at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 4

> hmm...

>

> how about using this link as a guide for helping me

> to answer my question?

>

> http://www.cs.umd.edu/class/summer2007/cmsc131/Project

> s/P3/proj3.html

How about asking a specific question instead of expecting us to study your homework assignment?

hunter9000a at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 5

I would never ask for help if I wasn't so frigging confused.

I did the first two projects without any help btu no one I know knows Java so I am stuck going here as a last resort. i really do not know how to achieve any of the stuff I need to reconstruct a picture. I wanted help just reproducing the same pic as a full copy that is constructed pixel by pixel that also collects the color information . I can do the rest if I get this foothold.

Yin_Absola at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 6

I need to be able to:

Extract the data (width, height) of the original,

Extract data for each pixel (Red,blue,green, and position)

Translate it onto a new blank photograph

return the new full picture

From there I can do the manipulations necessary to construct the remaining scenarios.

Yin_Absola at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 7

> I would never ask for help if I wasn't so frigging

> confused.

>

> I did the first two projects without any help btu no

> one I know knows Java so I am stuck going here as a

> last resort. i really do not know how to achieve any

> of the stuff I need to reconstruct a picture. I

> wanted help just reproducing the same pic as a full

> copy that is constructed pixel by pixel that also

> collects the color information . I can do the rest if

> I get this foothold.

If you have no idea how to finish a school assignment which uses classes outside of the standard Java classes, this is not the place to ask questions: you should ask them to your teacher/professor or TA.

prometheuzza at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 8
What are the methods of Photograph and Pixel?
BigDaddyLoveHandlesa at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 9
http://www.cs.umd.edu/class/summer2007/cmsc131/Projects/P3/doc/index.htmlit's mostly getters and setters actually
Yin_Absola at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 10

> I need to be able to:

>

> Extract the data (width, height) of the original,

> Extract data for each pixel (Red,blue,green, and

> position)

> Translate it onto a new blank photograph

> return the new full picture

>

> From there I can do the manipulations necessary to

> construct the remaining scenarios.

Have you studied the Pixel and Photograph apis like the assignment says? If so, what specific problem are you having using them?

(btw, this teacher's supposed to be teaching you how to manipulate images, but he uses jpg for cartoons and screenshots? blech)

hunter9000a at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 11

> http://www.cs.umd.edu/class/summer2007/cmsc131/Projects/P3/doc/index.html

>

> it's mostly getters and setters actually

Can you use those getters and setters to do any of the following?

Extract the data (width, height) of the original,

Extract data for each pixel (Red,blue,green, and position)

Translate it onto a new blank photograph

return the new full picture

BigDaddyLoveHandlesa at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 12

Extract data for each pixel (Red,blue,green, and position)->

Translate it onto a new blank photograph

that's my main problem... I think the rest is just minor confusion.

I'll show you what I have so far... it''s going to be messy but

public static Photograph copy(Photograph photo) {

Photograph copyPhoto = new Photograph(photo.getHeight(), photo.getWidth());

int xPix, yPix;

int xPixMax=photo.getHeight();

int yPixMax=photo.getWidth();

int red,blue,green;

Pixel pixel=new Pixel (red,blue,green);

for (xPix = 1; xPix <= xPixMax; xPix++){

for (yPix = 1; yPix <= yPixMax; yPix++){

green= photo.getGreen(xPix,yPix);

red= photo.getRed(xPix,yPix);

blue= photo.getBlue(xPix,yPix);

copyPhoto.setPixel(xPix,yPix,pixel);

}

}

return copyPhoto;

Yin_Absola at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 13

> Extract data for each pixel (Red,blue,green, and

> position)->

> Translate it onto a new blank photograph

>

>

> that's my main problem... I think the rest is just

> minor confusion.

>

> I'll show you what I have so far... it''s going to be

> messy but

Do you have a way of viewing the Photograph that method returns? If so you can verify it it's working correctly. If it isn't, then we need to know what's wrong with the results to help you fix it.

hunter9000a at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 14

A couple basic mistakes:

1. The API states: (Photohgraph's getPixel):

Pixel at position (x, y) in the photograph. Note that the origin (0, 0) is located at the upper left corner of the photo.

so x and y coordinates start with 0, not the 1 in your code.

2. This will not work:

Pixel pixel=new Pixel (red,blue,green);

//later:

green= photo.getGreen(xPix,yPix);

red= photo.getRed(xPix,yPix);

blue= photo.getBlue(xPix,yPix);

copyPhoto.setPixel(xPix,yPix,pixel);

assigning to int variables red, green and blue will not change the state of pixel. You need to create a new Pixel object for every tuple of values.

Also: Photograph does not have getRed/getGreen/getBlue methods. Those are Pixel methods, so apply them to a Pixel object, not a Photograph object.

Message was edited by:

BigDaddyLoveHandles

BigDaddyLoveHandlesa at 2007-7-12 23:00:52 > top of Java-index,Java Essentials,New To Java...
# 15

public static Photograph copy(Photograph photo) {

Photograph copyPhoto = new Photograph(photo.getHeight(), photo.getWidth());

int xPix, yPix;

int xPixMax=photo.getHeight();

int yPixMax=photo.getWidth();

int red,blue,green;

Pixel pixel= new Pixel (red,blue,green);

Pixel copyPix= new Pixel (red,blue,green);

for (xPix = 0; xPix <= xPixMax; xPix++){

for (yPix = 0; yPix <= yPixMax; yPix++){

errorgreen= pixel.getGreen(xPix,yPix);

errorred= pixel.getRed(xPix,yPix);

errorblue= pixel.getBlue(xPix,yPix);

errorcopyPix(red, blue, green);

copyPhoto.setPixel(xPix,yPix,copyPix);

}

}

return copyPhoto;

Yin_Absola at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 16
Pixel pixel= new Pixel (red,blue,green);Pixel copyPix= new Pixel (red,blue,green);These are not pixels from the Photograph. How would you get a Pixel from a Photograph?
BigDaddyLoveHandlesa at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 17
I added a photo.getPixel(xPix,yPix); into the nested for loop, but I still don't know how the getGreen and others work as I want to set the same values to the same position in the copyPhoto object that is created by this method.
Yin_Absola at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 18

public static Photograph copy(Photograph photo) {

Photograph copyPhoto = new Photograph(photo.getHeight(), photo.getWidth());

int xPix, yPix;

int xPixMax=photo.getHeight();

int yPixMax=photo.getWidth();

int red,blue,green;

Pixel pixel= new Pixel (red, blue, green); //this is the only remaining error right now.

for (xPix = 0; xPix <= xPixMax; xPix++){

for (yPix = 0; yPix <= yPixMax; yPix++){

photo.getPixel(xPix,yPix);

green=pixel.getGreen();

red= pixel.getRed();

blue= pixel.getBlue();

Pixel copyPix= new Pixel (red,blue,green);

copyPhoto.setPixel(xPix,yPix,copyPix);

}

}

return copyPhoto;

Yin_Absola at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 19
> but I still don't know how the getGreen and others workThey return the value of that color component.
BigDaddyLoveHandlesa at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 20
photo.getPixel(xPix,yPix); According to the API, this method returns a pixel object. Your code ignores the returned value.
BigDaddyLoveHandlesa at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 21
how about...pixel= photo.getPixel(xPix,yPix);still the Pixel pixel= new Pixel(red,blue,green);line needs fixing as it says it might not have been initialized... but when I remove the parameters a lot more errors come up...
Yin_Absola at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 22
> line needs fixing as it says it might not have been initialized... but when I remove the parameters a lot more errors come up...Why do you need to create a pixel there?
BigDaddyLoveHandlesa at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 23
because it is where the data of the pixel is supposed to be copied to, right? otherwise, there is no point in pixel= photo.getPixel(xPix,yPix); which is essential to actually getting a pixel and utilizing it
Yin_Absola at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 24

When you assign to a variable, that overwrites its previous value.

Then this:

Pixel pixel = new Pixel(red, green, blue); //assuming red, green and blue are initialized.

...

pixel= photo.getPixel(xPix,yPix);

Is the same as this:

Pixel pixel = null;

...

pixel= photo.getPixel(xPix,yPix);

Suggestion: define variable when you can give them a value:

Pixel pixel= photo.getPixel(xPix,yPix);

Do you see the difference?

BigDaddyLoveHandlesa at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 25

cool, that fixed that part

but something is wrong with the overall thing

theoretically this should work for everything and no errors come up until I run it.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!

at sun.awt.image.IntegerInterleavedRaster.getDataElements(Unknown Source)

at java.awt.image.BufferedImage.getRGB(Unknown Source)

at photo.Photograph.getPixel(Photograph.java:73)

at editing.PhotoTools.copy(PhotoTools.java:30)

at photo.PhotoSystem.begin(PhotoSystem.java:60)

at editing.Driver.main(Driver.java:13)

Yin_Absola at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 26
well thanks for the help anyways...
Yin_Absola at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 27

> Coordinate out of bounds!

Looking at that stack trace, it's clear that you are calling getPixel with an x or y value that is out of bounds. Suppose you had this array:

double[] v = new double[size];

What for loop would you write to iterate over its elements? Then compare that answer to the code you wrote to iterate over the pixels in the Photograph.

BigDaddyLoveHandlesa at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 28
i'm confused... I thought I had the bounds set properly
Yin_Absola at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 29
> i'm confused... I thought I had the bounds set> properlyapparently not...
jwentinga at 2007-7-21 23:03:24 > top of Java-index,Java Essentials,New To Java...
# 30
> i'm confused... I thought I had the bounds set properlyIf the photograph has a width of 100 pixels, and the first pixel index is 0, what is the last pixel index?
BigDaddyLoveHandlesa at 2007-7-21 23:03:30 > top of Java-index,Java Essentials,New To Java...