document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  *kelas utama atau main class pada program ini,
  3.  *fungsi class ini yaitu menampilkan aplikasi GUI
  4.  *dan tampilan awal apllikasi
  5.  *
  6.  * @author Migel Aulia Mandiri Putra
  7.  * @version 0.1
  8.  */
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.awt.image.*;
  12. import javax.swing.*;
  13. import java.io.File;
  14.  
  15. public class ImageViewer
  16. {
  17.     private static final String VERSION = "Version 1.0";
  18.     private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
  19.     private JFrame frame;
  20.     private ImagePanel imagePanel;
  21.     private JLabel filenameLabel;
  22.     private JLabel statusLabel;
  23.     private OFImage currentImage;
  24.    
  25.     public ImageViewer()
  26.     {
  27.         currentImage = null;
  28.         makeFrame();
  29.     }
  30.    
  31.     // Fungsi Menu
  32.     // Fungsi openFile: Membuka file/ menampilkan gambar
  33.     private void openFile()
  34.     {
  35.         int returnVal = fileChooser.showOpenDialog(frame);
  36.         if(returnVal != JFileChooser.APPROVE_OPTION) return;  // gagal
  37.         File selectedFile = fileChooser.getSelectedFile();
  38.         currentImage = ImageFileManager.loadImage(selectedFile);
  39.         if(currentImage == null)
  40.         {   // File gambar tidak sesuai format
  41.             JOptionPane.showMessageDialog(frame,
  42.                     "Format File Salaj",
  43.                     "Image Load Error",
  44.                     JOptionPane.ERROR_MESSAGE);
  45.             return;
  46.         }
  47.         imagePanel.setImage(currentImage);
  48.         showFilename(selectedFile.getPath());
  49.         showStatus("File loaded.");
  50.         frame.pack();
  51.     }
  52.     // Fungsi close digunakan untuk menutup gambar
  53.     private void close()
  54.     {
  55.         currentImage = null;
  56.         imagePanel.clearImage();
  57.         showFilename(null);
  58.     }
  59.     // Fungsi quit digunakan untuk keluar dari program/ aplikasi
  60.     private void quit()
  61.     {
  62.         System.exit(0);
  63.     }
  64.     // Fungsi makeDarker digunakan sebagai filter pada gambar
  65.     // Membuat gambar menjadi gelap
  66.     private void makeDarker()
  67.     {
  68.         if(currentImage != null)
  69.         {
  70.             currentImage.darker();
  71.             frame.repaint();
  72.             showStatus("Applied: darker");
  73.         }
  74.        
  75.         else
  76.         {
  77.             showStatus("No image loaded.");
  78.         }
  79.     }
  80.     // Fungsi makeLighter digunakan sebagai filter pada gambar
  81.     // Membuat gambar menjadi lebih terang
  82.     private void makeLighter()
  83.     {
  84.         if(currentImage != null)
  85.         {
  86.             currentImage.lighter();
  87.             frame.repaint();
  88.             showStatus("Applied: lighter");
  89.         }
  90.        
  91.         else
  92.         {
  93.             showStatus("No image loaded.");
  94.         }
  95.     }
  96.     // Fungsi makeDarker digunakan sebagai filter pada gambar
  97.     // Menerapkan filter threshold pada gambar
  98.     private void threshold()
  99.     {
  100.         if(currentImage != null)
  101.         {
  102.             currentImage.threshold();
  103.             frame.repaint();
  104.             showStatus("Applied: threshold");
  105.         }
  106.        
  107.         else
  108.         {
  109.             showStatus("No image loaded.");
  110.         }
  111.     }
  112.     // Fungsi showAbout digunakan untuk menampilkan informasi aplikasi
  113.     private void showAbout()
  114.     {
  115.         JOptionPane.showMessageDialog(frame,
  116.                     "ImageViewer\\n" + VERSION,
  117.                     "About ImageViewer",
  118.                     JOptionPane.INFORMATION_MESSAGE);
  119.     }
  120.     // Method pendukung
  121.     /**
  122.      * Tampilkan nama file pada label.
  123.      * @param filename Nama file yang akan ditampilkan.
  124.      */
  125.     private void showFilename(String filename)
  126.     {
  127.         if(filename == null)
  128.         {
  129.             filenameLabel.setText("No file displayed.");
  130.         }
  131.        
  132.         else
  133.         {
  134.             filenameLabel.setText("File: " + filename);
  135.         }
  136.     }
  137.    
  138.     /**
  139.      * Tampilkan status pada frame status bar.
  140.      * @param text Status yang akan ditampilkan.
  141.      */
  142.     private void showStatus(String text)
  143.     {
  144.         statusLabel.setText(text);
  145.     }
  146.     // Digunkan untuk membangun frame dan komponennya
  147.     private void makeFrame()
  148.     {
  149.         frame = new JFrame("ImageViewer");
  150.         makeMenuBar(frame);
  151.        
  152.         Container contentPane = frame.getContentPane();
  153.        
  154.         contentPane.setLayout(new BorderLayout(6, 6));
  155.        
  156.         filenameLabel = new JLabel();
  157.         contentPane.add(filenameLabel, BorderLayout.NORTH);
  158.  
  159.         imagePanel = new ImagePanel();
  160.         contentPane.add(imagePanel, BorderLayout.CENTER);
  161.  
  162.         statusLabel = new JLabel(VERSION);
  163.         contentPane.add(statusLabel, BorderLayout.SOUTH);
  164.        
  165.         // Menampilkan frame      
  166.         showFilename(null);
  167.         frame.pack();
  168.        
  169.         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  170.         frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
  171.         frame.setVisible(true);
  172.     }
  173.    
  174.     /**
  175.      * Membuat main frame\'s menu bar.
  176.      * @param frame Frame yang akan ditambahkan di menu bar.
  177.      */
  178.     private void makeMenuBar(JFrame frame)
  179.     {
  180.         final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  181.  
  182.         JMenuBar menubar = new JMenuBar();
  183.         frame.setJMenuBar(menubar);
  184.        
  185.         JMenu menu;
  186.         JMenuItem item;
  187.        
  188.         // Membuat menu File
  189.         menu = new JMenu("File");
  190.         menubar.add(menu);
  191.        
  192.         item = new JMenuItem("Open");
  193.         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));
  194.         item.addActionListener(new ActionListener()
  195.         {
  196.             public void actionPerformed(ActionEvent e)
  197.             {
  198.                 openFile();
  199.             }
  200.         });
  201.         menu.add(item);
  202.  
  203.         item = new JMenuItem("Close");
  204.         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));
  205.         item.addActionListener(new ActionListener()
  206.         {
  207.             public void actionPerformed(ActionEvent e)
  208.             {
  209.                 close();
  210.             }
  211.         });
  212.         menu.add(item);
  213.         menu.addSeparator();
  214.        
  215.         item = new JMenuItem("Quit");
  216.         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
  217.         item.addActionListener(new ActionListener()
  218.         {
  219.             public void actionPerformed(ActionEvent e)
  220.             {
  221.                 quit();
  222.             }
  223.         });
  224.         menu.add(item);
  225.  
  226.         // Membuat menu Filter
  227.         menu = new JMenu("Filter");
  228.         menubar.add(menu);
  229.        
  230.         item = new JMenuItem("Darker");
  231.         item.addActionListener(new ActionListener()
  232.         {
  233.             public void actionPerformed(ActionEvent e)
  234.             {
  235.                 makeDarker();
  236.             }
  237.         });
  238.         menu.add(item);
  239.  
  240.         item = new JMenuItem("Lighter");
  241.         item.addActionListener(new ActionListener()
  242.         {
  243.             public void actionPerformed(ActionEvent e)
  244.             {
  245.                 makeLighter();
  246.             }
  247.         });
  248.         menu.add(item);
  249.  
  250.         item = new JMenuItem("Threshold");
  251.         item.addActionListener(new ActionListener()
  252.         {
  253.             public void actionPerformed(ActionEvent e)
  254.             {
  255.                 threshold();
  256.             }
  257.         });
  258.         menu.add(item);
  259.  
  260.         // Membuat menu Help
  261.         menu = new JMenu("Help");
  262.         menubar.add(menu);
  263.        
  264.         item = new JMenuItem("About ImageViewer...");
  265.         item.addActionListener(new ActionListener() {
  266.             public void actionPerformed(ActionEvent e)
  267.             {
  268.                 showAbout();
  269.             }
  270.         });
  271.         menu.add(item);
  272.     }
  273. }
');