Results 1 to 4 of 4
  1. #1
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default The problem with scaling images

    Hi!

    I'm trying to create a method for zooming in/out images (ImageIcon in JLabel). The problem is with low image quality after zooming in/out an image multiple times Please, help me to find my error. I've tried different methods, but all of them failed.

    Thanks!

    PHP Code:
    private void doZoom(int scaleFactor) {
              Component[] c = MainClass.getSelectablePanel().getComponents();
              for(int i = 0; i < c.length; i++) {
                    if(c[i] instanceof JLabel) {                   
                        JLabel selectedLabel = (JLabel)c[i];
                        ImageIcon newIcon = (ImageIcon) selectedLabel.getIcon();
                        selectedLabel.setIcon(scale(newIcon.getImage(), 0.75));
                    }
             }
             repaint();
        }
    
    
        private ImageIcon scale(Image src, double scale) {
            int w = (int)(scale*src.getWidth(this));
            int h = (int)(scale*src.getHeight(this));
            int type = BufferedImage.TYPE_INT_RGB;
            BufferedImage dst = new BufferedImage(w, h, type);
            Graphics2D g2 = dst.createGraphics();
            g2.drawImage(src, 0, 0, w, h, this);
            g2.dispose();
            return new ImageIcon(dst);
        }
    Last edited by LianaN; 01-18-2011 at 11:10 AM.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Try keeping the Image and current scaling applied separate from the Display. The problem is that you are scaling an image smaller which has the affect of reducing some of the information about the Image then you attempt to use this new "corrupted" image when applying the new scaling.

    Maybe try extending the JLabel class so that it holds the Image to display and the current scaling applied to it. Then when you want to "scale" the image, just pass the scaling to be applied and have it recreate from the original.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    See Drawing an Image (The Java™ Tutorials > 2D Graphics > Working with Images) for info on how to dynamically scale an image without effecting the original image in the model

  4. #4
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Thanks! I have almost solved the problem. The only question that remains unsolved is: why the JLabel background becomes black after scaling? I would appreciate it if someone could help me with it.

    PHP Code:
        
    private void doZoom(int scaleFactor) {
              Component[] c = MainClass.getSelectablePanel().getComponents();
              for(int i = 0; i < c.length; i++) {
                    if(c[i] instanceof MyLabel) {
                        MyLabel selectedLabel = (MyLabel)c[i];
                        BufferedImage source = (BufferedImage) selectedLabel.getOriginalImage();
                        int w = selectedLabel.getWidth() + scaleFactor;
                        int h = selectedLabel.getHeight() + scaleFactor;
                        BufferedImage result = copyImage(source, w, h);
                        selectedLabel.setSize(new Dimension(w, h));
                        selectedLabel.setIcon(scale(result, w, h));
                    }
             }
             repaint();
    }
    
    public static BufferedImage copyImage(BufferedImage image, int width, int height) {
            int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
            BufferedImage copiedImage = new BufferedImage(width, height, type);
            Graphics2D g = copiedImage.createGraphics();
            g.setComposite(AlphaComposite.Src);
    
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    
            g.setRenderingHint(RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY);
    
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
    
            g.drawImage(image, 0, 0, width, height, null);
            g.dispose();
            return copiedImage;
    }
    
    private ImageIcon scale(Image src, int w, int h) {
            int type = BufferedImage.TYPE_INT_RGB;
            BufferedImage dst = new BufferedImage(w, h, type);
            Graphics2D g2 = dst.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(src, 0, 0, w, h, this);
            g2.dispose();
            return new ImageIcon(dst);
    }
    
    
    public class MyLabel extends JLabel {
        private ImageIcon imageicon;
        private Image img;
    
        public MyLabel(ImageIcon ii) {
            this.imageicon = ii;
            this.setIcon(ii);
        }
    
        public ImageIcon getOriginalIcon() {
            return imageicon;
        }
    
        public void setImage(Image img) {
            this.img = img;
        }
    
        public Image getOriginalImage() {
            return img;
        }
    
      }
    Last edited by LianaN; 01-18-2011 at 08:54 PM.

Similar Threads

  1. Scaling an object in Greenfoot
    By Tayl0r in forum Java Gaming
    Replies: 0
    Last Post: 12-03-2010, 10:05 PM
  2. Bilinear Interpolation Scaling help
    By Xpectro in forum Java 2D
    Replies: 1
    Last Post: 06-15-2010, 07:12 PM
  3. scaling image
    By OmerHalit in forum Advanced Java
    Replies: 3
    Last Post: 05-07-2010, 01:30 AM
  4. Scaling a JPEG
    By ScottVal in forum Advanced Java
    Replies: 5
    Last Post: 03-21-2009, 08:47 PM
  5. scaling images
    By willemjav in forum Java Applets
    Replies: 7
    Last Post: 06-19-2008, 09:54 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
  •