Java GUI Image Viewer

Pada kesempatan kali ini saya ingin berbagai ilmu mengenai java GUI atau Graphical User Interfaces. Java GUI pada prinsipnya terdapat Components yaitu seperti button, label, menus, slider. Kedua adalah Layout yaitu yang memetakkan komponen-komponen tersebut. yang Ketiga Event sebagai reaksi dari user input seperti button press, menu selections, dan lain-lain.

Langsung saja kita masuk pada contoh program yaitu program penampil gambar. berikut daftar Class - Class yang dibutuhkan:



1. ImageViewer
1:  import java.awt.*;  
2:  import java.awt.event.*;  
3:  import java.awt.image.*;  
4:  import javax.swing.*;  
5:  import java.io.File;  
6:  /**  
7:   *  
8:   * @author Annas  
9:   * @version 26112018  
10:   */  
11:  public class ImageViewer  
12:  {  
13:    //static field  
14:    private static final String VERSION = "Version 1.0";  
15:    private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));  
16:    //field  
17:    private JFrame frame;  
18:    private ImagePanel imagePanel;  
19:    private JLabel filenameLabel;  
20:    private JLabel statusLabel;  
21:    private OFImage currentImage;  
22:    public ImageViewer()  
23:    {  
24:      currentImage = null;  
25:      makeFrame();  
26:    }  
27:    private void openFile()  
28:    {  
29:      int returnVal = fileChooser.showOpenDialog(frame);  
30:      if(returnVal != JFileChooser.APPROVE_OPTION) {  
31:        return; // cancelled  
32:      }  
33:      File selectedFile = fileChooser.getSelectedFile();  
34:      currentImage = ImageFileManager.loadImage(selectedFile);  
35:      if(currentImage == null) {  // image file was not a valid image  
36:        JOptionPane.showMessageDialog(frame,  
37:            "The file was not in a recognized image file format.",  
38:            "Image Load Error",  
39:            JOptionPane.ERROR_MESSAGE);  
40:        return;  
41:      }  
42:      imagePanel.setImage(currentImage);  
43:      showFilename(selectedFile.getPath());  
44:      showStatus("File loaded.");  
45:      frame.pack();  
46:    }  
47:    private void close()  
48:    {  
49:      currentImage = null;  
50:      imagePanel.clearImage();  
51:      showFilename(null);  
52:    }  
53:    private void quit()  
54:    {  
55:      System.exit(0);  
56:    }  
57:    private void makeDarker(){  
58:      if(currentImage != null){  
59:        currentImage.darker();  
60:        frame.repaint();  
61:        showStatus("Applied: darker");  
62:      }  
63:      else{  
64:        showStatus("No image loaded.");  
65:      }  
66:    }  
67:    private void makeLighter()  
68:    {  
69:      if(currentImage != null) {  
70:        currentImage.lighter();  
71:        frame.repaint();  
72:        showStatus("Applied: lighter");  
73:      }  
74:      else {  
75:        showStatus("No image loaded.");  
76:      }  
77:    }  
78:    private void threshold()  
79:    {  
80:      if(currentImage != null) {  
81:        currentImage.threshold();  
82:        frame.repaint();  
83:        showStatus("Applied: threshold");  
84:      }  
85:      else {  
86:        showStatus("No image loaded.");  
87:      }  
88:    }  
89:    private void showAbout()  
90:    {  
91:      JOptionPane.showMessageDialog(frame,  
92:          "ImageViewer\n" + VERSION,  
93:          "About ImageViewer",  
94:          JOptionPane.INFORMATION_MESSAGE);  
95:    }  
96:    //---support method---  
97:    private void showFilename(String filename)  
98:    {  
99:      if(filename == null) {  
100:        filenameLabel.setText("No file displayed.");  
101:      }  
102:      else {  
103:        filenameLabel.setText("File: " + filename);  
104:      }  
105:    }  
106:    private void showStatus(String text)  
107:    {  
108:      statusLabel.setText(text);  
109:    }  
110:    private void makeFrame()  
111:    {  
112:      frame = new JFrame("ImageViewer");  
113:      makeMenuBar(frame);  
114:      Container contentPane = frame.getContentPane();  
115:      // Specify the layout manager with nice spacing  
116:      contentPane.setLayout(new BorderLayout(6, 6));  
117:      filenameLabel = new JLabel();  
118:      contentPane.add(filenameLabel, BorderLayout.NORTH);  
119:      imagePanel = new ImagePanel();  
120:      contentPane.add(imagePanel, BorderLayout.CENTER);  
121:      statusLabel = new JLabel(VERSION);  
122:      contentPane.add(statusLabel, BorderLayout.SOUTH);  
123:      // building is done - arrange the components and show  
124:      showFilename(null);  
125:      frame.pack();  
126:      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();  
127:      frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);  
128:      frame.setVisible(true);  
129:    }  
130:    private void makeMenuBar(JFrame frame)  
131:    {  
132:      final int SHORTCUT_MASK =  
133:          Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();  
134:      JMenuBar menubar = new JMenuBar();  
135:      frame.setJMenuBar(menubar);  
136:      JMenu menu;  
137:      JMenuItem item;  
138:      // create the File menu  
139:      menu = new JMenu("File");  
140:      menubar.add(menu);  
141:      item = new JMenuItem("Open");  
142:      item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));  
143:      item.addActionListener(new ActionListener() {  
144:        public void actionPerformed(ActionEvent e) { openFile(); }  
145:      });  
146:      menu.add(item);  
147:      item = new JMenuItem("Close");  
148:      item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));  
149:      item.addActionListener(new ActionListener() {  
150:        public void actionPerformed(ActionEvent e) { close(); }  
151:      });  
152:      menu.add(item);  
153:      menu.addSeparator();  
154:      item = new JMenuItem("Quit");  
155:      item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));  
156:      item.addActionListener(new ActionListener() {  
157:        public void actionPerformed(ActionEvent e) { quit(); }  
158:      });  
159:      menu.add(item);  
160:      // create the Filter menu  
161:      menu = new JMenu("Filter");  
162:      menubar.add(menu);  
163:      item = new JMenuItem("Darker");  
164:      item.addActionListener(new ActionListener() {  
165:        public void actionPerformed(ActionEvent e) { makeDarker(); }  
166:      });  
167:      menu.add(item);  
168:      item = new JMenuItem("Lighter");  
169:      item.addActionListener(new ActionListener() {  
170:        public void actionPerformed(ActionEvent e) { makeLighter(); }  
171:      });  
172:      menu.add(item);  
173:      item = new JMenuItem("Threshold");  
174:      item.addActionListener(new ActionListener() {  
175:        public void actionPerformed(ActionEvent e) { threshold(); }  
176:      });  
177:      menu.add(item);  
178:      // create the Help menu  
179:      menu = new JMenu("Help");  
180:      menubar.add(menu);  
181:      item = new JMenuItem("About ImageViewer...");  
182:      item.addActionListener(new ActionListener() {  
183:        public void actionPerformed(ActionEvent e) { showAbout(); }  
184:      });  
185:      menu.add(item);  
186:    }  
187:  }  


