Results 1 to 4 of 4
  1. #1
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Change dimension of image

    Hi. I want to write small program that loads picture from file but not in original size but in size that I want.
    (I think this small program would be helpful for my main goal and that is zoom in and zoom out loaded image.)

    So far I have written program that displays loaded image with original size.

    class: Picture.java
    Java Code:
    import javax.imageio.ImageIO;
    
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    public class Picture extends Component {
    	
    	BufferedImage picture;
    	
    	public Picture() {
    		try {
    			picture = ImageIO.read(new File("little_bird.jpg"));
    		} catch(IOException e) {
    			e.printStackTrace();
    			System.exit(1);
    		}
    	}
    	
    	public void paint(Graphics g) {
    		g.drawImage(picture,0,0,null);
    	}	
    }


    class: ZoomExample.java
    Java Code:
    import java.awt.Dimension;
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class ZoomExample2 {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				ZoomFrame frame = new ZoomFrame();
    				frame.prik();
    			}
    		});
    	}
    }
    
    class ZoomFrame extends JFrame {
    	Picture slika = new Picture();
    	public ZoomFrame() {
    		getContentPane().add(slika);
    	}
    	
    	public void prik() {
    		setTitle("Example");
    		setSize(new Dimension(800,600));
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
    }


    Is there any way to change the size of original image that is loaded?

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I have written program that can show image at any size, and can zoomIn and zoomOut:


    class: Picture.java

    Java Code:
    import javax.imageio.ImageIO;
    
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    
    public class Picture extends Component {
    	
    	Image picture;
    	
    	private int newWidth;
    	private int newHeight;
    	
    	public Picture() {
    		
    		try {
    			picture = ImageIO.read(new File("little_bird.jpg"));
    			
    			setNewPictureSize(0.5);
    		} catch(IOException e) {
    			e.printStackTrace();
    			System.exit(1);
    		}
    	}
    	
    	public void setNewPictureSize(double coefficient) {
    		newWidth = (int)(picture.getWidth(null) * coefficient);
    		newHeight = (int)(picture.getHeight(null) * coefficient);
    		
    		picture = picture.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
    	}
    	
    	public void paint(Graphics g) {
    		g.drawImage(picture,0,0,null);
    	}
    }


    class: ZoomExample2.java

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class ZoomExample2 {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				ZoomFrame frame = new ZoomFrame();
    				frame.prik();
    			}
    		});
    	}
    }
    
    class ZoomFrame extends JFrame {
    	ZoomPanel zoomPanel;
    	
    	Picture pict = new Picture();
    	public ZoomFrame() {
    		getContentPane().add(pict);
    		
    		zoomPanel = new ZoomPanel();
    		getContentPane().add(zoomPanel, BorderLayout.SOUTH);
    	}
    	
    	public void prik() {
    		setTitle("ZoomIn / ZoomOut");
    		setSize(new Dimension(800,600));
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
    	
    	private class ZoomPanel extends JPanel {
    		JButton zoomIn;
    		JButton zoomOut;
    		public static final double ZOOM_IN = 1.10;
    		public static final double ZOOM_OUT = 0.90;
    		
    		public ZoomPanel() {
    			
    			zoomIn = new JButton("+");
    			add(zoomIn, BorderLayout.SOUTH);
    			
    			zoomIn.addActionListener(new ActionListener() {
    
    				@Override
    				public void actionPerformed(ActionEvent arg0) {
    				
    					pict.setNewPictureSize(ZOOM_IN);
    					pict.validate();
    					pict.repaint();
    				}
    				
    			});
    			
    			zoomOut = new JButton("-");
    			add(zoomOut, BorderLayout.EAST);
    			
    			zoomOut.addActionListener(new ActionListener() {
    				
    				@Override
    				public void actionPerformed(ActionEvent e) {
    					pict.setNewPictureSize(ZOOM_OUT);
    					pict.validate();
    					pict.repaint();
    				}
    			});
    		}
    	}
    }

  3. #3
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    Try creating a program that zooms and pans with only the use of a mouse.

    Here are some things to note in the code you posted:

    After the first time, you are scaling an already scaled image when you set a new picture size. Over time the image quality will deteriorate. And for slightly more complicated reasons, there's the possibility of an OutOfMemoryError.

    JPanel has FlowLayout by default, so the BorderLayout constraints when adding "+" and "-" are effectively ignored.

    The validate() calls on the picture component are unnecessary since the component is not a Container and doesn't contain any children.

    The Image returned by #getScaledInstance is a Toolkit image and is not guaranteed to be ready for drawing the first time you try to paint it. So you need to include a "this" as the ImageObserver parameter.
    Java Code:
    g.drawImage(picture,0,0,this);

    Lastly, extending Component doesn't seem like a good idea. This introduces the possibility of flashing when painting, and you'll run into all the problems of trying to mix heavyweight components with lightweights should your program get anymore complicated.

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    After the first time, you are scaling an already scaled image when you set a new picture size. Over time the image quality will deteriorate. And for slightly more complicated reasons, there's the possibility of an OutOfMemoryError.
    Lastly, extending Component doesn't seem like a good idea. This introduces the possibility of flashing when painting, and you'll run into all the problems of trying to mix heavyweight components with lightweights should your program get anymore complicated.
    Thanks for your good suggestions.
    Please give me some advices how to solve that problems and/or give me some useful links with examples so that I could see how it should be done.

Similar Threads

  1. change image
    By mysa in forum Java Applets
    Replies: 7
    Last Post: 09-01-2010, 03:51 PM
  2. Change colors, image on WizardDialog
    By laurie in forum SWT / JFace
    Replies: 0
    Last Post: 06-16-2010, 04:30 PM
  3. Javascript change image
    By butsags in forum New To Java
    Replies: 1
    Last Post: 12-10-2009, 11:08 PM
  4. how to change windows xp background image through java
    By erosenthil in forum Advanced Java
    Replies: 1
    Last Post: 08-18-2009, 12:57 PM
  5. Replies: 5
    Last Post: 05-18-2009, 12:42 PM

Posting Permissions

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