Advertisement
reyhanzo

Untitled

Dec 15th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.61 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.   * ImageViewer is the main class of the image viewer application. It builds and  
  8.   * displays the application GUI and initialises all other components.  
  9.   *  
  10.   * To start the application, create an object of this class.  
  11.   *  
  12.   * @author Michael Kolling and David J Barnes  
  13.   * @version 1.0  
  14.   */  
  15.  public class ImageViewer  
  16.  {  
  17.    // static fields:  
  18.    private static final String VERSION = "Version 1.0";  
  19.    private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));  
  20.    // fields:  
  21.    private JFrame frame;  
  22.    private ImagePanel imagePanel;  
  23.    private JLabel filenameLabel;  
  24.    private JLabel statusLabel;  
  25.    private OFImage currentImage;  
  26.    /**  
  27.     * Create an ImageViewer show it on screen.  
  28.     */  
  29.    public ImageViewer()  
  30.    {  
  31.      currentImage = null;  
  32.      makeFrame();  
  33.    }  
  34.    // ---- implementation of menu functions ----  
  35.    /**  
  36.     * Open function: open a file chooser to select a new image file.  
  37.     */  
  38.    private void openFile()  
  39.    {  
  40.      int returnVal = fileChooser.showOpenDialog(frame);  
  41.      if(returnVal != JFileChooser.APPROVE_OPTION) {  
  42.        return; // cancelled  
  43.      }  
  44.      File selectedFile = fileChooser.getSelectedFile();  
  45.      currentImage = ImageFileManager.loadImage(selectedFile);  
  46.      if(currentImage == null) {  // image file was not a valid image  
  47.        JOptionPane.showMessageDialog(frame,  
  48.            "The file was not in a recognized image file format.",  
  49.            "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.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement