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