NRP : 05111740000068
Kelas : PBO A
Pada postingan saya kali ini, saya akan membagikan hasil EAS PBO A saya yaitu membuat sebuah Image Viewer dengan berbagai filter dan fitur.
1. Gambarkan rancangan interfacenya.
Untuk interfacenya, ada 3 buah menu yaitu:
- File yang berisi :
- Open untuk membuka file image.
- Close untuk menutup Image yang telah dibuka / reset aplikasi.
- Save As untuk menyimpan gambar yang telah diedit.
- Quit untuk keluar dari aplikasi.
- Filter yang berisi :
- Filter Darker
- Filter Lighter
- Filter Threshold
- Filter Invert
- Filter Solarize
- Filter Smooth
- Filter Pixelize
- Filter Mirror
- Filter Grayscale
- Filter Edge Detection
- Filter Fish Eye
- Help untuk Informasi mengenai program Image Viewer
Selanjutnya ada panel di sebelah kiri yang berisi fitur yang terdapat pada Image Viewer. Adapun untuk fitur-fiturnya yaitu:
- Smaller untuk memperkecil gambar.
- Larger untuk memperbesar gambar.
- Crop untuk memotong gambar.
- Add Text untuk menambahkan teks pada gambar.
Kemudian untuk gambar akan ditampilkan disebelah kanan panel fitur dan nama file akan ditampilkan diatas panel fitur dengan status di bagian bawah kiri aplikasi.
2. Gambarkan dan jelaskan Class Diagram penyusun dari image viewer yang akan dibuat.
Disini, saya membuat 16 Class yang terdiri dari:
- ImageViewer sebagai main class dari program Image Viewer tersebut sekaligus berisi fitur Smaller, Larger, Crop, dan Add Text.
- OFImage sebagai class yang mendefinisikan sebuah gambar dalam Object First format.
- ImageFileManager sebagai utility class untuk membuka dan menyimpan gambar.
- ImagePanel sebagai class yang berisi komponen Swing dan menampilkan OFImage.
- Filter sebagai superclass abstrak untuk semua filter dalam Image Viewer.
- SolarizeFilter class untuk memberi efek solarize pada gambar.
- InvertFilter class untuk memberi efek invert pada gambar.
- MirrorFilter class untuk memberi efek mirror/membalik gambar secara horizontal.
- EdgeFilter class untuk mendeteksi edge dan memberi efek warna seperti pensil warna.
- FishEyeFilter class untuk memberi efek Fish Eye pada gambar.
- LighterFilter class untuk memberi efek terang pada gambar.
- SmoothFilter class untuk melembutkan gambar seperti soft lens.
- PixelizeFilter class untuk memberi efek pixelisasi pada gambar.
- GrayScaleFilter class untuk memberi efek hitam-putih pada gambar.
- ThresholdFilter class untuk memberi efek threshold pada gambar.
- DarkerFilter class untuk memberi efek gelap pada gambar.
3. Implementasikan ke dalam program.
Berikut Source Code beserta Screenshot pemakaiannya.
- Source Code
- ImageViewer
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 javax.swing.JOptionPane;
7: import javax.swing.JFrame;
8: import java.io.File;
9: import java.util.List;
10: import java.util.ArrayList;
11: import java.util.Iterator;
12: /**
13: * ImageViewer is the main class of the image viewer application. It builds and
14: * displays the application GUI and initialises all other components.
15: *
16: * To start the application, create an object of this class.
17: *
18: * @author Haikal Almaz Said
19: * @version 12/10/2018
20: */
21: public class ImageViewer
22: {
23: // static fields:
24: private static final String VERSION = "Version 3.1";
25: private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir")) ;
26: // fields:
27: private JFrame frame;
28: private ImagePanel imagePanel;
29: private JLabel filenameLabel;
30: private JLabel statusLabel;
31: private JButton smallerButton;
32: private JButton largerButton;
33: private JButton textButton;
34: private JButton cropButton;
35: private OFImage currentImage;
36: private List<Filter> filters;
37: /**
38: * Create an ImageViewer and display its GUI on screen.
39: */
40: public ImageViewer()
41: {
42: currentImage = null;
43: filters = createFilters();
44: makeFrame();
45: }
46: // ---- implementation of menu functions ----
47: /**
48: * Open function: open a file chooser to select a new image file,
49: * and then display the chosen image.
50: */
51: private void openFile()
52: {
53: int returnVal = fileChooser.showOpenDialog(frame);
54: if(returnVal != JFileChooser.APPROVE_OPTION) {
55: return; // cancelled
56: }
57: File selectedFile = fileChooser.getSelectedFile();
58: currentImage = ImageFileManager.loadImage(selectedFile);
59: if(currentImage == null) { // image file was not a valid image
60: JOptionPane.showMessageDialog(frame,
61: "The file was not in a recognized image file format.",
62: "Image Load Error",
63: JOptionPane.ERROR_MESSAGE);
64: return;
65: }
66: imagePanel.setImage(currentImage);
67: setButtonsEnabled(true);
68: showFilename(selectedFile.getPath());
69: showStatus("File loaded.");
70: frame.pack();
71: }
72: /**
73: * Close function: close the current image.
74: */
75: private void close()
76: {
77: currentImage = null;
78: imagePanel.clearImage();
79: showFilename(null);
80: setButtonsEnabled(false);
81: }
82: /**
83: * Save As function: save the current image to a file.
84: */
85: private void saveAs()
86: {
87: if(currentImage != null) {
88: int returnVal = fileChooser.showSaveDialog(frame);
89: if(returnVal != JFileChooser.APPROVE_OPTION) {
90: return; // cancelled
91: }
92: File selectedFile = fileChooser.getSelectedFile();
93: ImageFileManager.saveImage(currentImage, selectedFile);
94: showFilename(selectedFile.getPath());
95: }
96: }
97: /**
98: * Quit function: quit the application.
99: */
100: private void quit()
101: {
102: System.exit(0);
103: }
104: /**
105: * Apply a given filter to the current image.
106: *
107: * @param filter The filter object to be applied.
108: */
109: private void applyFilter(Filter filter)
110: {
111: if(currentImage != null) {
112: filter.apply(currentImage);
113: frame.repaint();
114: showStatus("Applied: " + filter.getName());
115: }
116: else {
117: showStatus("No image loaded.");
118: }
119: }
120: /**
121: * 'About' function: show the 'about' box.
122: */
123: private void showAbout()
124: {
125: JOptionPane.showMessageDialog(frame,
126: "ImageViewer\n" + VERSION,
127: "About ImageViewer",
128: JOptionPane.INFORMATION_MESSAGE);
129: }
130: /**
131: * Make the current picture larger.
132: */
133: private void makeLarger()
134: {
135: if(currentImage != null) {
136: // create new image with double size
137: int width = currentImage.getWidth();
138: int height = currentImage.getHeight();
139: OFImage newImage = new OFImage(width * 2, height * 2);
140: // copy pixel data into new image
141: for(int y = 0; y < height; y++) {
142: for(int x = 0; x < width; x++) {
143: Color col = currentImage.getPixel(x, y);
144: newImage.setPixel(x * 2, y * 2, col);
145: newImage.setPixel(x * 2 + 1, y * 2, col);
146: newImage.setPixel(x * 2, y * 2 + 1, col);
147: newImage.setPixel(x * 2+1, y * 2 + 1, col);
148: }
149: }
150: currentImage = newImage;
151: imagePanel.setImage(currentImage);
152: frame.pack();
153: }
154: }
155: /**
156: * Make the current picture smaller.
157: */
158: private void makeSmaller()
159: {
160: if(currentImage != null) {
161: // create new image with double size
162: int width = currentImage.getWidth() / 2;
163: int height = currentImage.getHeight() / 2;
164: OFImage newImage = new OFImage(width, height);
165: // copy pixel data into new image
166: for(int y = 0; y < height; y++) {
167: for(int x = 0; x < width; x++) {
168: newImage.setPixel(x, y, currentImage.getPixel(x * 2, y * 2));
169: }
170: }
171: currentImage = newImage;
172: imagePanel.setImage(currentImage);
173: frame.pack();
174: }
175: }
176: private void crop()
177: {
178: if(currentImage != null) {
179: int x1,x2,y1,y2;
180: x1 = Integer.parseInt(JOptionPane.showInputDialog("X1 Coordinate"));
181: y1 = Integer.parseInt(JOptionPane.showInputDialog("Y1 Coordinate"));
182: x2 = Integer.parseInt(JOptionPane.showInputDialog("X2 Coordinate"));
183: y2 = Integer.parseInt(JOptionPane.showInputDialog("Y2 Coordinate"));
184: int width = x2-x1;
185: int height = y2-y1;
186: OFImage newImage = new OFImage(width, height);
187: // copy pixel data into new image
188: for(int y = 0; y < height; y++) {
189: for(int x = 0; x < width; x++) {
190: newImage.setPixel(x, y, currentImage.getPixel(x+x1, y+y1));
191: }
192: }
193: currentImage = newImage;
194: imagePanel.setImage(currentImage);
195: frame.pack();
196: }
197: }
198: private void makeText()
199: {
200: JTextField xField = new JTextField(5);
201: JTextField yField = new JTextField(5);
202: JTextField zField = new JTextField(5);
203: JPanel myPanel = new JPanel();
204: myPanel.add(new JLabel("R"));
205: myPanel.add(xField);
206: myPanel.add(Box.createVerticalStrut(15)); // a spacer
207: myPanel.add(new JLabel("G"));
208: myPanel.add(yField);
209: myPanel.add(Box.createVerticalStrut(15)); // a spacer
210: myPanel.add(new JLabel("B"));
211: myPanel.add(zField);
212: if(currentImage != null) {
213: int width = currentImage.getWidth();
214: int height = currentImage.getHeight();
215: int xPosition = Integer.parseInt(JOptionPane.showInputDialog("Pixel Position X"));
216: int yPosition = Integer.parseInt(JOptionPane.showInputDialog("Pixel Position Y"));
217: float fontSize = Float.parseFloat(JOptionPane.showInputDialog("Font Size"));
218: String addText = JOptionPane.showInputDialog("Write Something..");
219: int result = JOptionPane.showConfirmDialog(null, myPanel, "Font Color", JOptionPane.O K_CANCEL_OPTION);
220: OFImage newImage = new OFImage(width, height);
221: // copy pixel data into new image
222: for(int y = 0; y < height; y++) {
223: for(int x = 0; x < width; x++) {
224: Color col = currentImage.getPixel(x, y);
225: newImage.setPixel(x, y, col);
226: }
227: }
228: int r = Integer.parseInt(xField.getText());
229: int gr = Integer.parseInt(yField.getText());
230: int b = Integer.parseInt(zField.getText());
231: Color c = new Color(r,gr,b);
232: Graphics g = newImage.getGraphics();
233: g.setFont(g.getFont().deriveFont(fontSize));
234: g.setColor(c);
235: g.drawString(addText, xPosition, yPosition);
236: g.dispose();
237: currentImage = newImage;
238: imagePanel.setImage(currentImage);
239: }
240: }
241: // ---- support methods ----
242: /**
243: * Show the file name of the current image in the fils display label.
244: * 'null' may be used as a parameter if no file is currently loaded.
245: *
246: * @param filename The file name to be displayed, or null for 'no file'.
247: */
248: private void showFilename(String filename)
249: {
250: if(filename == null) {
251: filenameLabel.setText("No file displayed.");
252: }
253: else {
254: filenameLabel.setText("File: " + filename);
255: }
256: }
257: /**
258: * Show a message in the status bar at the bottom of the screen.
259: * @param text The status message.
260: */
261: private void showStatus(String text)
262: {
263: statusLabel.setText(text);
264: }
265: /**
266: * Enable or disable all toolbar buttons.
267: *
268: * @param status 'true' to enable the buttons, 'false' to disable.
269: */
270: private void setButtonsEnabled(boolean status)
271: {
272: smallerButton.setEnabled(status);
273: largerButton.setEnabled(status);
274: cropButton.setEnabled(status);
275: }
276: /**
277: * Create a list with all the known filters.
278: * @return The list of filters.
279: */
280: private List<Filter> createFilters()
281: {
282: List<Filter> filterList = new ArrayList<Filter>();
283: filterList.add(new DarkerFilter("Darker"));
284: filterList.add(new LighterFilter("Lighter"));
285: filterList.add(new ThresholdFilter("Threshold"));
286: filterList.add(new InvertFilter("Invert"));
287: filterList.add(new SolarizeFilter("Solarize"));
288: filterList.add(new SmoothFilter("Smooth"));
289: filterList.add(new PixelizeFilter("Pixelize"));
290: filterList.add(new MirrorFilter("Mirror"));
291: filterList.add(new GrayScaleFilter("Grayscale"));
292: filterList.add(new EdgeFilter("Edge Detection"));
293: filterList.add(new FishEyeFilter("Fish Eye"));
294: return filterList;
295: }
296: // ---- Swing stuff to build the frame and all its components and menus ----
297: /**
298: * Create the Swing frame and its content.
299: */
300: private void makeFrame()
301: {
302: frame = new JFrame("ImageViewer");
303: JPanel contentPane = (JPanel)frame.getContentPane();
304: contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
305: makeMenuBar(frame);
306: // Specify the layout manager with nice spacing
307: contentPane.setLayout(new BorderLayout(6, 6));
308: // Create the image pane in the center
309: imagePanel = new ImagePanel();
310: imagePanel.setBorder(new EtchedBorder());
311: contentPane.add(imagePanel, BorderLayout.CENTER);
312: // Create two labels at top and bottom for the file name and status messages
313: filenameLabel = new JLabel();
314: contentPane.add(filenameLabel, BorderLayout.NORTH);
315: statusLabel = new JLabel(VERSION);
316: contentPane.add(statusLabel, BorderLayout.SOUTH);
317: // Create the toolbar with the buttons
318: JPanel toolbar = new JPanel();
319: toolbar.setLayout(new GridLayout(0, 1));
320: smallerButton = new JButton("Smaller");
321: smallerButton.addActionListener(new ActionListener() {
322: public void actionPerformed(ActionEvent e) { makeSmaller(); }
323: });
324: toolbar.add(smallerButton);
325: largerButton = new JButton("Larger");
326: largerButton.addActionListener(new ActionListener() {
327: public void actionPerformed(ActionEvent e) { makeLarger(); }
328: });
329: toolbar.add(largerButton);
330: cropButton = new JButton("Crop");
331: cropButton.addActionListener(new ActionListener() {
332: public void actionPerformed(ActionEvent e) { crop(); }
333: });
334: toolbar.add(cropButton);
335: textButton = new JButton("Add Text");
336: textButton.addActionListener(new ActionListener() {
337: public void actionPerformed(ActionEvent e) { makeText();}
338: });
339: toolbar.add(textButton);
340: // Add toolbar into panel with flow layout for spacing
341: JPanel flow = new JPanel();
342: flow.add(toolbar);
343: contentPane.add(flow, BorderLayout.WEST);
344: // building is done - arrange the components
345: showFilename(null);
346: setButtonsEnabled(false);
347: frame.pack();
348: // place the frame at the center of the screen and show
349: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
350: frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
351: frame.setVisible(true);
352: }
353: /**
354: * Create the main frame's menu bar.
355: *
356: * @param frame The frame that the menu bar should be added to.
357: */
358: private void makeMenuBar(JFrame frame)
359: {
360: final int SHORTCUT_MASK =
361: Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
362: JMenuBar menubar = new JMenuBar();
363: frame.setJMenuBar(menubar);
364: JMenu menu;
365: JMenuItem item;
366: // create the File menu
367: menu = new JMenu("File");
368: menubar.add(menu);
369: item = new JMenuItem("Open...");
370: item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));
371: item.addActionListener(new ActionListener() {
372: public void actionPerformed(ActionEvent e) { openFile(); }
373: });
374: menu.add(item);
375: item = new JMenuItem("Close");
376: item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));
377: item.addActionListener(new ActionListener() {
378: public void actionPerformed(ActionEvent e) { close(); }
379: });
380: menu.add(item);
381: menu.addSeparator();
382: item = new JMenuItem("Save As...");
383: item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, SHORTCUT_MASK));
384: item.addActionListener(new ActionListener() {
385: public void actionPerformed(ActionEvent e) { saveAs(); }
386: });
387: menu.add(item);
388: menu.addSeparator();
389: item = new JMenuItem("Quit");
390: item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
391: item.addActionListener(new ActionListener() {
392: public void actionPerformed(ActionEvent e) { quit(); }
393: });
394: menu.add(item);
395: // create the Filter menu
396: menu = new JMenu("Filter");
397: menubar.add(menu);
398: for(final Filter filter : filters) {
399: item = new JMenuItem(filter.getName());
400: item.addActionListener(new ActionListener() {
401: public void actionPerformed(ActionEvent e) {
402: applyFilter(filter);
403: }
404: });
405: menu.add(item);
406: }
407: // create the Help menu
408: menu = new JMenu("Help");
409: menubar.add(menu);
410: item = new JMenuItem("About ImageViewer...");
411: item.addActionListener(new ActionListener() {
412: public void actionPerformed(ActionEvent e) { showAbout(); }
413: });
414: menu.add(item);
415: }
416: }
- 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 Haikal Almaz Said
8: * @version 12/10/2018
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: }
- ImageFileManager
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 Haikal Almaz Said
12: * @version 12/10/2018
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: }
- ImagePanel
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 Haikal Almaz Said
11: * @version 12/10/2018
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: }
- 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 Haikal Almaz Said
7: * @version 12/10/2018
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: }
- SolarizeFilter
1: import java.awt.Color;
2: /**
3: * An image filter to create a solarization effect.
4: *
5: * @author Haikal Almaz Said
6: * @version 12/10/2018
7: */
8: public class SolarizeFilter extends Filter
9: {
10: /**
11: * Constructor for objects of class Solarize.
12: * @param name The name of the filter.
13: */
14: public SolarizeFilter(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 pix = image.getPixel(x, y);
30: int red = pix.getRed();
31: if(red <= 127) {
32: red = 255 - red;
33: }
34: int green = pix.getGreen();
35: if(green <= 127) {
36: green = 255 - green;
37: }
38: int blue = pix.getBlue();
39: if(blue <= 127) {
40: blue = 255 - blue;
41: }
42: image.setPixel(x, y, new Color(red, green, blue));
43: }
44: }
45: }
46: }
- InvertFilter
1: import java.awt.Color;
2: /**
3: * An image filter to invert colors.
4: *
5: * @author Haikal Almaz Said
6: * @version 12/10/2018
7: */
8: public class InvertFilter extends Filter
9: {
10: /**
11: * Constructor for objects of class InvertFilter.
12: * @param name The name of the filter.
13: */
14: public InvertFilter(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 pix = image.getPixel(x, y);
30: image.setPixel(x, y, new Color(255 - pix.getRed(),
31: 255 - pix.getGreen(),
32: 255 - pix.getBlue()));
33: }
34: }
35: }
36: }
- MirrorFilter
1: import java.awt.Color;
2: /**
3: * An image filter to mirror (flip) the image horizontally.
4: *
5: * @author Haikal Almaz Said
6: * @version 12/10/2018
7: */
8: public class MirrorFilter extends Filter
9: {
10: /**
11: * Constructor for objects of class MirrorFilter.
12: * @param name The name of the filter.
13: */
14: public MirrorFilter(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 / 2; x++) {
29: Color left = image.getPixel(x, y);
30: image.setPixel(x, y, image.getPixel(width - 1 - x, y));
31: image.setPixel(width - 1 - x, y, left);
32: }
33: }
34: }
35: }
- EdgeFilter
1: import java.awt.Color;
2: import java.util.List;
3: import java.util.ArrayList;
4: /**
5: * An image filter to detect edges and highlight them, a bit like
6: * a colored pencil drawing.
7: *
8: * @author Haikal Almaz Said
9: * @version 12/10/2018
10: */
11: public class EdgeFilter extends Filter
12: {
13: private static final int TOLERANCE = 20;
14: private OFImage original;
15: private int width;
16: private int height;
17: /**
18: * Constructor for objects of class EdgeFilter.
19: * @param name The name of the filter.
20: */
21: public EdgeFilter(String name)
22: {
23: super(name);
24: }
25: /**
26: * Apply this filter to an image.
27: *
28: * @param image The image to be changed by this filter.
29: */
30: public void apply(OFImage image)
31: {
32: original = new OFImage(image);
33: width = original.getWidth();
34: height = original.getHeight();
35: for(int y = 0; y < height; y++) {
36: for(int x = 0; x < width; x++) {
37: image.setPixel(x, y, edge(x, y));
38: }
39: }
40: }
41: /**
42: * Return a new color that is the smoothed color of a given
43: * position. The "smoothed color" is the color value that is the
44: * average of this pixel and all the adjacent pixels.
45: * @param xpos The x position of the pixel.
46: * @param ypos The y position of the pixel.
47: * @return The smoothed color.
48: */
49: private Color edge(int xpos, int ypos)
50: {
51: List<Color> pixels = new ArrayList<Color>(9);
52: for(int y = ypos-1; y <= ypos+1; y++) {
53: for(int x = xpos-1; x <= xpos+1; x++) {
54: if( x >= 0 && x < width && y >= 0 && y < height ) {
55: pixels.add(original.getPixel(x, y));
56: }
57: }
58: }
59: return new Color(255 - diffRed(pixels), 255 - diffGreen(pixels), 255 - diffBlue(pixels));
60: }
61: /**
62: * @param pixels The list of pixels to be averaged.
63: * @return The average of all the red values in the given list of pixels.
64: */
65: private int diffRed(List<Color> pixels)
66: {
67: int max = 0;
68: int min = 255;
69: for(Color color : pixels) {
70: int val = color.getRed();
71: if(val > max) {
72: max = val;
73: }
74: if(val < min) {
75: min = val;
76: }
77: }
78: int difference = max - min - TOLERANCE;
79: if(difference < 0) {
80: difference = 0;
81: }
82: return difference;
83: }
84: /**
85: * @param pixels The list of pixels to be averaged.
86: * @return The average of all the green values in the given list of pixels.
87: */
88: private int diffGreen(List<Color> pixels)
89: {
90: int max = 0;
91: int min = 255;
92: for(Color color : pixels) {
93: int val = color.getGreen();
94: if(val > max) {
95: max = val;
96: }
97: if(val < min) {
98: min = val;
99: }
100: }
101: int difference = max - min - TOLERANCE;
102: if(difference < 0) {
103: difference = 0;
104: }
105: return difference;
106: }
107: /**
108: * @param pixels The list of pixels to be averaged.
109: * @return The average of all the blue values in the given list of pixels.
110: */
111: private int diffBlue(List<Color> pixels)
112: {
113: int max = 0;
114: int min = 255;
115: for(Color color : pixels) {
116: int val = color.getBlue();
117: if(val > max) {
118: max = val;
119: }
120: if(val < min) {
121: min = val;
122: }
123: }
124: int difference = max - min - TOLERANCE;
125: if(difference < 0) {
126: difference = 0;
127: }
128: return difference;
129: }
130: }
- FishEyeFilter
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 Haikal Almaz Said
7: * @version 12/10/2018
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: }
- LighterFilter
1: /**
2: * An image filter to make the image a bit lighter.
3: *
4: * @author Haikal Almaz Said
5: * @version 12/10/2018
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: }
- SmoothFilter
1: import java.awt.Color;
2: import java.util.List;
3: import java.util.ArrayList;
4: /**
5: * An image filter to reduce sharp edges and pixelization. A bit like
6: * a soft lens.
7: *
8: * @author Haikal Almaz Said
9: * @version 12/10/2018
10: */
11: public class SmoothFilter extends Filter
12: {
13: private OFImage original;
14: private int width;
15: private int height;
16: /**
17: * Constructor for objects of class SmoothFilter.
18: * @param name The name of the filter.
19: */
20: public SmoothFilter(String name)
21: {
22: super(name);
23: }
24: /**
25: * Apply this filter to an image.
26: *
27: * @param image The image to be changed by this filter.
28: */
29: public void apply(OFImage image)
30: {
31: original = new OFImage(image);
32: width = original.getWidth();
33: height = original.getHeight();
34: for(int y = 0; y < height; y++) {
35: for(int x = 0; x < width; x++) {
36: image.setPixel(x, y, smooth(x, y));
37: }
38: }
39: }
40: /**
41: * Return a new color that is the smoothed color of a given
42: * position. The "smoothed color" is the color value that is the
43: * average of this pixel and all the adjacent pixels.
44: * @param xpos The xposition of the pixel.
45: * @param ypos The yposition of the pixel.
46: * @return The smoothed color.
47: */
48: private Color smooth(int xpos, int ypos)
49: {
50: List<Color> pixels = new ArrayList<Color>(9);
51: for(int y = ypos - 1; y <= ypos + 1; y++) {
52: for(int x = xpos - 1; x <= xpos + 1; x++) {
53: if( x >= 0 && x < width && y >= 0 && y < height )
54: pixels.add(original.getPixel(x, y));
55: }
56: }
57: return new Color(avgRed(pixels), avgGreen(pixels), avgBlue(pixels));
58: }
59: /**
60: * @param pixels The list of pixels.
61: * @return The average of all the red values in the given list of pixels.
62: */
63: private int avgRed(List<Color> pixels)
64: {
65: int total = 0;
66: for(Color color : pixels) {
67: total += color.getRed();
68: }
69: return total / pixels.size();
70: }
71: /**
72: * @param pixels The list of pixels.
73: * @return The average of all the green values in the given list of pixels.
74: */
75: private int avgGreen(List<Color> pixels)
76: {
77: int total = 0;
78: for(Color color : pixels) {
79: total += color.getGreen();
80: }
81: return total / pixels.size();
82: }
83: /**
84: * @param pixels The list of pixels.
85: * @return The average of all the blue values in the given list of pixels.
86: */
87: private int avgBlue(List<Color> pixels)
88: {
89: int total = 0;
90: for(Color color : pixels) {
91: total += color.getBlue();
92: }
93: return total / pixels.size();
94: }
95: }
- PixelizeFilter
1: import java.awt.Color;
2: /**
3: * An image filter to create a pixelization effect, like an enlarged
4: * low-resolution digital image.
5: *
6: * @author Haikal Almaz Said
7: * @version 12/10/2018
8: */
9: public class PixelizeFilter extends Filter
10: {
11: /**
12: * Constructor for objects of class PixelizeFilter.
13: * @param name The name of the filter.
14: */
15: public PixelizeFilter(String name)
16: {
17: super(name);
18: }
19: /**
20: * Apply this filter to an image.
21: *
22: * @param image The image to be changed by this filter.
23: */
24: public void apply(OFImage image)
25: {
26: final int PIXEL_SIZE = 5;
27: int width = image.getWidth();
28: int height = image.getHeight();
29: for(int y = 0; y < height; y += PIXEL_SIZE) {
30: for(int x = 0; x < width; x += PIXEL_SIZE) {
31: Color pix = image.getPixel(x, y);
32: for(int dy = y; dy < y + PIXEL_SIZE; dy++) {
33: for(int dx = x; dx < x + PIXEL_SIZE; dx++) {
34: if( dx < width && dy < height )
35: image.setPixel(dx, dy, pix);
36: }
37: }
38: }
39: }
40: }
41: }
- GrayScaleFilter
1: import java.awt.Color;
2: /**
3: * An image filter to remove color from an image.
4: *
5: * @author Haikal Almaz Said
6: * @version 12/10/2018
7: */
8: public class GrayScaleFilter extends Filter
9: {
10: /**
11: * Constructor for objects of class GrayScaleFilter.
12: * @param name The name of the filter.
13: */
14: public GrayScaleFilter(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 pix = image.getPixel(x, y);
30: int avg = (pix.getRed() + pix.getGreen() + pix.getBlue()) / 3;
31: image.setPixel(x, y, new Color(avg, avg, avg));
32: }
33: }
34: }
35: }
- ThresholdFilter
1: import java.awt.Color;
2: /**
3: * An three-level gray-based threshold filter.
4: *
5: * @author Haikal Almaz Said
6: * @version 12/10/2018
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: }
- DarkerFilter
1: /**
2: * An image filter to make the image a bit darker.
3: *
4: * @author Haikal Almaz Said
5: * @version 12/10/2018
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: }
- Screenshot
(Tampilan ketika pertama menjalankan program)
(Tampilan ketika membuka file gambar)
(Tampilan ketika menggunakan fitur Smaller dimana ukuran gambar mengecil)
(Tampilan ketika menggunakan fitur Larger dimana ukuran gambar membesar)
(Memasukkan koordinat X awal untuk Add Text)
(Memasukkan koordinat Y untuk Add Text)
(Memasukkan Font Size untuk Add Text)
(Menambahkan Warna dengan RGB pada Add Text)
(Memasukkan Teks yang akan di-Insert)
(Tampilan setelah Add Text)
(Setelah Crop dengan koordinat 0,0 ; 400,300)
(Setelah diberi filter Darker)
(Setelah diberi filter Edge Detection)
(Setelah diberi filter Fish Eye)
(Setelah diberi filter Gray Scale)
(Setelah diberi Filter Invert)
(Setelah diberi filter Lighter)
(Setelah diberi filter Pixelize)
(Setelah diberi filter Mirror)
(Setelah diberi filter Smooth)
(Setelah diberi filter Solarize)
(Setelah diberi filter Threshold)
No comments:
Post a Comment