Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. import java.awt.Graphics;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7.  
  8. import javax.imageio.ImageIO;
  9. import javax.swing.JFileChooser;
  10. import javax.swing.JFrame;
  11. import javax.swing.JMenu;
  12. import javax.swing.JMenuBar;
  13. import javax.swing.JMenuItem;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JPanel;
  16. import javax.swing.SwingUtilities;
  17. import javax.swing.UIManager;
  18. import javax.swing.UnsupportedLookAndFeelException;
  19.  
  20. public class MainClass
  21. {
  22. public static void main(String[] args)
  23. {
  24. // TODO Auto-generated method stub
  25. try {
  26. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  27. } catch (ClassNotFoundException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. } catch (InstantiationException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. } catch (IllegalAccessException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. } catch (UnsupportedLookAndFeelException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. }
  40. SwingUtilities.invokeLater(new Runnable()
  41. {
  42. @Override
  43. public void run()
  44. {
  45. My_UI mui=new My_UI("");
  46. mui.Show(1024, 768);
  47. }
  48. });
  49. }
  50.  
  51. static class My_UI
  52. {
  53. private JFrame main_frame;
  54. private JMenuBar menuBar;
  55. private JMenu menu;
  56. private JMenuItem menuItemOpen,menuItemClose,menuItemExit;
  57. private Viewer img_view;
  58.  
  59. private void BuildUI()
  60. {
  61. menuBar = new JMenuBar();
  62. menu = new JMenu("Файл");
  63. menuBar.add(menu);
  64. menuItemOpen = new JMenuItem("Загрузити зображення");
  65. menuItemOpen.addActionListener(new ActionListener()
  66. {
  67. @Override
  68. public void actionPerformed(ActionEvent event)
  69. {
  70. JFileChooser fc = new JFileChooser();
  71. int returnVal=fc.showOpenDialog(main_frame);
  72. if (returnVal == JFileChooser.APPROVE_OPTION)
  73. {
  74. File file = fc.getSelectedFile();
  75. img_view.clean();
  76. img_view.setImage(file);
  77. }
  78. }
  79. });
  80. menu.add(menuItemOpen);
  81. menu.addSeparator();
  82. menuItemClose = new JMenuItem("Очистити");
  83. menuItemClose.addActionListener(new ActionListener()
  84. {
  85. @Override
  86. public void actionPerformed(ActionEvent event)
  87. {
  88. img_view.clean();
  89. }
  90. });
  91. menu.add(menuItemClose);
  92. menu.addSeparator();
  93. menuItemExit = new JMenuItem("Вихід");
  94. menuItemExit.addActionListener(new ActionListener()
  95. {
  96. @Override
  97. public void actionPerformed(ActionEvent event)
  98. {
  99. main_frame.dispose();
  100. }
  101. });
  102. menu.add(menuItemExit);
  103. main_frame.setJMenuBar(menuBar);
  104. img_view = new Viewer();
  105. main_frame.add(img_view);
  106. }
  107.  
  108. public My_UI(String wnd_title)
  109. {
  110. main_frame=new JFrame(wnd_title);
  111. main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  112. }
  113. public void Show(int widht,int heigt)
  114. {
  115. main_frame.setBounds(0, 0, widht, heigt);
  116. BuildUI();
  117. main_frame.setVisible(true);
  118. }
  119.  
  120. static class Viewer extends JPanel
  121. {
  122. /**
  123. *
  124. */
  125. private static final long serialVersionUID = -1050612360192626088L;
  126. private BufferedImage image;
  127. public Viewer()
  128. {
  129. image=null;
  130. }
  131. public void paint(Graphics g)
  132. {
  133. if(this.image!=null)
  134. {
  135. g.drawImage(image, 0, 0,g.getClipBounds().width,g.getClipBounds().height, null);
  136. }
  137. }
  138. public void setImage(File iFile)
  139. {
  140. try
  141. {
  142. this.image=ImageIO.read(iFile);
  143. this.repaint();
  144. }
  145. catch (IOException e)
  146. {
  147. // TODO Auto-generated catch block
  148. JOptionPane.showMessageDialog(this.getParent(),e.toString(),"Error",JOptionPane.ERROR_MESSAGE);
  149. e.printStackTrace();
  150. }
  151. }
  152. public void clean()
  153. {
  154. if(this.image!=null)
  155. {
  156. this.image.flush();
  157. this.image=null;
  158. this.repaint();
  159. }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement