Advertisement
lamaulfarid

ImageViewer

Dec 9th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.44 KB | None | 0 0
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.image.*;
  5. import javax.swing.*;
  6. import java.io.File;
  7.  
  8. /**
  9.  * ImageViewer is the main class of the Image Viewer application. It builds and
  10.  * displays the application GUI and initialises all other components.
  11.  *
  12.  * @author Ahmad Lamaul Farid
  13.  * @version 9 Desember 2020
  14.  */
  15.  
  16. public class ImageViewer
  17. {
  18.     private static final String VERSION = "Version 1.0";
  19.     private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
  20.    
  21.     private JFrame frame;
  22.     private ImagePanel imagePanel;
  23.     private JLabel filenameLabel;
  24.     private JLabel statusLabel;
  25.     private OFImage currentImage;
  26.    
  27.     /**
  28.      * Create an ImageViewer show it on screen.
  29.      */
  30.    
  31.     public ImageViewer()
  32.     {
  33.         currentImage = null;
  34.         makeFrame();
  35.     }
  36.    
  37.     private void openFile()
  38.     {
  39.         int returnVal = fileChooser.showOpenDialog(frame);
  40.        
  41.         if (returnVal != JFileChooser.APPROVE_OPTION) return;
  42.        
  43.         File selectedFile = fileChooser.getSelectedFile();
  44.         currentImage = ImageFileManager.loadImage(selectedFile);
  45.        
  46.         if (currentImage == null)
  47.         {
  48.             JOptionPane.showMessageDialog(frame, "The file was not in a recognized image file format.", "Image Load Error", JOptionPane.ERROR_MESSAGE);
  49.             return;
  50.         }
  51.        
  52.         imagePanel.setImage(currentImage);
  53.         showFilename(selectedFile.getPath());
  54.         showStatus("File loaded.");
  55.         frame.pack();
  56.     }
  57.    
  58.     private void close()
  59.     {
  60.         currentImage = null;
  61.         imagePanel.clearImage();
  62.         showFilename(null);
  63.     }
  64.    
  65.     private void quit()
  66.     {
  67.         System.exit(0);
  68.     }
  69.    
  70.     private void makeDarker()
  71.     {
  72.         if (currentImage != null)
  73.         {
  74.             currentImage.darker();
  75.             frame.repaint();
  76.             showStatus("Applied: Darker");
  77.         }
  78.        
  79.         else
  80.         {
  81.             showStatus("No image loaded.");
  82.         }
  83.     }
  84.    
  85.     private void makeLighter()
  86.     {
  87.         if (currentImage != null)
  88.         {
  89.             currentImage.lighter();
  90.             frame.repaint();
  91.             showStatus("Applied: Lighter");
  92.         }
  93.        
  94.         else
  95.         {
  96.             showStatus("No image loaded.");
  97.         }
  98.     }
  99.    
  100.     private void threshold()
  101.     {
  102.         if (currentImage != null)
  103.         {
  104.             currentImage.threshold();
  105.             frame.repaint();
  106.             showStatus("Applied: Threshold");
  107.         }
  108.        
  109.         else
  110.         {
  111.             showStatus("No image loaded.");
  112.         }
  113.     }
  114.    
  115.     private void showAbout()
  116.     {
  117.         JOptionPane.showMessageDialog(frame, "ImageViewer\n" + VERSION, "About ImageViewer", JOptionPane.INFORMATION_MESSAGE);
  118.     }
  119.    
  120.     private void showFilename(String filename)
  121.     {
  122.         if (filename == null)
  123.         {
  124.             filenameLabel.setText("No file displayed.");
  125.         }
  126.        
  127.         else
  128.         {
  129.             filenameLabel.setText("File: " + filename);
  130.         }
  131.     }
  132.    
  133.     private void showStatus(String text)
  134.     {
  135.         statusLabel.setText(text);
  136.     }
  137.    
  138.     private void makeFrame()
  139.     {
  140.         frame = new JFrame("ImageViewer");
  141.         makeMenuBar(frame);
  142.        
  143.         Container contentPane = frame.getContentPane();
  144.        
  145.         contentPane.setLayout(new BorderLayout(6 ,6));
  146.        
  147.         filenameLabel = new JLabel();
  148.         contentPane.add(filenameLabel, BorderLayout.NORTH);
  149.        
  150.         imagePanel = new ImagePanel();
  151.         contentPane.add(imagePanel, BorderLayout.CENTER);
  152.        
  153.         statusLabel = new JLabel(VERSION);
  154.         contentPane.add(statusLabel, BorderLayout.SOUTH);
  155.        
  156.         showFilename(null);
  157.         frame.pack();
  158.        
  159.         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  160.         frame.setLocation(d.width / 2 - frame.getWidth() / 2, d.height / 2 - frame.getHeight() / 2);
  161.         frame.setVisible(true);
  162.     }
  163.    
  164.     private void makeMenuBar(JFrame frame)
  165.     {
  166.         final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  167.        
  168.         JMenuBar menubar = new JMenuBar();
  169.         frame.setJMenuBar(menubar);
  170.        
  171.         JMenu menu;
  172.         JMenuItem item;
  173.        
  174.         menu = new JMenu("File");
  175.         menubar.add(menu);
  176.        
  177.         item = new JMenuItem("Open");
  178.        
  179.         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));
  180.         item.addActionListener(new ActionListener()
  181.         {
  182.             public void actionPerformed(ActionEvent e)
  183.             {
  184.                 openFile();
  185.             }
  186.         });
  187.        
  188.         menu.add(item);
  189.        
  190.         item = new JMenuItem("Close");
  191.        
  192.         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
  193.         item.addActionListener(new ActionListener()
  194.         {
  195.             public void actionPerformed(ActionEvent e)
  196.             {
  197.                 quit();
  198.             }
  199.         });
  200.        
  201.         menu.add(item);
  202.        
  203.         // Filter menu
  204.        
  205.         menu = new JMenu("Filter");
  206.         menubar.add(menu);
  207.        
  208.         item = new JMenuItem("Darker");
  209.         item.addActionListener(new ActionListener()
  210.         {
  211.             public void actionPerformed(ActionEvent e)
  212.             {
  213.                 makeDarker();
  214.             }
  215.         });
  216.        
  217.         menu.add(item);
  218.        
  219.         item = new JMenuItem("Lighter");
  220.         item.addActionListener(new ActionListener()
  221.         {
  222.             public void actionPerformed(ActionEvent e)
  223.             {
  224.                 makeLighter();
  225.             }
  226.         });
  227.        
  228.         menu.add(item);
  229.        
  230.         item = new JMenuItem("Threshold");
  231.         item.addActionListener(new ActionListener()
  232.         {
  233.             public void actionPerformed(ActionEvent e)
  234.             {
  235.                 threshold();
  236.             }
  237.         });
  238.        
  239.         menu.add(item);
  240.        
  241.         // Help menu
  242.        
  243.         menu = new JMenu("Help");
  244.         menubar.add(menu);
  245.        
  246.         item = new JMenuItem("About ImageViewer...");
  247.         item.addActionListener(new ActionListener()
  248.         {
  249.             public void actionPerformed(ActionEvent e)
  250.             {
  251.                 showAbout();
  252.             }
  253.         });
  254.        
  255.         menu.add(item);
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement