This program loads an image when the program starts. Everytime that the user clicks the mouse, the image gets brighter.
import java.awt.*;
import objectdraw.*;
/**
* This program reads an image from a file. When the user
* clicks the button, the image is brightened.
*
* @author Barbara Lerner
* @version March 26, 2008
*/
public class ImageBrightener extends FrameWindowController
{
// The number of columns of pixels in the image
private int imgWidth;
// The number of rows of pixels in the image
private int imgHeight;
// The picture as read from the file
private Picture picture;
// The image as displayed on the screen
private VisibleImage visibleImage;
// The name of the file containing the picture
private static final String FILENAME = "http://www.mtholyoke.edu/~blerner/cs101/Examples/Lecture16/ImageBrightener/KidsAndEleanor.jpg";
/**
* Load the picture from the file and display it
*/
public void begin() {
resize (488, 686);
picture = new Picture (FILENAME);
imgWidth = picture.getWidth();
imgHeight = picture.getHeight();
showImage();
}
/**
* Removes the existing image if there is one. Then displays the picture
* including any brightening that has been done so far
*/
private void showImage () {
if (visibleImage != null) {
visibleImage.removeFromCanvas();
}
visibleImage = picture.createVisibleImage (0, 0, canvas);
}
/**
* When the user clicks the mouse, brighten the picture and redisplay it.
*/
public void onMouseClick (Location point) {
brighten();
showImage();
}
/**
* Visit each pixel in the image and replace the current color of the pixel
* with a brighter color.
*/
private void brighten () {
for (int row = 0; row < imgHeight; row++) {
for (int col = 0; col < imgWidth; col++) {
picture.setPixel (row, col, picture.getPixel(row, col).brighter());
}
}
}
}