document.write('
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. * ImageFileManager is a small utility class with static methods to load
  8. * and save images.
  9. *
  10. * The files on disk can be in JPG or PNG image format. For files written
  11. * by this class, the format is determined by the constant IMAGE_FORMAT.
  12. *
  13. * @author Vania Meilani T.
  14. * @version 1.0
  15. */
  16. public class ImageViewer
  17. {
  18. private static final String VERSION = "Version 1.0";
  19. private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
  20. private JFrame frame;
  21. private ImagePanel imagePanel;
  22. private JLabel filenameLabel;
  23. private JLabel statusLabel;
  24. private OFImage currentImage;
  25. public ImageViewer()
  26. {
  27. currentImage = null;
  28. makeFrame();
  29. }
  30. private void openFile()
  31. {
  32. int returnVal = fileChooser.showOpenDialog(frame);
  33. if(returnVal != JFileChooser.APPROVE_OPTION) {
  34. return;
  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. private void close()
  51. {
  52. currentImage = null;
  53. imagePanel.clearImage();
  54. showFilename(null);
  55. }
  56. private void quit()
  57. {
  58. System.exit(0);
  59. }
  60. private void makeDarker()
  61. {
  62. if(currentImage != null) {
  63. currentImage.darker();
  64. frame.repaint();
  65. showStatus("Applied: darker");
  66. }
  67. else {
  68. showStatus("No image loaded.");
  69. }
  70. }
  71. private void makeLighter()
  72. {
  73. if(currentImage != null) {
  74. currentImage.lighter();
  75. frame.repaint();
  76. showStatus("Applied: lighter");
  77. }
  78. else {
  79. showStatus("No image loaded.");
  80. }
  81. }
  82. private void threshold()
  83. {
  84. if(currentImage != null) {
  85. currentImage.threshold();
  86. frame.repaint();
  87. showStatus("Applied: threshold");
  88. }
  89. else {
  90. showStatus("No image loaded.");
  91. }
  92. }
  93. private void showAbout()
  94. {
  95. JOptionPane.showMessageDialog(frame,
  96. "ImageViewer\\n" + VERSION,
  97. "About ImageViewer",
  98. JOptionPane.INFORMATION_MESSAGE);
  99. }
  100. private void showFilename(String filename)
  101. {
  102. if(filename == null) {
  103. filenameLabel.setText("No file displayed.");
  104. }
  105. else {
  106. filenameLabel.setText("File: " + filename);
  107. }
  108. }
  109. private void showStatus(String text)
  110. {
  111. statusLabel.setText(text);
  112. }
  113. private void makeFrame()
  114. {
  115. frame = new JFrame("ImageViewer");
  116. makeMenuBar(frame);
  117. Container contentPane = frame.getContentPane();
  118. contentPane.setLayout(new BorderLayout(6, 6));
  119. filenameLabel = new JLabel();
  120. contentPane.add(filenameLabel, BorderLayout.NORTH);
  121. imagePanel = new ImagePanel();
  122. contentPane.add(imagePanel, BorderLayout.CENTER);
  123. statusLabel = new JLabel(VERSION);
  124. contentPane.add(statusLabel, BorderLayout.SOUTH);
  125. showFilename(null);
  126. frame.pack();
  127. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  128. frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
  129. frame.setVisible(true);
  130. }
  131. private void makeMenuBar(JFrame frame)
  132. {
  133. final int SHORTCUT_MASK =
  134. Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  135. JMenuBar menubar = new JMenuBar();
  136. frame.setJMenuBar(menubar);
  137. JMenu menu;
  138. JMenuItem item;
  139. menu = new JMenu("File");
  140. menubar.add(menu);
  141. item = new JMenuItem("Open");
  142. item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));
  143. item.addActionListener(new ActionListener() {
  144. public void actionPerformed(ActionEvent e) { openFile(); }
  145. });
  146. menu.add(item);
  147. item = new JMenuItem("Close");
  148. item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));
  149. item.addActionListener(new ActionListener() {
  150. public void actionPerformed(ActionEvent e) { close(); }
  151. });
  152. menu.add(item);
  153. menu.addSeparator();
  154. item = new JMenuItem("Quit");
  155. item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
  156. item.addActionListener(new ActionListener() {
  157. public void actionPerformed(ActionEvent e) { quit(); }
  158. });
  159. menu.add(item);
  160. menu = new JMenu("Filter");
  161. menubar.add(menu);
  162. item = new JMenuItem("Darker");
  163. item.addActionListener(new ActionListener() {
  164. public void actionPerformed(ActionEvent e) { makeDarker(); }
  165. });
  166. menu.add(item);
  167. item = new JMenuItem("Lighter");
  168. item.addActionListener(new ActionListener() {
  169. public void actionPerformed(ActionEvent e) { makeLighter(); }
  170. });
  171. menu.add(item);
  172. item = new JMenuItem("Threshold");
  173. item.addActionListener(new ActionListener() {
  174. public void actionPerformed(ActionEvent e) { threshold(); }
  175. });
  176. menu.add(item);
  177. menu = new JMenu("Help");
  178. menubar.add(menu);
  179. item = new JMenuItem("About ImageViewer...");
  180. item.addActionListener(new ActionListener() {
  181. public void actionPerformed(ActionEvent e) { showAbout(); }
  182. });
  183. menu.add(item);
  184. }
  185. }
');