Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JFileChooser;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- public class MainMenu extends JMenuBar
- {
- private MainFrame mf;
- private DrawPanel panel;
- // creating list of options in file menu
- private JMenuItem jmOpenFile = new JMenuItem("Open...");
- /**
- * main menu on the top with three buttons File, Edit and Edit image
- */
- public MainMenu(MainFrame mf, DrawPanel dp)
- {
- this.mf = mf;
- panel = dp;
- // adding button to the top menu
- JMenu fileMenu = new JMenu("File");
- this.add(fileMenu);
- fileMenu.add(jmOpenFile);
- initFile();
- }
- private void initFile()
- {
- // here i open image from file
- jmOpenFile.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- JFileChooser openFile = new JFileChooser();
- int result = openFile.showOpenDialog(openFile);
- if (result == JFileChooser.APPROVE_OPTION)
- {
- File file = openFile.getSelectedFile();
- String pathName = file.getAbsolutePath();
- File imageFile = new File(pathName);
- try
- {
- // here i read image from file and want to use method from drawPanel to display image on the DrawPanel
- BufferedImage image = ImageIO.read(imageFile);
- Dimension imageSize = new Dimension(image.getWidth(),
- image.getHeight());
- panel.setPreferredSize(imageSize);
- panel.repaint();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement