Image Viewer versi 3

Pada kesempatan kali ini saya akan posting kodingan image viewer dengan bahasa Java menggunakan BlueJ IDE. kodingan ini saya kutip dari author buku yang kami pelajari. Berikut gambar mapping class-classnya.


1. Image Viewer
1:  import java.awt.*;  
2:  import java.awt.event.*;  
3:  import java.awt.image.*;  
4:  import javax.swing.*;  
5:  import javax.swing.border.*;  
6:  import java.io.File;  
7:  import java.util.List;  
8:  import java.util.ArrayList;  
9:  import java.util.Iterator;  
10:  /**  
11:   * ImageViewer is the main class of the image viewer application. It builds and  
12:   * displays the application GUI and initialises all other components.  
13:   *   
14:   * To start the application, create an object of this class.  
15:   *   
16:   * @author Michael Kolling and David J Barnes   
17:   * @version 3.0  
18:   */  
19:  public class ImageViewer  
20:  {  
21:    // static fields:  
22:    private static final String VERSION = "Version 3.0";  
23:    private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));  
24:    // fields:  
25:    private JFrame frame;  
26:    private ImagePanel imagePanel;  
27:    private JLabel filenameLabel;  
28:    private JLabel statusLabel;  
29:    private JButton smallerButton;  
30:    private JButton largerButton;  
31:    private OFImage currentImage;  
32:    private List<Filter> filters;  
33:    /**  
34:     * Create an ImageViewer and display its GUI on screen.  
35:     */  
36:    public ImageViewer()  
37:    {  
38:      currentImage = null;  
39:      filters = createFilters();  
40:      makeFrame();  
41:    }  
42:    // ---- implementation of menu functions ----  
43:    /**  
44:     * Open function: open a file chooser to select a new image file,  
45:     * and then display the chosen image.  
46:     */  
47:    private void openFile()  
48:    {  
49:      int returnVal = fileChooser.showOpenDialog(frame);   
50:      if(returnVal != JFileChooser.APPROVE_OPTION) {  
51:        return; // cancelled  
52:      }  
53:      File selectedFile = fileChooser.getSelectedFile();  
54:      currentImage = ImageFileManager.loadImage(selectedFile);  
55:      if(currentImage == null) {  // image file was not a valid image  
56:        JOptionPane.showMessageDialog(frame,  
57:            "The file was not in a recognized image file format.",  
58:            "Image Load Error",  
59:            JOptionPane.ERROR_MESSAGE);  
60:        return;  
61:      }  
62:      imagePanel.setImage(currentImage);  
63:      setButtonsEnabled(true);  
64:      showFilename(selectedFile.getPath());  
65:      showStatus("File loaded.");  
66:      frame.pack();  
67:    }  
68:    /**  
69:     * Close function: close the current image.  
70:     */  
71:    private void close()  
72:    {  
73:      currentImage = null;  
74:      imagePanel.clearImage();  
75:      showFilename(null);  
76:      setButtonsEnabled(false);  
77:    }  
78:    /**  
79:     * Save As function: save the current image to a file.  
80:     */  
81:    private void saveAs()  
82:    {  
83:      if(currentImage != null) {  
84:        int returnVal = fileChooser.showSaveDialog(frame);  
85:        if(returnVal != JFileChooser.APPROVE_OPTION) {  
86:          return; // cancelled  
87:        }  
88:        File selectedFile = fileChooser.getSelectedFile();  
89:        ImageFileManager.saveImage(currentImage, selectedFile);  
90:        showFilename(selectedFile.getPath());  
91:      }  
92:    }  
93:    /**  
94:     * Quit function: quit the application.  
95:     */  
96:    private void quit()  
97:    {  
98:      System.exit(0);  
99:    }  
100:    /**  
101:     * Apply a given filter to the current image.  
102:     *   
103:     * @param filter  The filter object to be applied.  
104:     */  
105:    private void applyFilter(Filter filter)  
106:    {  
107:      if(currentImage != null) {  
108:        filter.apply(currentImage);  
109:        frame.repaint();  
110:        showStatus("Applied: " + filter.getName());  
111:      }  
112:      else {  
113:        showStatus("No image loaded.");  
114:      }  
115:    }  
116:    /**  
117:     * 'About' function: show the 'about' box.  
118:     */  
119:    private void showAbout()  
120:    {  
121:      JOptionPane.showMessageDialog(frame,   
122:            "ImageViewer\n" + VERSION,  
123:            "About ImageViewer",   
124:            JOptionPane.INFORMATION_MESSAGE);  
125:    }  
126:    /**  
127:     * Make the current picture larger.  
128:     */  
129:    private void makeLarger()  
130:    {  
131:      if(currentImage != null) {  
132:        // create new image with double size  
133:        int width = currentImage.getWidth();  
134:        int height = currentImage.getHeight();  
135:        OFImage newImage = new OFImage(width * 2, height * 2);  
136:        // copy pixel data into new image  
137:        for(int y = 0; y < height; y++) {  
138:          for(int x = 0; x < width; x++) {  
139:            Color col = currentImage.getPixel(x, y);  
140:            newImage.setPixel(x * 2, y * 2, col);  
141:            newImage.setPixel(x * 2 + 1, y * 2, col);  
142:            newImage.setPixel(x * 2, y * 2 + 1, col);  
143:            newImage.setPixel(x * 2+1, y * 2 + 1, col);  
144:          }  
145:        }  
146:        currentImage = newImage;  
147:        imagePanel.setImage(currentImage);  
148:        frame.pack();  
149:      }  
150:    }  
151:    /**  
152:     * Make the current picture smaller.  
153:     */  
154:    private void makeSmaller()  
155:    {  
156:      if(currentImage != null) {  
157:        // create new image with double size  
158:        int width = currentImage.getWidth() / 2;  
159:        int height = currentImage.getHeight() / 2;  
160:        OFImage newImage = new OFImage(width, height);  
161:        // copy pixel data into new image  
162:        for(int y = 0; y < height; y++) {  
163:          for(int x = 0; x < width; x++) {  
164:            newImage.setPixel(x, y, currentImage.getPixel(x * 2, y * 2));  
165:          }  
166:        }  
167:        currentImage = newImage;  
168:        imagePanel.setImage(currentImage);  
169:        frame.pack();  
170:      }  
171:    }  
172:    // ---- support methods ----  
173:    /**  
174:     * Show the file name of the current image in the fils display label.  
175:     * 'null' may be used as a parameter if no file is currently loaded.  
176:     *   
177:     * @param filename The file name to be displayed, or null for 'no file'.  
178:     */  
179:    private void showFilename(String filename)  
180:    {  
181:      if(filename == null) {  
182:        filenameLabel.setText("No file displayed.");  
183:      }  
184:      else {  
185:        filenameLabel.setText("File: " + filename);  
186:      }  
187:    }  
188:    /**  
189:     * Show a message in the status bar at the bottom of the screen.  
190:     * @param text The message to be displayed.  
191:     */  
192:    private void showStatus(String text)  
193:    {  
194:      statusLabel.setText(text);  
195:    }  
196:    /**  
197:     * Enable or disable all toolbar buttons.  
198:     *   
199:     * @param status 'true' to enable the buttons, 'false' to disable.  
200:     */  
201:    private void setButtonsEnabled(boolean status)  
202:    {  
203:      smallerButton.setEnabled(status);  
204:      largerButton.setEnabled(status);  
205:    }  
206:    /**  
207:     * Create a list with all the known filters.  
208:     * @return The list of filters.  
209:     */  
210:    private List<Filter> createFilters()  
211:    {  
212:      List<Filter> filterList = new ArrayList<Filter>();  
213:      filterList.add(new DarkerFilter("Darker"));  
214:      filterList.add(new LighterFilter("Lighter"));  
215:      filterList.add(new ThresholdFilter("Threshold"));  
216:      filterList.add(new FishEyeFilter("Fish Eye"));  
217:      return filterList;  
218:    }  
219:    // ---- swing stuff to build the frame and all its components ----  
220:    /**  
221:     * Create the Swing frame and its content.  
222:     */  
223:    private void makeFrame()  
224:    {  
225:      frame = new JFrame("ImageViewer");  
226:      JPanel contentPane = (JPanel)frame.getContentPane();  
227:      contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));  
228:      makeMenuBar(frame);  
229:      // Specify the layout manager with nice spacing  
230:      contentPane.setLayout(new BorderLayout(6, 6));  
231:      // Create the image pane in the center  
232:      imagePanel = new ImagePanel();  
233:      imagePanel.setBorder(new EtchedBorder());  
234:      contentPane.add(imagePanel, BorderLayout.CENTER);  
235:      // Create two labels at top and bottom for the file name and status message  
236:      filenameLabel = new JLabel();  
237:      contentPane.add(filenameLabel, BorderLayout.NORTH);  
238:      statusLabel = new JLabel(VERSION);  
239:      contentPane.add(statusLabel, BorderLayout.SOUTH);  
240:      // Create the toolbar with the buttons  
241:      JPanel toolbar = new JPanel();  
242:      toolbar.setLayout(new GridLayout(0, 1));  
243:      smallerButton = new JButton("Smaller");  
244:      smallerButton.addActionListener(new ActionListener() {  
245:                  public void actionPerformed(ActionEvent e) { makeSmaller(); }  
246:                });  
247:      toolbar.add(smallerButton);  
248:      largerButton = new JButton("Larger");  
249:      largerButton.addActionListener(new ActionListener() {  
250:                  public void actionPerformed(ActionEvent e) { makeLarger(); }  
251:                });  
252:      toolbar.add(largerButton);  
253:      // Add toolbar into panel with flow layout for spacing  
254:      JPanel flow = new JPanel();  
255:      flow.add(toolbar);  
256:      contentPane.add(flow, BorderLayout.WEST);  
257:      // building is done - arrange the components     
258:      showFilename(null);  
259:      setButtonsEnabled(false);  
260:      frame.pack();  
261:      // place the frame at the center of the screen and show  
262:      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();  
263:      frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);  
264:      frame.setVisible(true);  
265:    }  
266:    /**  
267:     * Create the main frame's menu bar.  
268:     *   
269:     * @param frame  The frame that the menu bar should be added to.  
270:     */  
271:    private void makeMenuBar(JFrame frame)  
272:    {  
273:      final int SHORTCUT_MASK =  
274:        Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();  
275:      JMenuBar menubar = new JMenuBar();  
276:      frame.setJMenuBar(menubar);  
277:      JMenu menu;  
278:      JMenuItem item;  
279:      // create the File menu  
280:      menu = new JMenu("File");  
281:      menubar.add(menu);  
282:      item = new JMenuItem("Open...");  
283:        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));  
284:        item.addActionListener(new ActionListener() {  
285:                  public void actionPerformed(ActionEvent e) { openFile(); }  
286:                });  
287:      menu.add(item);  
288:      item = new JMenuItem("Close");  
289:        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));  
290:        item.addActionListener(new ActionListener() {  
291:                  public void actionPerformed(ActionEvent e) { close(); }  
292:                });  
293:      menu.add(item);  
294:      menu.addSeparator();  
295:      item = new JMenuItem("Save As...");  
296:        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, SHORTCUT_MASK));  
297:        item.addActionListener(new ActionListener() {  
298:                  public void actionPerformed(ActionEvent e) { saveAs(); }  
299:                });  
300:      menu.add(item);  
301:      menu.addSeparator();  
302:      item = new JMenuItem("Quit");  
303:        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));  
304:        item.addActionListener(new ActionListener() {  
305:                  public void actionPerformed(ActionEvent e) { quit(); }  
306:                });  
307:      menu.add(item);  
308:      // create the Filter menu  
309:      menu = new JMenu("Filter");  
310:      menubar.add(menu);  
311:      for(final Filter filter : filters) {  
312:        item = new JMenuItem(filter.getName());  
313:        item.addActionListener(new ActionListener() {  
314:                  public void actionPerformed(ActionEvent e) {   
315:                    applyFilter(filter);  
316:                  }  
317:                });  
318:         menu.add(item);  
319:       }  
320:      // create the Help menu  
321:      menu = new JMenu("Help");  
322:      menubar.add(menu);  
323:      item = new JMenuItem("About ImageViewer...");  
324:        item.addActionListener(new ActionListener() {  
325:                  public void actionPerformed(ActionEvent e) { showAbout(); }  
326:                });  
327:      menu.add(item);  
328:    }  
329:  }  


2. Image Panel
1:  import java.awt.*;  
2:  import javax.swing.*;  
3:  import java.awt.image.*;  
4:  /**  
5:   * An ImagePanel is a Swing component that can display an OFImage.  
6:   * It is constructed as a subclass of JComponent with the added functionality  
7:   * of setting an OFImage that will be displayed on the surface of this  
8:   * component.  
9:   *   
10:   * @author Michael Kolling and David J. Barnes  
11:   * @version 1.0  
12:   */  
13:  public class ImagePanel extends JComponent  
14:  {  
15:    // The current width and height of this panel  
16:    private int width, height;  
17:    // An internal image buffer that is used for painting. For  
18:    // actual display, this image buffer is then copied to screen.  
19:    private OFImage panelImage;  
20:    /**  
21:     * Create a new, empty ImagePanel.  
22:     */  
23:    public ImagePanel()  
24:    {  
25:      width = 360;  // arbitrary size for empty panel  
26:      height = 240;  
27:      panelImage = null;  
28:    }  
29:    /**  
30:     * Set the image that this panel should show.  
31:     *   
32:     * @param image The image to be displayed.  
33:     */  
34:    public void setImage(OFImage image)  
35:    {  
36:      if(image != null) {  
37:        width = image.getWidth();  
38:        height = image.getHeight();  
39:        panelImage = image;  
40:        repaint();  
41:      }  
42:    }  
43:    /**  
44:     * Clear the image on this panel.  
45:     */  
46:    public void clearImage()  
47:    {  
48:      Graphics imageGraphics = panelImage.getGraphics();  
49:      imageGraphics.setColor(Color.LIGHT_GRAY);  
50:      imageGraphics.fillRect(0, 0, width, height);  
51:      repaint();  
52:    }  
53:    // The following methods are redefinitions of methods  
54:    // inherited from superclasses.  
55:    /**  
56:     * Tell the layout manager how big we would like to be.  
57:     * (This method gets called by layout managers for placing  
58:     * the components.)  
59:     *   
60:     * @return The preferred dimension for this component.  
61:     */  
62:    public Dimension getPreferredSize()  
63:    {  
64:      return new Dimension(width, height);  
65:    }  
66:    /**  
67:     * This component needs to be redisplayed. Copy the internal image   
68:     * to screen. (This method gets called by the Swing screen painter   
69:     * every time it want this component displayed.)  
70:     *   
71:     * @param g The graphics context that can be used to draw on this component.  
72:     */  
73:    public void paintComponent(Graphics g)  
74:    {  
75:      Dimension size = getSize();  
76:      g.clearRect(0, 0, size.width, size.height);  
77:      if(panelImage != null) {  
78:        g.drawImage(panelImage, 0, 0, null);  
79:      }  
80:    }  
81:  }  


3. OFImage
1:  import java.awt.*;  
2:  import java.awt.image.*;  
3:  import javax.swing.*;  
4:  /**  
5:   * OFImage is a class that defines an image in OF (Objects First) format.  
6:   *   
7:   * @author Michael Kolling and David J. Barnes  
8:   * @version 2.0  
9:   */  
10:  public class OFImage extends BufferedImage  
11:  {  
12:    /**  
13:     * Create an OFImage copied from a BufferedImage.  
14:     * @param image The image to copy.  
15:     */  
16:    public OFImage(BufferedImage image)  
17:    {  
18:       super(image.getColorModel(), image.copyData(null),   
19:          image.isAlphaPremultiplied(), null);  
20:    }  
21:    /**  
22:     * Create an OFImage with specified size and unspecified content.  
23:     * @param width The width of the image.  
24:     * @param height The height of the image.  
25:     */  
26:    public OFImage(int width, int height)  
27:    {  
28:      super(width, height, TYPE_INT_RGB);  
29:    }  
30:    /**  
31:     * Set a given pixel of this image to a specified color. The  
32:     * color is represented as an (r,g,b) value.  
33:     * @param x The x position of the pixel.  
34:     * @param y The y position of the pixel.  
35:     * @param col The color of the pixel.  
36:     */  
37:    public void setPixel(int x, int y, Color col)  
38:    {  
39:      int pixel = col.getRGB();  
40:      setRGB(x, y, pixel);  
41:    }  
42:    /**  
43:     * Get the color value at a specified pixel position.  
44:     * @param x The x position of the pixel.  
45:     * @param y The y position of the pixel.  
46:     * @return The color of the pixel at the given position.  
47:     */  
48:    public Color getPixel(int x, int y)  
49:    {  
50:      int pixel = getRGB(x, y);  
51:      return new Color(pixel);  
52:    }  
53:  }  


4. Image File Manager
1:  import java.awt.image.*;  
2:  import javax.imageio.*;  
3:  import java.io.*;  
4:  /**  
5:   * ImageFileManager is a small utility class with static methods to load  
6:   * and save images.  
7:   *   
8:   * The files on disk can be in JPG or PNG image format. For files written  
9:   * by this class, the format is determined by the constant IMAGE_FORMAT.  
10:   *   
11:   * @author Michael Kolling and David J Barnes   
12:   * @version 2.0  
13:   */  
14:  public class ImageFileManager  
15:  {  
16:    // A constant for the image format that this writer uses for writing.  
17:    // Available formats are "jpg" and "png".  
18:    private static final String IMAGE_FORMAT = "jpg";  
19:    /**  
20:     * Read an image file from disk and return it as an image. This method  
21:     * can read JPG and PNG file formats. In case of any problem (e.g the file   
22:     * does not exist, is in an undecodable format, or any other read error)   
23:     * this method returns null.  
24:     *   
25:     * @param imageFile The image file to be loaded.  
26:     * @return      The image object or null is it could not be read.  
27:     */  
28:    public static OFImage loadImage(File imageFile)  
29:    {  
30:      try {  
31:        BufferedImage image = ImageIO.read(imageFile);  
32:        if(image == null || (image.getWidth(null) < 0)) {  
33:          // we could not load the image - probably invalid file format  
34:          return null;  
35:        }  
36:        return new OFImage(image);  
37:      }  
38:      catch(IOException exc) {  
39:        return null;  
40:      }  
41:    }  
42:    /**  
43:     * Write an image file to disk. The file format is JPG. In case of any   
44:     * problem the method just silently returns.  
45:     *   
46:     * @param image The image to be saved.  
47:     * @param file  The file to save to.  
48:     */  
49:    public static void saveImage(OFImage image, File file)  
50:    {  
51:      try {  
52:        ImageIO.write(image, IMAGE_FORMAT, file);  
53:      }  
54:      catch(IOException exc) {  
55:        return;  
56:      }  
57:    }  
58:  }  


5. Filter
1:  /**  
2:   * Filter is an abstract superclass for all image filters in this  
3:   * application. Filters can be applied to OFImages by invoking the apply   
4:   * method.  
5:   *   
6:   * @author Michael Kolling and David J Barnes   
7:   * @version 1.0  
8:   */  
9:  public abstract class Filter  
10:  {  
11:    private String name;  
12:    /**  
13:     * Create a new filter with a given name.  
14:     * @param name The name of the filter.  
15:     */  
16:    public Filter(String name)  
17:    {  
18:      this.name = name;  
19:    }  
20:    /**  
21:     * Return the name of this filter.  
22:     *   
23:     * @return The name of this filter.  
24:     */  
25:    public String getName()  
26:    {  
27:      return name;  
28:    }  
29:    /**  
30:     * Apply this filter to an image.  
31:     *   
32:     * @param image The image to be changed by this filter.  
33:     */  
34:    public abstract void apply(OFImage image);  
35:  }  


6. Darker Filter
1:  /**  
2:   * An image filter to make the image a bit darker.  
3:   *   
4:   * @author Michael Kolling and David J Barnes   
5:   * @version 1.0  
6:   */  
7:  public class DarkerFilter extends Filter  
8:  {  
9:    /**  
10:     * Constructor for objects of class DarkerFilter.  
11:     * @param name The name of the filter.  
12:     */  
13:    public DarkerFilter(String name)  
14:    {  
15:      super(name);  
16:    }  
17:    /**  
18:     * Apply this filter to an image.  
19:     *   
20:     * @param image The image to be changed by this filter.  
21:     */  
22:    public void apply(OFImage image)  
23:    {  
24:      int height = image.getHeight();  
25:      int width = image.getWidth();  
26:      for(int y = 0; y < height; y++) {  
27:        for(int x = 0; x < width; x++) {  
28:          image.setPixel(x, y, image.getPixel(x, y).darker());  
29:        }  
30:      }  
31:    }  
32:  }  


7. Fish Eye Filter
1:  import java.awt.Color;  
2:  /**  
3:   * An image filter to create an effect similar to a fisheye camera lens.  
4:   * (Works especially well on portraits.)  
5:   *   
6:   * @author Michael Kolling and David J Barnes   
7:   * @version 1.0  
8:   */  
9:  public class FishEyeFilter extends Filter  
10:  {  
11:    // constants:  
12:    private final static int SCALE = 20;  // this defines the strenght of the filter  
13:    private final static double TWO_PI = 2 * Math.PI;  
14:    /**  
15:     * Constructor for objects of class LensFilter.  
16:     * @param name The name of the filter.  
17:     */  
18:    public FishEyeFilter(String name)  
19:    {  
20:      super(name);  
21:    }  
22:    /**  
23:     * Apply this filter to an image.  
24:     *   
25:     * @param image The image to be changed by this filter.  
26:     */  
27:    public void apply(OFImage image)  
28:    {  
29:      int height = image.getHeight();  
30:      int width = image.getWidth();  
31:      OFImage original = new OFImage(image);  
32:      int[] xa = computeXArray(width);  
33:      int[] ya = computeYArray(height);  
34:      for(int y = 0; y < height; y++) {  
35:        for(int x = 0; x < width; x++) {  
36:          image.setPixel(x, y, original.getPixel(x + xa[x], y + ya[y]));  
37:        }  
38:      }  
39:    }  
40:    /**  
41:     * Compute and return an array of horizontal offsets for each pixel column.  
42:     * These can then be applied as the horizontal offset for each pixel.  
43:     */  
44:    private int[] computeXArray(int width)  
45:    {  
46:      int[] xArray = new int[width];  
47:      for(int i=0; i < width; i++) {  
48:        xArray[i] = (int)(Math.sin( ((double)i / width) * TWO_PI) * SCALE);  
49:      }  
50:      return xArray;  
51:    }  
52:    /**  
53:     * Compute and return an array of vertical offsets for each pixel row.  
54:     * These can then be applied as the vertical offset for each pixel.  
55:     */  
56:    private int[] computeYArray(int height)  
57:    {  
58:      int[] yArray = new int[height];  
59:      for(int i=0; i < height; i++) {  
60:        yArray[i] = (int)(Math.sin( ((double)i / height) * TWO_PI) * SCALE);  
61:      }  
62:      return yArray;  
63:    }  
64:  }  


8. Lighter Filter
1:  /**  
2:   * An image filter to make the image a bit lighter.  
3:   *   
4:   * @author Michael Kolling and David J Barnes   
5:   * @version 1.0  
6:   */  
7:  public class LighterFilter extends Filter  
8:  {  
9:       /**  
10:        * Constructor for objects of class LighterFilter.  
11:     * @param name The name of the filter.  
12:        */  
13:       public LighterFilter(String name)  
14:    {  
15:      super(name);  
16:       }  
17:    /**  
18:     * Apply this filter to an image.  
19:     *   
20:     * @param image The image to be changed by this filter.  
21:     */  
22:    public void apply(OFImage image)  
23:    {  
24:      int height = image.getHeight();  
25:      int width = image.getWidth();  
26:      for(int y = 0; y < height; y++) {  
27:        for(int x = 0; x < width; x++) {  
28:          image.setPixel(x, y, image.getPixel(x, y).brighter());  
29:        }  
30:      }  
31:    }  
32:  }  


9. Threshold Filter
1:  import java.awt.Color;  
2:  /**  
3:   * An three-level gray-based threshold filter.  
4:   *   
5:   * @author Michael Kolling and David J Barnes   
6:   * @version 1.0  
7:   */  
8:  public class ThresholdFilter extends Filter  
9:  {  
10:       /**  
11:        * Constructor for objects of class ThresholdFilter.  
12:     * @param name The name of the filter.  
13:        */  
14:       public ThresholdFilter(String name)  
15:    {  
16:      super(name);  
17:       }  
18:    /**  
19:     * Apply this filter to an image.  
20:     *   
21:     * @param image The image to be changed by this filter.  
22:     */  
23:    public void apply(OFImage image)  
24:    {  
25:      int height = image.getHeight();  
26:      int width = image.getWidth();  
27:      for(int y = 0; y < height; y++) {  
28:        for(int x = 0; x < width; x++) {  
29:          Color pixel = image.getPixel(x, y);  
30:          int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen()) / 3;  
31:          if(brightness <= 85) {  
32:            image.setPixel(x, y, Color.BLACK);  
33:          }  
34:          else if(brightness <= 170) {  
35:            image.setPixel(x, y, Color.GRAY);  
36:          }  
37:          else {  
38:            image.setPixel(x, y, Color.WHITE);  
39:          }  
40:        }  
41:      }  
42:    }  
43:  }  


Sekarang kita jalankan class Image Viewer dan viola, jadilah aplikasi Image Viewer kita.


Komentar

Postingan populer dari blog ini

Pong Java Game

EAS PBO-B