Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.event.ChangeEvent;
- import javax.swing.event.ChangeListener;
- import java.awt.*;
- import java.awt.Image;
- import java.awt.image.*;
- import java.awt.event.*;
- import java.io.*;
- import javax.imageio.*;
- class ImgArea extends Canvas {
- BufferedImage bi;
- File f = null;
- public void paint(Graphics g) {
- Graphics2D g2d = (Graphics2D)g;
- g2d.drawImage(bi, 0, 0, null);
- }
- public void readImage(String path) throws IOException {
- f = new File(path);
- bi = ImageIO.read(f);
- }
- public void saveImage(String path) throws IOException {
- f = new File(path);
- ImageIO.write(bi, "jpg", f);
- }
- }
- public class Biometry extends JFrame implements ActionListener, ChangeListener {
- JMenuBar menuBar;
- JMenu file, edit;
- JMenuItem open, save, exit, zoom, changePixel, histograms;
- JSlider sZoom;
- float valueZoom;
- int width, height;
- BufferedImage image = null;
- File f = null;
- ImgArea ia;
- public Biometry() {
- setTitle("Biometry");
- ia = new ImgArea();
- Container cont = getContentPane();
- cont.add(ia, BorderLayout.CENTER);
- setExtendedState(this.getExtendedState()|this.MAXIMIZED_BOTH);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(null);
- menuBar = new JMenuBar();
- setJMenuBar(menuBar);
- file = new JMenu("File");
- file.setMnemonic(KeyEvent.VK_F);
- menuBar.add(file);
- open = new JMenuItem("Open", 'O');
- file.add(open);
- open.addActionListener(this);
- save = new JMenuItem("Save", 'S');
- file.add(save);
- save.addActionListener(this);
- exit = new JMenuItem("Exit", 'X');
- file.add(exit);
- exit.addActionListener(this);
- edit = new JMenu("Edit");
- edit.setMnemonic(KeyEvent.VK_E);
- menuBar.add(edit);
- zoom = new JMenuItem("Zoom", 'Z');
- edit.add(zoom);
- zoom.addActionListener(this);
- changePixel = new JMenuItem("Change pixel color", 'C');
- edit.add(changePixel);
- histograms = new JMenuItem("Histograms", 'H');
- edit.add(histograms);
- ia.requestFocus();
- }
- public void sliderZoom() {
- sZoom = new JSlider(0, 10, 0);
- sZoom.setBounds(10, 10, 100, 20);
- sZoom.setMajorTickSpacing(1);
- sZoom.setPaintTicks(true);
- sZoom.addChangeListener(this);
- add(sZoom);
- }
- public void actionPerformed(ActionEvent e) {
- Object source = e.getSource();
- if (source==open) {
- JFileChooser fc = new JFileChooser();
- if (fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
- File file = fc.getSelectedFile();
- try {
- ia.readImage(file.getAbsolutePath());
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- else if (source==save) {
- JFileChooser fc = new JFileChooser();
- if (fc.showSaveDialog(null)==JFileChooser.APPROVE_OPTION) {
- File file = fc.getSelectedFile();
- try {
- ia.saveImage(file.getAbsolutePath());
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- else if (source==exit) {
- int answer = JOptionPane.showConfirmDialog(null, "You want exit?", "Question", JOptionPane.YES_NO_OPTION);
- if (answer == JOptionPane.YES_OPTION)
- dispose();
- }
- else if (source==zoom) {
- sliderZoom();
- }
- }
- public void stateChanged(ChangeEvent e) {
- valueZoom = sZoom.getValue();
- }
- public static void main(String[] args) {
- Biometry window = new Biometry();
- window.setVisible(true);
- }
- }
Add Comment
Please, Sign In to add comment