Advertisement
Guest User

OpenImage.java

a guest
Oct 1st, 2011
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.imageio.ImageIO;
  4. import javax.swing.*;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.awt.Color;
  9. import java.awt.image.ImageFilter;
  10. import java.awt.Graphics2D;
  11.  
  12.  
  13. public class OpenImage extends JFrame implements ActionListener{
  14.     /**
  15.      *
  16.      */
  17.    
  18.  
  19.     OpenImageLabel label;
  20.     Container cp;
  21.     File file;
  22.     BufferedImage bi;
  23.    
  24.     public OpenImage() throws IOException{
  25.          
  26.         super("Resize and Rotate");
  27.        
  28.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  29.         JPanel row1 = new JPanel();
  30.         JButton open = new JButton ("Open");
  31.         open.addActionListener(this);
  32.         JButton rotate = new JButton("Rotate");
  33.         rotate.addActionListener(this);
  34.         JButton resize = new JButton("Resize");
  35.         resize.addActionListener(this);
  36.         JButton exit = new JButton ("Exit");
  37.         exit.addActionListener(this);
  38.         row1.add(open);
  39.         row1.add(rotate);
  40.         row1.add(resize);
  41.         row1.add(exit);
  42.        
  43.         BorderLayout grid1 = new BorderLayout();
  44.         cp = getContentPane();
  45.         cp.setLayout(grid1);
  46.        
  47.         JPanel topPanel = new JPanel();
  48.         topPanel.setLayout( new BorderLayout() );
  49.         getContentPane().add( topPanel );
  50.        
  51.         label = new OpenImageLabel();
  52.        
  53.         JScrollPane scrollPane = new JScrollPane();
  54.         scrollPane.getViewport().add( label );
  55.         topPanel.add( scrollPane, BorderLayout.CENTER );
  56.  
  57.         JButton save = new JButton("Save");
  58.         JPanel row3 = new JPanel();
  59.         row3.add(save);
  60.        
  61.         cp.add(BorderLayout.NORTH, row1);
  62.         getContentPane().add( topPanel, BorderLayout.CENTER );
  63.         cp.add(BorderLayout.SOUTH, row3);
  64.  
  65.         pack();
  66.         setSize(900,700);
  67.         setVisible(true);  
  68.     }
  69.  
  70.  
  71.    
  72.  
  73.  
  74.    
  75.  
  76.  
  77.     // This method returns a buffered image with the contents of an image
  78.  
  79.     public void actionPerformed(ActionEvent e) {
  80.         String command = e.getActionCommand();
  81.         if (command == "Exit"){
  82.             System.exit(0);
  83.         }
  84.         if (command == "Open"){
  85.             JFileChooser chooser = new JFileChooser();
  86.             int returnVal = chooser.showOpenDialog(this);
  87.             if (returnVal == JFileChooser.APPROVE_OPTION) {
  88.                 file = chooser.getSelectedFile();
  89.                 //This is where a real application would open the file.
  90.                 label.openFile(file);
  91.                 System.out.println("Opening: " + file.getName() + "." + "\n");
  92.               } else {
  93.                 System.out.println("Open command cancelled by user." + "\n");
  94.               }
  95.         }
  96.         if(command =="Rotate"){
  97.             //We will rotate image here
  98.             label.rotateImage();
  99.             }
  100.     }
  101.  
  102.     public static void main(String [] args){
  103.         try {
  104.             // Set System L&F
  105.         UIManager.setLookAndFeel(
  106.             UIManager.getSystemLookAndFeelClassName());
  107.     }
  108.     catch (UnsupportedLookAndFeelException e) {
  109.        // handle exception
  110.     }
  111.     catch (ClassNotFoundException e) {
  112.        // handle exception
  113.     }
  114.     catch (InstantiationException e) {
  115.        // handle exception
  116.     }
  117.     catch (IllegalAccessException e) {
  118.        // handle exception
  119.     }
  120.  
  121.         try {
  122.             JFrame frame = new OpenImage();
  123.         } catch (IOException e) {
  124.             // TODO Auto-generated catch block
  125.             e.printStackTrace();
  126.         }
  127.     }
  128.    
  129. }
  130.  
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement