Advertisement
mnaufaldillah

ImageViewer Tugas 7

Dec 6th, 2020
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.24 KB | None | 0 0
  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.  * ImageViewer is the main class of the image viewer application. It builds and
  9.  * displays the application GUI and initialises all other components.
  10.  *
  11.  * To start the application, create an object of this class.
  12.  *
  13.  * @author Muhammad Naufaldillah
  14.  * @version 5 December 2020
  15.  */
  16. public class ImageViewer
  17. {
  18.     // static fields:
  19.     private static final String VERSION = "Version 1.0";
  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.      * Create an ImageViewer show it on screen.
  31.      */
  32.     public ImageViewer()
  33.     {
  34.         currentImage = null;
  35.         makeFrame();
  36.     }
  37.    
  38.     // ---- implementation of menu functions ----
  39.    
  40.     /**
  41.      * Open function: open a file chooser to select a new image file.
  42.      */
  43.     private void openFile()
  44.     {
  45.         int returnVal = fileChooser.showOpenDialog(frame);
  46.        
  47.         if(returnVal != JFileChooser.APPROVE_OPTION)
  48.         {
  49.             return;     //  cancelled
  50.         }
  51.         File selectedFile = fileChooser.getSelectedFile();
  52.         currentImage = ImageFileManager.loadImage(selectedFile);
  53.        
  54.         if(currentImage == null)
  55.         {
  56.             // image file was not a valid image
  57.             JOptionPane.showMessageDialog(frame,
  58.                 "The file was not in a recognized image file format.",
  59.                 "Image Load Error",
  60.                 JOptionPane.ERROR_MESSAGE);
  61.             return;
  62.         }
  63.        
  64.         imagePanel.setImage(currentImage);
  65.         showFilename(selectedFile.getPath());
  66.         showStatus("File loaded.");
  67.         frame.pack();
  68.     }
  69.    
  70.     /**
  71.      * Close function: close the current image.
  72.      */
  73.     private void close()
  74.     {
  75.         currentImage = null;
  76.         imagePanel.clearImage();
  77.         showFilename(null);
  78.     }
  79.    
  80.     /**
  81.      * Quit function: quit the application.
  82.      */
  83.     private void quit()
  84.     {
  85.         System.exit(0);
  86.     }
  87.    
  88.     /**
  89.      * 'Darker' function: make the picture darker.
  90.      */
  91.     private void makeDarker()
  92.     {
  93.         if(currentImage != null)
  94.         {
  95.             currentImage.darker();
  96.             frame.repaint();
  97.             showStatus("Applied: darker");
  98.         }
  99.         else
  100.         {
  101.             showStatus("No image loaded.");
  102.         }
  103.     }
  104.    
  105.     /**
  106.      * 'Lighter' function: make the picture lighter
  107.      */
  108.     private void makeLighter()
  109.     {
  110.         if(currentImage != null)
  111.         {
  112.             currentImage.lighter();
  113.             frame.repaint();
  114.             showStatus("Applied: lighter");
  115.         }
  116.         else
  117.         {
  118.             showStatus("No image loaded.");
  119.         }
  120.     }
  121.    
  122.     /**
  123.      * 'threshold' function: apply the threshold filter
  124.      */
  125.     private void threshold()
  126.     {
  127.         if(currentImage != null)
  128.         {
  129.             currentImage.threshold();
  130.             frame.repaint();
  131.             showStatus("Applied: threshold");
  132.         }
  133.         else
  134.         {
  135.             showStatus("No image loaded.");
  136.         }
  137.     }
  138.    
  139.     /**
  140.      * showAbout function: showing the picture's detail
  141.      */
  142.     private void showAbout()
  143.     {
  144.         JOptionPane.showMessageDialog(frame,
  145.                     "ImageViewer\n" + VERSION,
  146.                     "About ImageViewer",
  147.                     JOptionPane.INFORMATION_MESSAGE);
  148.     }
  149.    
  150.     // ---- support methods ----
  151.    
  152.     /**
  153.      * Display a file name on the appropriate label.
  154.      * @param filename The file name to be displayed.
  155.      */
  156.     private void showFilename(String filename)
  157.     {
  158.         if(filename == null)
  159.         {
  160.             filenameLabel.setText("No file displayed.");
  161.         }
  162.         else
  163.         {
  164.             filenameLabel.setText("File: " + filename);
  165.         }
  166.     }
  167.    
  168.     /**
  169.      * Display a status message in the frame's status bar.
  170.      * @param text The status message to be displayed.
  171.      */
  172.     private void showStatus(String text)
  173.     {
  174.         statusLabel.setText(text);
  175.     }
  176.    
  177.     // ---- swing stuff to build the frame and all its components ----
  178.    
  179.     /**
  180.      * Create the Swing frame and its content.
  181.      */
  182.     private void makeFrame()
  183.     {
  184.         frame = new JFrame("ImageViewer");
  185.         makeMenuBar(frame);
  186.        
  187.         Container contentPane = frame.getContentPane();
  188.        
  189.         // Specify the layout manager with nice spacing
  190.         contentPane.setLayout(new BorderLayout(6, 6));
  191.        
  192.         filenameLabel = new JLabel();
  193.         contentPane.add(filenameLabel, BorderLayout.NORTH);
  194.        
  195.         imagePanel = new ImagePanel();
  196.         contentPane.add(imagePanel, BorderLayout.CENTER);
  197.        
  198.         statusLabel = new JLabel(VERSION);
  199.         contentPane.add(statusLabel, BorderLayout.SOUTH);
  200.        
  201.         // building is done - arrange the components and show
  202.         showFilename(null);
  203.         frame.pack();
  204.        
  205.         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  206.         frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
  207.         frame.setVisible(true);
  208.     }
  209.    
  210.     /**
  211.      * Create the main frame's menu bar.
  212.      * @param frame The frame that the menu bar should be added to.
  213.      */
  214.     private void makeMenuBar(JFrame frame)
  215.     {
  216.         final int SHORTCUT_MASK =
  217.             Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();  
  218.          
  219.         JMenuBar menubar = new JMenuBar();
  220.         frame.setJMenuBar(menubar);
  221.        
  222.         JMenu menu;
  223.         JMenuItem item;
  224.        
  225.         // create the File menu
  226.         menu = new JMenu("File");
  227.         menubar.add(menu);
  228.        
  229.         item = new JMenuItem("Open");
  230.             item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));
  231.             item.addActionListener(new ActionListener() {
  232.                                public void actionPerformed(ActionEvent e) { openFile(); }
  233.                            });
  234.         menu.add(item);
  235.        
  236.         item = new JMenuItem("Close");
  237.             item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));
  238.             item.addActionListener(new ActionListener() {
  239.                                public void actionPerformed(ActionEvent e) { close(); }
  240.                             });
  241.         menu.add(item);
  242.         menu.addSeparator();
  243.        
  244.         item = new JMenuItem("Quit");
  245.             item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
  246.             item.addActionListener(new ActionListener() {
  247.                                public void actionPerformed(ActionEvent e) { quit(); }
  248.                             });
  249.         menu.add(item);
  250.        
  251.         // create the Filter menu
  252.         menu = new JMenu("Filter");
  253.         menubar.add(menu);
  254.        
  255.         item = new JMenuItem("Darker");
  256.            item.addActionListener(new ActionListener() {
  257.                               public void actionPerformed(ActionEvent e) { makeDarker(); }
  258.                             });
  259.         menu.add(item);
  260.        
  261.         item = new JMenuItem("Lighter");
  262.            item.addActionListener(new ActionListener() {
  263.                               public void actionPerformed(ActionEvent e) { makeLighter(); }
  264.                             });
  265.         menu.add(item);
  266.        
  267.         item = new JMenuItem("Threshold");
  268.            item.addActionListener(new ActionListener() {
  269.                               public void actionPerformed(ActionEvent e) { threshold(); }
  270.                             });
  271.         menu.add(item);
  272.        
  273.         // create the Help menu
  274.         menu = new JMenu("Help");
  275.         menubar.add(menu);
  276.        
  277.         item = new JMenuItem("About ImageViewer...");
  278.             item.addActionListener(new ActionListener() {
  279.                                public void actionPerformed(ActionEvent e) { showAbout(); }
  280.                             });
  281.         menu.add(item);
  282.        
  283.     }
  284. }
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement