Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Graphics;
- 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.JFrame;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.SwingUtilities;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- public class MainClass
- {
- public static void main(String[] args)
- {
- // TODO Auto-generated method stub
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (UnsupportedLookAndFeelException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- SwingUtilities.invokeLater(new Runnable()
- {
- @Override
- public void run()
- {
- My_UI mui=new My_UI("");
- mui.Show(1024, 768);
- }
- });
- }
- static class My_UI
- {
- private JFrame main_frame;
- private JMenuBar menuBar;
- private JMenu menu;
- private JMenuItem menuItemOpen,menuItemClose,menuItemExit;
- private Viewer img_view;
- private void BuildUI()
- {
- menuBar = new JMenuBar();
- menu = new JMenu("Файл");
- menuBar.add(menu);
- menuItemOpen = new JMenuItem("Загрузити зображення");
- menuItemOpen.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent event)
- {
- JFileChooser fc = new JFileChooser();
- int returnVal=fc.showOpenDialog(main_frame);
- if (returnVal == JFileChooser.APPROVE_OPTION)
- {
- File file = fc.getSelectedFile();
- img_view.clean();
- img_view.setImage(file);
- }
- }
- });
- menu.add(menuItemOpen);
- menu.addSeparator();
- menuItemClose = new JMenuItem("Очистити");
- menuItemClose.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent event)
- {
- img_view.clean();
- }
- });
- menu.add(menuItemClose);
- menu.addSeparator();
- menuItemExit = new JMenuItem("Вихід");
- menuItemExit.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent event)
- {
- main_frame.dispose();
- }
- });
- menu.add(menuItemExit);
- main_frame.setJMenuBar(menuBar);
- img_view = new Viewer();
- main_frame.add(img_view);
- }
- public My_UI(String wnd_title)
- {
- main_frame=new JFrame(wnd_title);
- main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void Show(int widht,int heigt)
- {
- main_frame.setBounds(0, 0, widht, heigt);
- BuildUI();
- main_frame.setVisible(true);
- }
- static class Viewer extends JPanel
- {
- /**
- *
- */
- private static final long serialVersionUID = -1050612360192626088L;
- private BufferedImage image;
- public Viewer()
- {
- image=null;
- }
- public void paint(Graphics g)
- {
- if(this.image!=null)
- {
- g.drawImage(image, 0, 0,g.getClipBounds().width,g.getClipBounds().height, null);
- }
- }
- public void setImage(File iFile)
- {
- try
- {
- this.image=ImageIO.read(iFile);
- this.repaint();
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- JOptionPane.showMessageDialog(this.getParent(),e.toString(),"Error",JOptionPane.ERROR_MESSAGE);
- e.printStackTrace();
- }
- }
- public void clean()
- {
- if(this.image!=null)
- {
- this.image.flush();
- this.image=null;
- this.repaint();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement