Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.imageio.ImageIO;
- import javax.swing.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.awt.Color;
- import java.awt.image.ImageFilter;
- import java.awt.Graphics2D;
- public class OpenImage extends JFrame implements ActionListener{
- /**
- *
- */
- OpenImageLabel label;
- Container cp;
- File file;
- BufferedImage bi;
- public OpenImage() throws IOException{
- super("Resize and Rotate");
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- JPanel row1 = new JPanel();
- JButton open = new JButton ("Open");
- open.addActionListener(this);
- JButton rotate = new JButton("Rotate");
- rotate.addActionListener(this);
- JButton resize = new JButton("Resize");
- resize.addActionListener(this);
- JButton exit = new JButton ("Exit");
- exit.addActionListener(this);
- row1.add(open);
- row1.add(rotate);
- row1.add(resize);
- row1.add(exit);
- BorderLayout grid1 = new BorderLayout();
- cp = getContentPane();
- cp.setLayout(grid1);
- JPanel topPanel = new JPanel();
- topPanel.setLayout( new BorderLayout() );
- getContentPane().add( topPanel );
- label = new OpenImageLabel();
- JScrollPane scrollPane = new JScrollPane();
- scrollPane.getViewport().add( label );
- topPanel.add( scrollPane, BorderLayout.CENTER );
- JButton save = new JButton("Save");
- JPanel row3 = new JPanel();
- row3.add(save);
- cp.add(BorderLayout.NORTH, row1);
- getContentPane().add( topPanel, BorderLayout.CENTER );
- cp.add(BorderLayout.SOUTH, row3);
- pack();
- setSize(900,700);
- setVisible(true);
- }
- // This method returns a buffered image with the contents of an image
- public void actionPerformed(ActionEvent e) {
- String command = e.getActionCommand();
- if (command == "Exit"){
- System.exit(0);
- }
- if (command == "Open"){
- JFileChooser chooser = new JFileChooser();
- int returnVal = chooser.showOpenDialog(this);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- file = chooser.getSelectedFile();
- //This is where a real application would open the file.
- label.openFile(file);
- System.out.println("Opening: " + file.getName() + "." + "\n");
- } else {
- System.out.println("Open command cancelled by user." + "\n");
- }
- }
- if(command =="Rotate"){
- //We will rotate image here
- label.rotateImage();
- }
- }
- public static void main(String [] args){
- try {
- // Set System L&F
- UIManager.setLookAndFeel(
- UIManager.getSystemLookAndFeelClassName());
- }
- catch (UnsupportedLookAndFeelException e) {
- // handle exception
- }
- catch (ClassNotFoundException e) {
- // handle exception
- }
- catch (InstantiationException e) {
- // handle exception
- }
- catch (IllegalAccessException e) {
- // handle exception
- }
- try {
- JFrame frame = new OpenImage();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement