import javax.swing.*; import java.awt.*; import java.awt.image.*; /** * A class demonstrating manipulation of image pixels. * Version 0: just the core definition * * @author Chris Bailey-Kellogg, Dartmouth CS 10, Fall 2012 * @author CBK, Winter 2014, rewritten for BufferedImage * @author CBK, Spring 2015, refactored to separate GUI from operations */ public class ImageProcessingGUI0 extends DrawingGUI { private ImageProcessor0 proc; // handles the image processing /** * Creates the GUI for the image processor, with the window scaled to the to-process image's size */ public ImageProcessingGUI0(ImageProcessor0 proc) { super("Image processing", proc.getImage().getWidth(), proc.getImage().getHeight()); this.proc = proc; } /** * DrawingGUI method, here showing the current image */ @Override public void draw(Graphics g) { g.drawImage(proc.getImage(), 0, 0, null); } /** * DrawingGUI method, here dispatching on image processing operations */ @Override public void handleKeyPress(char op) { System.out.println("Handling key '"+op+"'"); if (op=='s') { // save a snapshot saveImage(proc.getImage(), "pictures/snapshot.jpg", "jpg"); } else { System.out.println("Unknown operation"); } repaint(); // Re-draw, since image has changed } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { // Load the image to process BufferedImage baker = loadImage("pictures/baker.jpg"); // Create a new processor, and a GUI to handle it new ImageProcessingGUI0(new ImageProcessor0(baker)); } }); } }