java.lang.Object Pict
public class Pict
This utility class contains static methods to translate between images and 2-D arrays.
To convert a image's filename or URL to an array of int:
int[][] myPixels = imageToPixelsBW( "http://www.radford.edu/~itec120/2007fall-ibarland/Lectures/lect01-ufo/muse.jpeg" );
displayPixels( myPixels ); // Bring up a window with the image.
myPixels[13][23] = 255; // Set location (13,23) to white
displayPixels( myPixels ); // Look for the modified pixel.
Note that once you have displayed an image, modifying the array won't
retroactively change that image; you'll have to call displayPixels
again
after making any modifications to see a new image.
Optional/Challenge:
If you want to deal with color images, you can use imageToPixelsColor
,
which will return an array of java.awt.Color
objects. See the Java API
for details, but the most useful color methods are getRed()
, setRed(int)
,
and similarly for green and blue. To display a color image (if you have
a 2-D array of Color
s), call displayPixels( Color[][] )
.
Constructor Summary | |
---|---|
Pict()
|
Method Summary | |
---|---|
static void |
displayPixels(java.awt.Color[][] pixels)
Given an array of Colors, draw it as a Color image. |
static void |
displayPixels(int[][] pixels)
Given an array of grayscale values in [0,256), draw it as a black-and-white image. |
static int[][] |
fileToPixelsBW(java.lang.String filenameOrURL)
Given a filename or URL, return a 2-D array of grayscale values in [0,256). |
static java.awt.Color[][] |
fileToPixelsColor(java.lang.String filenameOrURL)
Given a filename or URL, return a 2-D array of java.awt.Colors. |
static void |
main(java.lang.String[] args)
A test method, which just displays three images: A color version, a black-and-white version, and a tiny 4x3 image. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Pict()
Method Detail |
---|
public static void displayPixels(java.awt.Color[][] pixels)
pixels
- A 2-D array of java.awt.Colors.public static void displayPixels(int[][] pixels)
pixels
- A 2-D array of grayscale values in [0,256).public static int[][] fileToPixelsBW(java.lang.String filenameOrURL)
filenameOrURL
- A string like "H:\MyFile.jpeg" or "http://www.radford.edu/someUser/somePict.jpg"
public static java.awt.Color[][] fileToPixelsColor(java.lang.String filenameOrURL)
filenameOrURL
- A string like "H:\MyFile.jpeg" or "http://www.radford.edu/someUser/somePict.jpg"
public static void main(java.lang.String[] args)