Advertisement
redvelvee

Untitled

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