Guest User

Untitled

a guest
Mar 14th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.event.ChangeEvent;
  3. import javax.swing.event.ChangeListener;
  4. import java.awt.*;
  5. import java.awt.Image;
  6. import java.awt.image.*;
  7. import java.awt.event.*;
  8. import java.io.*;
  9. import javax.imageio.*;
  10.  
  11. class ImgArea extends Canvas {
  12.     BufferedImage bi;
  13.     File f = null;
  14.    
  15.     public void paint(Graphics g) {
  16.         Graphics2D g2d = (Graphics2D)g;
  17.         g2d.drawImage(bi, 0, 0, null);
  18.     }
  19.    
  20.     public void readImage(String path) throws IOException {
  21.         f = new File(path);
  22.         bi = ImageIO.read(f);
  23.     }
  24.    
  25.     public void saveImage(String path) throws IOException {
  26.         f = new File(path);
  27.         ImageIO.write(bi, "jpg", f);
  28.     }
  29. }
  30.  
  31. public class Biometry extends JFrame implements ActionListener, ChangeListener {
  32.     JMenuBar menuBar;
  33.     JMenu file, edit;
  34.     JMenuItem open, save, exit, zoom, changePixel, histograms;
  35.     JSlider sZoom;
  36.     float valueZoom;
  37.     int width, height;
  38.     BufferedImage image = null;
  39.     File f = null;
  40.     ImgArea ia;
  41.    
  42.     public Biometry() {
  43.         setTitle("Biometry");
  44.         ia = new ImgArea();
  45.         Container cont = getContentPane();
  46.         cont.add(ia, BorderLayout.CENTER);
  47.         setExtendedState(this.getExtendedState()|this.MAXIMIZED_BOTH);
  48.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49.         setLayout(null);
  50.        
  51.         menuBar = new JMenuBar();
  52.         setJMenuBar(menuBar);
  53.        
  54.         file = new JMenu("File");
  55.         file.setMnemonic(KeyEvent.VK_F);
  56.         menuBar.add(file);
  57.         open = new JMenuItem("Open", 'O');
  58.         file.add(open);
  59.         open.addActionListener(this);
  60.         save = new JMenuItem("Save", 'S');
  61.         file.add(save);
  62.         save.addActionListener(this);
  63.         exit = new JMenuItem("Exit", 'X');
  64.         file.add(exit);
  65.         exit.addActionListener(this);
  66.        
  67.         edit = new JMenu("Edit");
  68.         edit.setMnemonic(KeyEvent.VK_E);
  69.         menuBar.add(edit);
  70.         zoom = new JMenuItem("Zoom", 'Z');
  71.         edit.add(zoom);
  72.         zoom.addActionListener(this);
  73.         changePixel = new JMenuItem("Change pixel color", 'C');
  74.         edit.add(changePixel);
  75.         histograms = new JMenuItem("Histograms", 'H');
  76.         edit.add(histograms);
  77.         ia.requestFocus();
  78.     }
  79.    
  80.     public void sliderZoom() {
  81.         sZoom = new JSlider(0, 10, 0);
  82.         sZoom.setBounds(10, 10, 100, 20);
  83.         sZoom.setMajorTickSpacing(1);
  84.         sZoom.setPaintTicks(true);
  85.         sZoom.addChangeListener(this);
  86.         add(sZoom);
  87.     }
  88.    
  89.     public void actionPerformed(ActionEvent e) {
  90.         Object source = e.getSource();
  91.         if (source==open) {
  92.             JFileChooser fc = new JFileChooser();
  93.             if (fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
  94.                 File file = fc.getSelectedFile();
  95.                 try {
  96.                     ia.readImage(file.getAbsolutePath());
  97.                 } catch (IOException e1) {
  98.                     e1.printStackTrace();
  99.                 }
  100.             }
  101.         }
  102.         else if (source==save) {
  103.             JFileChooser fc = new JFileChooser();
  104.             if (fc.showSaveDialog(null)==JFileChooser.APPROVE_OPTION) {
  105.                 File file = fc.getSelectedFile();
  106.                 try {
  107.                     ia.saveImage(file.getAbsolutePath());
  108.                 } catch (IOException e1) {
  109.                     e1.printStackTrace();
  110.                 }
  111.             }
  112.         }
  113.         else if (source==exit) {
  114.             int answer = JOptionPane.showConfirmDialog(null, "You want exit?", "Question", JOptionPane.YES_NO_OPTION);
  115.             if (answer == JOptionPane.YES_OPTION)
  116.                 dispose();
  117.         }
  118.         else if (source==zoom) {
  119.             sliderZoom();
  120.         }
  121.     }
  122.    
  123.     public void stateChanged(ChangeEvent e) {
  124.         valueZoom = sZoom.getValue();
  125.     }
  126.    
  127.     public static void main(String[] args) {
  128.         Biometry window = new Biometry();
  129.         window.setVisible(true);
  130.     }
  131. }
Add Comment
Please, Sign In to add comment