RickyS20_

Untitled

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