Class Pict
- java.lang.Object
-
- Pict
-
public class Pict extends java.lang.Object
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; myPixels = Pict.fileToPixelsBW( "https://www.radford.edu/~itec120/2012fall-ibarland/Lectures/lect01-ufo/muse.jpeg" ); Pict.displayPixels( myPixels ); // Bring up a window with the image. myPixels[13][23] = 255; // Set location (13,23) to white. myPixels[14][23] = 0; // Set location (14,23) to black. Pict.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 useimageToPixelsColor
, which will return an array ofjava.awt.Color
objects. See the Java API for details, but the most useful color methods aregetRed()
,setRed(int)
, and similarly for green and blue. To display a color image (if you have a 2-D array ofColor
s), calldisplayPixels( Color[][] )
.
-
-
Constructor Summary
Constructors Constructor Description Pict()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
displayPixels(int[][] pixels)
Given an array of grayscale values in [0,256), draw it as a black-and-white image.static void
displayPixels(java.awt.Color[][] pixels)
Given an array of Colors, draw it as a Color 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.
-
-
-
Method Detail
-
displayPixels
public static void displayPixels(int[][] pixels)
Given an array of grayscale values in [0,256), draw it as a black-and-white image. (Of course, subsequent changes to the array won't retroactively change this image.)- Parameters:
pixels
- A 2-D array of grayscale values in [0,256).
-
displayPixels
public static void displayPixels(java.awt.Color[][] pixels)
Given an array of Colors, draw it as a Color image. (Of course, subsequent changes to the array won't retroactively change this image.)- Parameters:
pixels
- A 2-D array of java.awt.Colors.
-
fileToPixelsBW
public static int[][] fileToPixelsBW(java.lang.String filenameOrURL)
Given a filename or URL, return a 2-D array of grayscale values in [0,256).- Parameters:
filenameOrURL
- A string like "H:\MyFile.jpeg" or "https://www.radford.edu/someUser/somePict.jpg"- Returns:
- The 2-D array of grayscale values representing the indicated picture. Will cause a run-time exception if the file/URL can't be opened.
-
fileToPixelsColor
public static java.awt.Color[][] fileToPixelsColor(java.lang.String filenameOrURL)
Given a filename or URL, return a 2-D array of java.awt.Colors.- Parameters:
filenameOrURL
- A string like "H:\MyFile.jpeg" or "https://www.radford.edu/someUser/somePict.jpg"- Returns:
- The 2-D array of grayscale values representing the indicated picture. Will cause a run-time exception if the file/URL can't be opened.
-
main
public 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.
-
-