Advertisement
Guest User

Untitled

a guest
May 12th, 2014
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import java.awt.Dimension;
  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.JMenu;
  11. import javax.swing.JMenuBar;
  12. import javax.swing.JMenuItem;
  13.  
  14. public class MainMenu extends JMenuBar
  15. {
  16.  
  17. private MainFrame mf;
  18. private DrawPanel panel;
  19.  
  20. // creating list of options in file menu
  21. private JMenuItem jmOpenFile = new JMenuItem("Open...");
  22.  
  23. /**
  24. * main menu on the top with three buttons File, Edit and Edit image
  25. */
  26. public MainMenu(MainFrame mf, DrawPanel dp)
  27. {
  28. this.mf = mf;
  29. panel = dp;
  30. // adding button to the top menu
  31. JMenu fileMenu = new JMenu("File");
  32. this.add(fileMenu);
  33. fileMenu.add(jmOpenFile);
  34. initFile();
  35. }
  36.  
  37. private void initFile()
  38. {
  39. // here i open image from file
  40. jmOpenFile.addActionListener(new ActionListener()
  41. {
  42. public void actionPerformed(ActionEvent e)
  43. {
  44.  
  45. JFileChooser openFile = new JFileChooser();
  46. int result = openFile.showOpenDialog(openFile);
  47.  
  48. if (result == JFileChooser.APPROVE_OPTION)
  49. {
  50. File file = openFile.getSelectedFile();
  51. String pathName = file.getAbsolutePath();
  52. File imageFile = new File(pathName);
  53.  
  54. try
  55. {
  56. // here i read image from file and want to use method from drawPanel to display image on the DrawPanel
  57. BufferedImage image = ImageIO.read(imageFile);
  58. Dimension imageSize = new Dimension(image.getWidth(),
  59. image.getHeight());
  60. panel.setPreferredSize(imageSize);
  61. panel.repaint();
  62. }
  63. catch (IOException e1)
  64. {
  65. e1.printStackTrace();
  66. }
  67. }
  68. }
  69. });
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement