Advertisement
Guest User

Untitled

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