Manipulating Pictures in Java- I
As we know, pictures are very popular media, used for communication purposes. To understand picture manipulation, you must be familiar with java arrays, metrics, pixels, colors and type of images used to store and display. You are also require to read my previous article written about bookClasses from here.
An interface describing digital picture with the name DigitalPicture.java is available in the code. Methods defined in this interface are as follows.
Method | Description |
String getFileName() | Get file name |
String getTitle() | get the title of the picture |
void setTitle(String title) | set the title of the picture |
int getWidth() | get the width of the picture in pixels |
int getHeight() | get the height of the picture in pixels |
Image getImage() | get the image from the picture |
BufferedImage getBufferedImage() | get the buffered image |
int getBasicPixel(int x, int y) | get the pixel information as an int |
setBasicPixel(int x, int y, int rgb) | set the pixel information |
Pixel getPixel(int x, int y) | get the pixel information as an object |
void load(Image image) | load the image into the picture |
boolean load(String fileName) | load the picture from a file |
void show() | show the picture |
SimplePicture.java class is also available which implements DigitalPicture interface. The purpose of simple picture class is to load and manipulate picture by using pixels. A simple picture may have an associated file name, a title, width and height. SimplePicture class uses a BufferedImage to hold pixels and can display picture using PictureFrame ( a JFrame). Following methods are available in SimplePicture class.
Method | Description |
SimplePicture() | Constructor with no arguments |
SimplePicture(String fileName) | load the picture into the buffered image |
SimplePicture(int width, int height) | constructor that takes the width and height desired for a picture and creates a buffered image of that size |
SimplePicture(int width, int height, Color theColor) | A constructor that takes the width and height desired for a picture and creates a buffered image of that size. It also takes the color to use for the background of the picture. |
SimplePicture(SimplePicture copyPicture) | Constructor that takes a picture to copy information copyPicture |
SimplePicture(BufferedImage image) | onstructor that takes a buffered image |
String getExtension() | get the extension for this picture |
copyPicture(SimplePicture sourcePicture) | copy all of the passed source picture into the current picture object |
void setAllPixelsToAColor(Color color) | set the color in the picture to the passed color |
BufferedImage getBufferedImage() | get the buffered image |
Graphics getGraphics() | get a graphics object for this picture |
Graphics2D createGraphics() | get a Graphics2D object for this picture |
String getFileName() | get the file name associated with the picture |
setFileName(String name) | set the file name |
String getTitle() | get the title of the picture |
void setTitle(String title) | set the title for the picture |
int getWidth() | get the width of the picture in pixels |
int getHeight() | get the height of the picture in pixels |
PictureFrame getPictureFrame() | get the picture frame for the picture |
void setPictureFrame(PictureFrame pictureFrame) | set the picture frame for this picture |
Image getImage() | get an image from the picture |
int getBasicPixel(int x, int y) | return the pixel value as an int for the given x and y location |
void setBasicPixel(int x, int y, int rgb) | set the value of a pixel in the picture from an int |
Pixel getPixel(int x, int y) | get a pixel object for the given x and y location |
Pixel[] getPixels() | get a one-dimensional array of Pixels for this simple picture |
void load(Image image) | load the buffered image with the passed image |
void show() | show the picture in a picture frame |
void hide() | hide the picture |
void setVisible(boolean flag) | make this picture visible or not |
void explore() | open a picture explorer on a copy of this simple picture |
void repaint() | force the picture to redraw itself |
void loadOrFail(String fileName) | load the picture from the passed file name |
boolean load(String fileName) | write the contents of the picture to a file with the given name |
boolean loadImage(String fileName) | load the picture from the passed file name |
void addMessage(String message, int xPos, int yPos) | draw a message as a string on the buffered image at given position |
void drawString(String text, int xPos, int yPos) | draw a string at the given location on the picture at given position |
Picture scale(double xFactor, double yFactor) | create a new picture by scaling the current |
Picture getPictureWithWidth(int width) | create a new picture of the passed width |
Picture getPictureWithHeight(int height) | create a new picture of the passed height |
boolean loadPictureAndShowIt(String fileName) | load a picture from a file name and show it in a picture frame |
void writeOrFail(String fileName) | write the contents of the picture to a file with |
boolean write(String fileName) | write the contents of the picture to a file with given name |
static void setMediaPath(String directory) | set the media path by setting the directory to use |
static String getMediaPath(String fileName) | get the directory for the media |
Rectangle2D getTransformEnclosingRect(AffineTransform trans) | get the coordinates of the enclosing rectangle after this transformation is applied to the current picture |
Rectangle2D getTranslationEnclosingRect(AffineTransform trans) | get the coordinates of the enclosing rectangle |
String toString() | return a string with information about this picture |
Now one can write a Picture or MyPicture class that inherits from SimplePicture class that allows to customize functionality of SimplePicture class. We will discuss MyPicture class implementation in our next article.
No Responses