Advertisement
Guest User

Class Image Viewer

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