2. ImageFileManager
1:  import java.awt.image.*;  
2:  import javax.imageio.*;  
3:  import java.io.*;  
4:  /**  
5:   *  
6:   * @author Annas  
7:   * @version 26112018  
8:   */  
9:  public class ImageFileManager  
10:  {  
11:    private static final String IMAGE_FORMAT = "jpg";  
12:    public static OFImage loadImage(File imageFile)  
13:    {  
14:      try {  
15:        BufferedImage image = ImageIO.read(imageFile);  
16:        if(image == null || (image.getWidth(null) < 0)) {  
17:          return null;  
18:        }  
19:        return new OFImage(image);  
20:      }  
21:      catch(IOException exc) {  
22:        return null;  
23:      }  
24:    }  
25:    public static void saveImage(OFImage image, File file)  
26:    {  
27:      try {  
28:        ImageIO.write(image, IMAGE_FORMAT, file);  
29:      }  
30:      catch(IOException exc) {  
31:        return;  
32:      }  
33:    }  
34:  }  


3. ImagePanel
1:  import java.awt.*;  
2:  import javax.swing.*;  
3:  import java.awt.image.*;  
4:  /**  
5:   *  
6:   * @author Annas  
7:   * @version 26112018  
8:   */  
9:  public class ImagePanel extends JComponent  
10:  {  
11:    private int width, height;  
12:    private OFImage panelImage;  
13:    public ImagePanel()  
14:    {  
15:      width = 360;  
16:      height = 240;  
17:      panelImage = null;  
18:    }  
19:    public void setImage(OFImage image)  
20:    {  
21:      if(image!=null)  
22:      {  
23:        width = image.getWidth();  
24:        height = image.getHeight();  
25:        panelImage = image;  
26:        repaint();  
27:      }  
28:    }  
29:    public void clearImage()  
30:    {  
31:      Graphics imageGraphics = panelImage.getGraphics();  
32:      imageGraphics.setColor(Color.LIGHT_GRAY);  
33:      imageGraphics.fillRect(0, 0, width, height);  
34:      repaint();  
35:    }  
36:    public Dimension getPreferredSize()  
37:    {  
38:      return new Dimension (width, height);  
39:    }  
40:    public void paintComponent(Graphics g)  
41:    {  
42:      Dimension size = getSize();  
43:      g.clearRect(0, 0, size.width, size.height);  
44:      if(panelImage != null){  
45:        g.drawImage(panelImage, 0, 0, null);  
46:      }  
47:    }  
48:  }  


4. OFImage
1:  import java.awt.*;  
2:  import java.awt.image.*;  
3:  import javax.swing.*;  
4:  /**  
5:   *  
6:   * @author Annas  
7:   * @version 26112018  
8:   */  
9:  public class OFImage extends BufferedImage  
10:  {  
11:    public OFImage(BufferedImage image)  
12:    {  
13:       super(image.getColorModel(), image.copyData(null),  
14:          image.isAlphaPremultiplied(), null);  
15:    }  
16:    public void setPixel(int x, int y, Color col)  
17:    {  
18:      int pixel = col.getRGB();  
19:      setRGB(x, y, pixel);  
20:    }  
21:    public Color getPixel(int x, int y)  
22:    {  
23:      int pixel = getRGB(x, y);  
24:      return new Color(pixel);  
25:    }  
26:    public void darker()  
27:    {  
28:      int height = getHeight();  
29:      int width = getWidth();  
30:      for(int y = 0; y < height; y++) {  
31:        for(int x = 0; x < width; x++) {  
32:          setPixel(x, y, getPixel(x, y).darker());  
33:        }  
34:      }  
35:    }  
36:    public void lighter()  
37:    {  
38:      int height = getHeight();  
39:      int width = getWidth();  
40:      for(int y = 0; y < height; y++) {  
41:        for(int x = 0; x < width; x++) {  
42:          setPixel(x, y, getPixel(x, y).brighter());  
43:        }  
44:      }  
45:    }  
46:    public void threshold()  
47:    {  
48:      int height = getHeight();  
49:      int width = getWidth();  
50:      for(int y = 0; y < height; y++) {  
51:        for(int x = 0; x < width; x++) {  
52:          Color pixel = getPixel(x, y);  
53:          int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen()) / 3;  
54:          if(brightness <= 85) {  
55:            setPixel(x, y, Color.BLACK);  
56:          }  
57:          else if(brightness <= 170) {  
58:            setPixel(x, y, Color.GRAY);  
59:          }  
60:          else {  
61:            setPixel(x, y, Color.WHITE);  
62:          }  
63:        }  
64:      }  
65:    }  
66:  }  


Lalu coba kita buat instances dari ImageViewer dan open sebuah gambar maka akan didapat hasil seperti berikut:


Komentar

Postingan populer dari blog ini

Pong Java Game

EAS PBO-B

Image Viewer versi 3