Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4. import javax.swing.*;
  5. import javax.swing.border.*;
  6.  
  7. import java.io.File;
  8.  
  9. import java.util.List;
  10. import java.util.ArrayList;
  11. import java.util.Iterator;
  12.  
  13. /**
  14.  * ImageViewer is the main class of the image viewer application. It builds and
  15.  * displays the application GUI and initialises all other components.
  16.  *
  17.  * To start the application, create an object of this class.
  18.  *
  19.  * @author Gede
  20.  * @version 3.0(29/11/2018)
  21.  */
  22. public class ImageViewer
  23. {
  24.     // static fields:
  25.     private static final String VERSION = "Version 3.0";
  26.     private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
  27.  
  28.     // fields:
  29.     private JFrame frame;
  30.     private ImagePanel imagePanel;
  31.     private JLabel filenameLabel;
  32.     private JLabel statusLabel;
  33.     private JButton smallerButton;
  34.     private JButton largerButton;
  35.     private OFImage currentImage;
  36.    
  37.     private List<Filter> filters;
  38.    
  39.     /**
  40.      * Create an ImageViewer and display its GUI on screen.
  41.      */
  42.     public ImageViewer()
  43.     {
  44.         currentImage = null;
  45.         filters = createFilters();
  46.         makeFrame();
  47.     }
  48.  
  49.  
  50.     // ---- implementation of menu functions ----
  51.    
  52.     /**
  53.      * Open function: open a file chooser to select a new image file,
  54.      * and then display the chosen image.
  55.      */
  56.     private void openFile()
  57.     {
  58.         int returnVal = fileChooser.showOpenDialog(frame);
  59.  
  60.         if(returnVal != JFileChooser.APPROVE_OPTION) {
  61.             return;  // cancelled
  62.         }
  63.         File selectedFile = fileChooser.getSelectedFile();
  64.         currentImage = ImageFileManager.loadImage(selectedFile);
  65.        
  66.         if(currentImage == null) {   // image file was not a valid image
  67.             JOptionPane.showMessageDialog(frame,
  68.                     "The file was not in a recognized image file format.",
  69.                     "Image Load Error",
  70.                     JOptionPane.ERROR_MESSAGE);
  71.             return;
  72.         }
  73.  
  74.         imagePanel.setImage(currentImage);
  75.         setButtonsEnabled(true);
  76.         showFilename(selectedFile.getPath());
  77.         showStatus("File loaded.");
  78.         frame.pack();
  79.     }
  80.  
  81.     /**
  82.      * Close function: close the current image.
  83.      */
  84.     private void close()
  85.     {
  86.         currentImage = null;
  87.         imagePanel.clearImage();
  88.         showFilename(null);
  89.         setButtonsEnabled(false);
  90.     }
  91.  
  92.     /**
  93.      * Save As function: save the current image to a file.
  94.      */
  95.     private void saveAs()
  96.     {
  97.         if(currentImage != null) {
  98.             int returnVal = fileChooser.showSaveDialog(frame);
  99.    
  100.             if(returnVal != JFileChooser.APPROVE_OPTION) {
  101.                 return;  // cancelled
  102.             }
  103.             File selectedFile = fileChooser.getSelectedFile();
  104.             ImageFileManager.saveImage(currentImage, selectedFile);
  105.            
  106.             showFilename(selectedFile.getPath());
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Quit function: quit the application.
  112.      */
  113.     private void quit()
  114.     {
  115.         System.exit(0);
  116.     }
  117.  
  118.     /**
  119.      * Apply a given filter to the current image.
  120.      *
  121.      * @param filter   The filter object to be applied.
  122.      */
  123.     private void applyFilter(Filter filter)
  124.     {
  125.         if(currentImage != null) {
  126.             filter.apply(currentImage);
  127.             frame.repaint();
  128.             showStatus("Applied: " + filter.getName());
  129.         }
  130.         else {
  131.             showStatus("No image loaded.");
  132.         }
  133.     }
  134.  
  135.     /**
  136.      * 'About' function: show the 'about' box.
  137.      */
  138.     private void showAbout()
  139.     {
  140.         JOptionPane.showMessageDialog(frame,
  141.                     "ImageViewer\n" + VERSION,
  142.                     "About ImageViewer",
  143.                     JOptionPane.INFORMATION_MESSAGE);
  144.     }
  145.  
  146.     /**
  147.      * Make the current picture larger.
  148.      */
  149.     private void makeLarger()
  150.     {
  151.         if(currentImage != null) {
  152.             // create new image with double size
  153.             int width = currentImage.getWidth();
  154.             int height = currentImage.getHeight();
  155.             OFImage newImage = new OFImage(width * 2, height * 2);
  156.  
  157.             // copy pixel data into new image
  158.             for(int y = 0; y < height; y++) {
  159.                 for(int x = 0; x < width; x++) {
  160.                     Color col = currentImage.getPixel(x, y);
  161.                     newImage.setPixel(x * 2, y * 2, col);
  162.                     newImage.setPixel(x * 2 + 1, y * 2, col);
  163.                     newImage.setPixel(x * 2, y * 2 + 1, col);
  164.                     newImage.setPixel(x * 2+1, y * 2 + 1, col);
  165.                 }
  166.             }
  167.            
  168.             currentImage = newImage;
  169.             imagePanel.setImage(currentImage);
  170.             frame.pack();
  171.         }
  172.     }
  173.  
  174.     /**
  175.      * Make the current picture smaller.
  176.      */
  177.     private void makeSmaller()
  178.     {
  179.         if(currentImage != null) {
  180.             // create new image with double size
  181.             int width = currentImage.getWidth() / 2;
  182.             int height = currentImage.getHeight() / 2;
  183.             OFImage newImage = new OFImage(width, height);
  184.  
  185.             // copy pixel data into new image
  186.             for(int y = 0; y < height; y++) {
  187.                 for(int x = 0; x < width; x++) {
  188.                     newImage.setPixel(x, y, currentImage.getPixel(x * 2, y * 2));
  189.                 }
  190.             }
  191.            
  192.             currentImage = newImage;
  193.             imagePanel.setImage(currentImage);
  194.             frame.pack();
  195.         }
  196.     }
  197.    
  198.     // ---- support methods ----
  199.  
  200.     /**
  201.      * Show the file name of the current image in the fils display label.
  202.      * 'null' may be used as a parameter if no file is currently loaded.
  203.      *
  204.      * @param filename  The file name to be displayed, or null for 'no file'.
  205.      */
  206.     private void showFilename(String filename)
  207.     {
  208.         if(filename == null) {
  209.             filenameLabel.setText("No file displayed.");
  210.         }
  211.         else {
  212.             filenameLabel.setText("File: " + filename);
  213.         }
  214.     }
  215.  
  216.     /**
  217.      * Show a message in the status bar at the bottom of the screen.
  218.      * @param text The message to be displayed.
  219.      */
  220.     private void showStatus(String text)
  221.     {
  222.         statusLabel.setText(text);
  223.     }
  224.  
  225.     /**
  226.      * Enable or disable all toolbar buttons.
  227.      *
  228.      * @param status  'true' to enable the buttons, 'false' to disable.
  229.      */
  230.     private void setButtonsEnabled(boolean status)
  231.     {
  232.         smallerButton.setEnabled(status);
  233.         largerButton.setEnabled(status);
  234.     }
  235.  
  236.     /**
  237.      * Create a list with all the known filters.
  238.      * @return The list of filters.
  239.      */
  240.     private List<Filter> createFilters()
  241.     {
  242.         List<Filter> filterList = new ArrayList<Filter>();
  243.         filterList.add(new DarkerFilter("Darker"));
  244.         filterList.add(new LighterFilter("Lighter"));
  245.         filterList.add(new ThresholdFilter("Threshold"));
  246.         filterList.add(new FishEyeFilter("Fish Eye"));
  247.        
  248.         return filterList;
  249.     }
  250.    
  251.     // ---- swing stuff to build the frame and all its components ----
  252.    
  253.     /**
  254.      * Create the Swing frame and its content.
  255.      */
  256.     private void makeFrame()
  257.     {
  258.         frame = new JFrame("ImageViewer");
  259.         JPanel contentPane = (JPanel)frame.getContentPane();
  260.         contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
  261.  
  262.         makeMenuBar(frame);
  263.        
  264.         // Specify the layout manager with nice spacing
  265.         contentPane.setLayout(new BorderLayout(6, 6));
  266.  
  267.         // Create the image pane in the center
  268.         imagePanel = new ImagePanel();
  269.         imagePanel.setBorder(new EtchedBorder());
  270.         contentPane.add(imagePanel, BorderLayout.CENTER);
  271.        
  272.         // Create two labels at top and bottom for the file name and status message
  273.         filenameLabel = new JLabel();
  274.         contentPane.add(filenameLabel, BorderLayout.NORTH);
  275.  
  276.         statusLabel = new JLabel(VERSION);
  277.         contentPane.add(statusLabel, BorderLayout.SOUTH);
  278.        
  279.         // Create the toolbar with the buttons
  280.         JPanel toolbar = new JPanel();
  281.         toolbar.setLayout(new GridLayout(0, 1));
  282.        
  283.         smallerButton = new JButton("Smaller");
  284.         smallerButton.addActionListener(new ActionListener() {
  285.                                public void actionPerformed(ActionEvent e) { makeSmaller(); }
  286.                            });
  287.         toolbar.add(smallerButton);
  288.        
  289.         largerButton = new JButton("Larger");
  290.         largerButton.addActionListener(new ActionListener() {
  291.                                public void actionPerformed(ActionEvent e) { makeLarger(); }
  292.                            });
  293.         toolbar.add(largerButton);
  294.  
  295.         // Add toolbar into panel with flow layout for spacing
  296.         JPanel flow = new JPanel();
  297.         flow.add(toolbar);
  298.        
  299.         contentPane.add(flow, BorderLayout.WEST);
  300.        
  301.         // building is done - arrange the components    
  302.         showFilename(null);
  303.         setButtonsEnabled(false);
  304.         frame.pack();
  305.        
  306.         // place the frame at the center of the screen and show
  307.         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  308.         frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
  309.         frame.setVisible(true);
  310.     }
  311.    
  312.     /**
  313.      * Create the main frame's menu bar.
  314.      *
  315.      * @param frame   The frame that the menu bar should be added to.
  316.      */
  317.     private void makeMenuBar(JFrame frame)
  318.     {
  319.         final int SHORTCUT_MASK =
  320.             Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  321.  
  322.         JMenuBar menubar = new JMenuBar();
  323.         frame.setJMenuBar(menubar);
  324.        
  325.         JMenu menu;
  326.         JMenuItem item;
  327.        
  328.         // create the File menu
  329.         menu = new JMenu("File");
  330.         menubar.add(menu);
  331.        
  332.         item = new JMenuItem("Open...");
  333.             item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));
  334.             item.addActionListener(new ActionListener() {
  335.                                public void actionPerformed(ActionEvent e) { openFile(); }
  336.                            });
  337.         menu.add(item);
  338.  
  339.         item = new JMenuItem("Close");
  340.             item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));
  341.             item.addActionListener(new ActionListener() {
  342.                                public void actionPerformed(ActionEvent e) { close(); }
  343.                            });
  344.         menu.add(item);
  345.         menu.addSeparator();
  346.  
  347.         item = new JMenuItem("Save As...");
  348.             item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, SHORTCUT_MASK));
  349.             item.addActionListener(new ActionListener() {
  350.                                public void actionPerformed(ActionEvent e) { saveAs(); }
  351.                            });
  352.         menu.add(item);
  353.         menu.addSeparator();
  354.        
  355.         item = new JMenuItem("Quit");
  356.             item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
  357.             item.addActionListener(new ActionListener() {
  358.                                public void actionPerformed(ActionEvent e) { quit(); }
  359.                            });
  360.         menu.add(item);
  361.  
  362.         // create the Filter menu
  363.         menu = new JMenu("Filter");
  364.         menubar.add(menu);
  365.        
  366.         for(final Filter filter : filters) {
  367.             item = new JMenuItem(filter.getName());
  368.             item.addActionListener(new ActionListener() {
  369.                                 public void actionPerformed(ActionEvent e) {
  370.                                     applyFilter(filter);
  371.                                 }
  372.                            });
  373.              menu.add(item);
  374.          }
  375.  
  376.         // create the Help menu
  377.         menu = new JMenu("Help");
  378.         menubar.add(menu);
  379.        
  380.         item = new JMenuItem("About ImageViewer...");
  381.             item.addActionListener(new ActionListener() {
  382.                                public void actionPerformed(ActionEvent e) { showAbout(); }
  383.                            });
  384.         menu.add(item);
  385.     }
  386. }