public class ImageResource
extends java.lang.Object
ImageResource
class represents an image as a grid of Pixel
objects
and allows access to all of them, using the method pixels
. These pixels can then be
iterated over using a for
loop.
Note, no changes made to the pixels in this image affect the original image opened unless you use
the method save
.
Example usage:
ImageResource image = new ImageResource(); for (Pixel p : image.pixels()) { int red = p.getRed(); int green = p.getGreen(); int blue = p.getBlue(); int average = (red + green + blue) / 3; p.setRed(average); p.setGreen(average); p.setBlue(average); } image.draw();
This is open-source software released under the terms of the GPL (http://www.gnu.org/licenses/gpl.html).
Constructor and Description |
---|
ImageResource()
Create an
ImageResource object that represents the file chosen by the user using
a file selection dialog box. |
ImageResource(java.io.File file)
Create an
ImageResource object from a file given as a parameter. |
ImageResource(ImageResource other)
Create an
ImageResource object that is a copy of another image. |
ImageResource(int width,
int height)
Create an
ImageResource object whose size is the width and height passed as
parameters and whose pixels are all black. |
ImageResource(java.lang.String fileName)
Create an
ImageResource object from the file name passed as a parameter. |
Modifier and Type | Method and Description |
---|---|
void |
draw()
Displays this image in a separate window.
|
java.lang.String |
getFileName()
Returns the file name associated with this image.
|
int |
getHeight()
Returns the height of the image in pixels.
|
Pixel |
getPixel(int x,
int y)
Returns the pixel at the (x, y) coordinates passed as a parameter.
|
int |
getWidth()
Returns the width of the image in pixels.
|
java.lang.Iterable<Pixel> |
pixels()
Allow access to this image one pixel at a time.
|
void |
save()
Saves the image as a JPEG using its current file name or opens a file selection dialog box to
allow the user to choose a name if no file name set (for example if this image was created as
a blank sized image).
|
void |
saveAs()
Saves the image as a JPEG by opening a file selection dialog box to allow the user to choose
the new name for the file.
|
void |
setFileName(java.lang.String name)
Resets the file name associated with this image.
|
void |
setPixel(int x,
int y,
Pixel p)
Resets the pixel at the given (x, y) coordinates but does not redraw it.
|
java.lang.String |
toString()
Returns a string representation of the image (file name, width, and height).
|
public ImageResource()
ImageResource
object that represents the file chosen by the user using
a file selection dialog box.exception
- if no file is selected by the userpublic ImageResource(int width, int height)
ImageResource
object whose size is the width and height passed as
parameters and whose pixels are all black.width
- the width of the image in pixelsheight
- the height of the image in pixelsexception
- if the width or height are not positive valuespublic ImageResource(java.lang.String fileName)
ImageResource
object from the file name passed as a parameter.fileName
- the name of the fileexception
- if the file cannot be accessed or is not in an image formatpublic ImageResource(java.io.File file)
ImageResource
object from a file given as a parameter.file
- the file representing an imageexception
- if the file cannot be accessed or is not in an image formatpublic ImageResource(ImageResource other)
ImageResource
object that is a copy of another image.other
- the original image being copiedpublic int getWidth()
public int getHeight()
public java.lang.Iterable<Pixel> pixels()
Iterable
that will allow access to each pixel in this imagepublic void draw()
public java.lang.String getFileName()
public void setFileName(java.lang.String name)
name
- the new name for the filepublic Pixel getPixel(int x, int y)
x
- the column position of the pixely
- the row position of the pixelpublic void setPixel(int x, int y, Pixel p)
x
- the column position of the pixely
- the row position of the pixelp
- the new pixel values to usepublic java.lang.String toString()
toString
in class java.lang.Object
public void save()
exception
- if the current filename cannot be accessed for savingpublic void saveAs()
exception
- if no file is selected by the user