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