Results 1 to 4 of 4
  1. #1
    lackofcolor is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default Rotating an image

    Hello. I'm new in working with images. And I've been trying to work out how to rotate an image by 90degrees to the right or to the left. Could someone please help me with this? Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you yet gone through the Sun 2D graphics tutorials? If not, then you're in for a treat. Check out this section on transformations: Transforming Shapes, Text, and Images (The Java™ Tutorials > 2D Graphics > Advanced Topics in Java2D)

  3. #3
    lackofcolor is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    I've been busy with the previous weeks and AffineTransform is just really hard. Well, here's what I've done so far:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;
    import java.awt.image.*;
    import javax.imageio.*;
    import javax.swing.*;
    import java.io.*;
    import javax.swing.filechooser.*;
    
    public class ImageViewer extends JFrame {
    
      private AffineTransform at = new AffineTransform();
      private Buttons buttons = new Buttons();
    
      private JButton zoomIn      = new JButton("Zoom In");
      private JButton zoomOut     = new JButton("Zoom Out");
      private JButton rotateLeft  = new JButton("Rotate Left");
      private JButton rotateRight = new JButton("Rotate Right"); 
      private JButton reset       = new JButton("Reset"); 
      
      private JLabel canvas = new JLabel();
      
      private JMenu fileMenu = new JMenu("File"); 
      private JMenu createMenu = new JMenu("Create"); 
      private JMenuBar menuBar = new JMenuBar();
      
      private JMenuItem open;
      private JMenuItem add;
      private JMenuItem collage;
      
      private JFileChooser imageChooser;
    	
      private Image image;
      
      private int width;
      private int height;
      
      private Container container;
      
      private JPanel imgPnl = new JPanel();
      
      private double scale = 1.0;
      private int    angle = 0;
     
      public static void main(String args[]) {
        JFrame jFrame = new ImageViewer();
        jFrame.setVisible(true);
      }
      
      public ImageViewer() {
        super("ImageViewer");
        PicManagerLayout();
        PicManagerHandler();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    	setSize(500,500);
        setExtendedState(MAXIMIZED_BOTH);
        setLocationRelativeTo(null);
      }
      
      private void PicManagerLayout() {
    	buttons.disable(zoomIn, zoomOut, rotateLeft, rotateRight, reset);
      
        JPanel optionsPnl = new JPanel(new GridLayout(1, 0, 5, 5));
        optionsPnl.add(zoomIn);
        optionsPnl.add(zoomOut);
        optionsPnl.add(rotateLeft);
        optionsPnl.add(rotateRight);
        optionsPnl.add(reset);
        
        JPanel northPnl = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
        northPnl.add(optionsPnl);
        northPnl.setBackground(SystemColor.control);
    	
    	//JPanel sidePnl = new JPanel(new GridLayout(10 ,0 ,4, 4));
    	//labels[0].setIcon(new ImageIcon(image));
    	//sidePnl.add(labels[0]);
        
        container = getContentPane();
        container.setLayout(new BorderLayout());
        container.add(new JScrollPane(canvas), BorderLayout.CENTER);
        container.add(northPnl, BorderLayout.NORTH);
    	//container.add(sidePnl, BorderLayout.WEST);
    	
    	open = new JMenuItem("Open");
    	add = new JMenuItem("Add Image");
    	collage = new JMenuItem("Create collage");
    	fileMenu.add(open);
    	fileMenu.add(add);
    	createMenu.add(collage);
    	menuBar.add(fileMenu);
    	menuBar.add(createMenu);
    	setJMenuBar(menuBar);
      }
      
      public void labelIcon(Image image){
        canvas.setHorizontalAlignment(JLabel.CENTER);
        canvas.setVerticalAlignment(JLabel.CENTER);
        canvas.setIcon(new ImageIcon(image));
      }
      
      private void PicManagerHandler() {
        zoomIn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            scale *= 1.5;
            applyTransform(image);
          }
        });
        zoomOut.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            scale *= 2.0/3.0;
            applyTransform(image);
          }
        });
        rotateLeft.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            angle = (angle - 1) % 4;
            applyTransform(image);
          }
        });
        rotateRight.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            angle = (angle + 1) % 4;
            applyTransform(image);
          }
        });
        reset.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            scale = 1.0;
            angle = 0;
            applyTransform(image);
          }
        });
    	open.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent openev) {
            imageChooser = new JFileChooser();
    		imageChooser.setCurrentDirectory(new File("."));
    
    		imageChooser.setFileFilter(new ImageFilter());
    		
    		int chooser = imageChooser.showOpenDialog(container);
    		
    		if(chooser == JFileChooser.APPROVE_OPTION){
    			String name = imageChooser.getSelectedFile().getPath();
    			loadImage(name);
    		}
          }
        });
      }
      
      private void loadImage(String file) {
    	buttons.enable(zoomIn, zoomOut, rotateLeft, rotateRight, reset);
        try {
          image  = ImageIO.read(new File(file));
          width  = image.getWidth(null);
          height = image.getHeight(null);
    	  int fitWidth = this.getWidth();
    	  int fitHeight = this.getWidth();
    		if(width > fitWidth){
    			width = fitWidth;
    		}
    		if(height > fitHeight){
    			height = fitHeight;
    		}
        } catch (IOException ioex) {
          System.out.println("Cannot load image: \"" + file + "\"");
          System.exit(-1);
        }
    	labelIcon(image);
    	scale = 1.0;
    	angle = 0;
    	applyTransform(image);
      }
      
      private void applyTransform(Image image) {
        AffineTransform at = new AffineTransform();
        at.scale(scale, scale);
        at.rotate(0.5 * angle * Math.PI, width/2, height/2);
    	
        int w = Math.min(2000, (int) (scale * width));
        int h = Math.min(2000, (int) (scale * height));
          
        BufferedImage buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D  graphics = (Graphics2D) buffer.getGraphics();
        graphics.drawImage(image, at, null);
    	labelIcon(buffer);
      }
    }
    Java Code:
    import java.io.File;
    
    public class ImageFilter extends javax.swing.filechooser.FileFilter{
    	public boolean accept(File f){
    		String fileN = f.getName();
    		if(fileN.toLowerCase().endsWith(".jpg") ||
    		   fileN.toLowerCase().endsWith(".jpeg") ||	
    		   fileN.toLowerCase().endsWith(".gif") ||
    		   fileN.toLowerCase().endsWith(".png") ||
    		   f.isDirectory()
    		){
    			return true;
    		}else{
    			return false;
    		}
    	}
    	
    	public String getDescription(){
    		return "Images Only";
    	}
      }
    Java Code:
    import javax.swing.JButton;
    
    public class Buttons{
    	public void enable(JButton btn1, JButton btn2, JButton btn3, JButton btn4, JButton btn5){
    		btn1.setEnabled(true);
    		btn2.setEnabled(true);
    		btn3.setEnabled(true);
    		btn4.setEnabled(true);
    		btn5.setEnabled(true);
    	}
    	
    	public void disable(JButton btn1, JButton btn2, JButton btn3, JButton btn4, JButton btn5){
    		btn1.setEnabled(false);
    		btn2.setEnabled(false);
    		btn3.setEnabled(false);
    		btn4.setEnabled(false);
    		btn5.setEnabled(false);
    	}
    
    }
    Yeah, I made three classes so far, to implement OOP more. Anyway, please help me to solve the thing when rotating the image - try, and you'll see black at the sides and that the image will be cropped.

    Could someone also be kindly explain transform, translate, concatenate methods in detail? Maybe these methods can be helpful in my project.

    Thanks in advance.

  4. #4
    ScottVal is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Default

    I've been doing some work with images, transforms, rotations, etc. -- if you're still interested, we could collaborate. -Scott

Similar Threads

  1. Canvas Image popups another image (SWT)
    By SpaceY in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 02:25 PM
  2. Rotating objects in Java Applets
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-17-2008, 08:35 AM
  3. Rotating and flipping an image in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 09:01 PM
  4. Replies: 0
    Last Post: 01-08-2008, 06:29 PM
  5. Two Problems Rotating and collision detection help
    By jaferris in forum Java Applets
    Replies: 2
    Last Post: 01-08-2008, 12:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •