Advertisement
Dr_U

9_ImageViewerClass

Dec 10th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.00 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4. import javax.swing.*;
  5.  
  6. import java.io.File;
  7.  
  8. /**
  9.  * ImageViewer sebagai main class dalam aplikasi imageviewer.
  10.  * dengan membuat sebuah GUI dan menginisialisai komponen lainnya
  11.  * untuk memulai aplikasi dengan membuat sebuah object pada class ini
  12.  *
  13.  * @author Yusuf Anfasya
  14.  * @version 1
  15.  */
  16. public class ImageViewer
  17. {
  18.     // static fields:
  19.     private static final String VERSION = "Versi 1";
  20.     private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
  21.  
  22.     // fields:
  23.     private JFrame frame;
  24.     private ImagePanel imagePanel;
  25.     private JLabel filenameLabel;
  26.     private JLabel statusLabel;
  27.     private OFImage currentImage;
  28.    
  29.     /**
  30.      * membuat imageViewer tampil pada layar
  31.      */
  32.     public ImageViewer()
  33.     {
  34.         currentImage = null;
  35.         makeFrame();
  36.     }
  37.  
  38.  
  39.     // ---- implementasi fungsi ----
  40.    
  41.     /**
  42.      * fungsi Open untuk membuka dan memilih gambar baru
  43.      */
  44.     private void openFile()
  45.     {
  46.         int returnVal = fileChooser.showOpenDialog(frame);
  47.  
  48.         if(returnVal != JFileChooser.APPROVE_OPTION) {
  49.             return;  // apabila batal
  50.         }
  51.         File selectedFile = fileChooser.getSelectedFile();
  52.         currentImage = ImageFileManager.loadImage(selectedFile);
  53.  
  54.         if(currentImage == null) {   // image file was not a valid image
  55.             JOptionPane.showMessageDialog(frame,
  56.                     "Format File tidak dapat dikenali.",
  57.                     "Gambar ERROR!!",
  58.                     JOptionPane.ERROR_MESSAGE);
  59.             return;
  60.         }
  61.        
  62.         imagePanel.setImage(currentImage);
  63.         showFilename(selectedFile.getPath());
  64.         showStatus("File Terbaca.");
  65.         frame.pack();
  66.     }
  67.    
  68.     /**
  69.      * Fungsi Close untuk menutup gambar yang ada
  70.      */
  71.     private void close()
  72.     {
  73.         currentImage = null;
  74.         imagePanel.clearImage();
  75.         showFilename(null);
  76.     }
  77.    
  78.     /**
  79.      * Quit function: quit the application.
  80.      */
  81.     private void quit()
  82.     {
  83.         System.exit(0);
  84.     }
  85.    
  86.    
  87.     /**
  88.      * fungsi 'Gelapin'untuk membuat gambar tampak lebih gelap
  89.      */
  90.     private void jadiGelap()
  91.     {
  92.         if(currentImage != null) {
  93.             currentImage.gelapin();
  94.             frame.repaint();
  95.             showStatus("Applied: Sudah Gelap");
  96.         }
  97.         else {
  98.             showStatus("Gambar tidak terbaca.");
  99.         }
  100.     }
  101.    
  102.     /**
  103.      * fungsi 'Terangin 'untuk membuat gambar tampak lebih terang
  104.      */
  105.     private void jadiTerang()
  106.     {
  107.         if(currentImage != null) {
  108.             currentImage.terangin();
  109.             frame.repaint();
  110.             showStatus("Applied: Sudah Terang");
  111.         }
  112.         else {
  113.             showStatus("Gambar tidak terbaca.");
  114.         }
  115.     }
  116.    
  117.     /**
  118.      * fungsi 'HitamPutih'untuk membuat gambar tampak lebih gelap
  119.      */
  120.     private void HitamPutih()
  121.     {
  122.         if(currentImage != null) {
  123.             currentImage.HitamPutih();
  124.             frame.repaint();
  125.             showStatus("Applied: Pudar");
  126.         }
  127.         else {
  128.             showStatus("Gambar tidak terbaca.");
  129.         }
  130.     }
  131.    
  132.     /**
  133.      * Show the 'About...' dialog.
  134.      */
  135.     private void showAbout()
  136.     {
  137.         JOptionPane.showMessageDialog(frame,
  138.                     "ImageViewer\n" + VERSION,
  139.                     "Tentang ImageViewer",
  140.                     JOptionPane.INFORMATION_MESSAGE);
  141.     }
  142.    
  143.    
  144.     // ---- support methods ----
  145.  
  146.     /**
  147.      * menampilkan nama file
  148.      * @param apabila tidak ada
  149.      */
  150.     private void showFilename(String filename)
  151.     {
  152.         if(filename == null) {
  153.             filenameLabel.setText("File Tidak ada");
  154.         }
  155.         else {
  156.             filenameLabel.setText("File: " + filename);
  157.         }
  158.     }
  159.    
  160.     /**
  161.      * menampilkan pesan status di frame status bar nya
  162.      * @param tulisan status message nya
  163.      */
  164.     private void showStatus(String text)
  165.     {
  166.         statusLabel.setText(text);
  167.     }
  168.    
  169.     // ---- swing stuff to build the frame and all its components ----
  170.    
  171.     /**
  172.      * membuat swing frame dan kontennya.
  173.      */
  174.     private void makeFrame()
  175.     {
  176.         frame = new JFrame("ImageViewer");
  177.         makeMenuBar(frame);
  178.        
  179.         Container contentPane = frame.getContentPane();
  180.        
  181.         // mengatur spacing nya
  182.         contentPane.setLayout(new BorderLayout(6, 6));
  183.        
  184.         filenameLabel = new JLabel();
  185.         contentPane.add(filenameLabel, BorderLayout.NORTH);
  186.  
  187.         imagePanel = new ImagePanel();
  188.         contentPane.add(imagePanel, BorderLayout.CENTER);
  189.  
  190.         statusLabel = new JLabel(VERSION);
  191.         contentPane.add(statusLabel, BorderLayout.SOUTH);
  192.        
  193.         // mengatur component dan tampilan
  194.         showFilename(null);
  195.         frame.pack();
  196.        
  197.         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  198.         frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
  199.         frame.setVisible(true);
  200.     }
  201.    
  202.     /**
  203.      * membuat bar frame utama
  204.      * @param menu bar yang harus ditambahkan
  205.      */
  206.     private void makeMenuBar(JFrame frame)
  207.     {
  208.         final int SHORTCUT_MASK =
  209.             Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  210.  
  211.  
  212.         JMenuBar menubar = new JMenuBar();
  213.         frame.setJMenuBar(menubar);
  214.        
  215.         JMenu menu;
  216.         JMenuItem item;
  217.        
  218.         // file menu
  219.         menu = new JMenu("File");
  220.         menubar.add(menu);
  221.        
  222.         item = new JMenuItem("Open");
  223.             item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));
  224.             item.addActionListener(e -> openFile());
  225.         menu.add(item);
  226.  
  227.         item = new JMenuItem("Close");
  228.             item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));
  229.             item.addActionListener(e -> close());
  230.         menu.add(item);
  231.         menu.addSeparator();
  232.        
  233.         item = new JMenuItem("Quit");
  234.             item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
  235.             item.addActionListener(e -> quit());
  236.         menu.add(item);
  237.  
  238.  
  239.         // create the Filter menu
  240.         menu = new JMenu("Filter");
  241.         menubar.add(menu);
  242.        
  243.         item = new JMenuItem("Darker");
  244.             item.addActionListener(e -> jadiGelap());
  245.         menu.add(item);
  246.  
  247.         item = new JMenuItem("Lighter");
  248.             item.addActionListener(e -> jadiTerang());
  249.         menu.add(item);
  250.  
  251.         item = new JMenuItem("Threshold");
  252.             item.addActionListener(e -> HitamPutih());
  253.         menu.add(item);
  254.  
  255.        
  256.         // menu bantuan
  257.         menu = new JMenu("Help");
  258.         menubar.add(menu);
  259.        
  260.         item = new JMenuItem("About ImageViewer...");
  261.             item.addActionListener(e -> showAbout());
  262.         menu.add(item);
  263.  
  264.     }
  265. }
  266.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement