Solved

scale an image down!! (or up)

Posted on 2004-03-31
8
1,102 Views
Last Modified: 2013-11-22
I'm using a JLabel to display an image.  I am attempting to use the getScaledInstance method of Image but to no avail.  The images return as their original size.  Ultimately, what I need to do is get the height and width of the image, determine how much bigger (or smaller) it is than the applet display area, and resize it accordingly.  

Here's my code:

    public void init()
    {
        btnNext = new JButton(">>");
        btnNext.addActionListener(this);
        btnNext.setActionCommand("NEXT");

        btnPrev = new JButton("<<");
        btnPrev.addActionListener(this);
        btnPrev.setActionCommand("PREV");

        pnlSouth = new JPanel();
        pnlSouth.setLayout(new GridLayout(1,2));
        pnlSouth.add(btnPrev);
        pnlSouth.add(btnNext);

        image = Toolkit.getDefaultToolkit().getImage("fighters_a10_0008.jpg");
        image.getScaledInstance(100,100 ,Image.SCALE_FAST);
        icon = new ImageIcon(image);

        lblPic = new JLabel();
        lblPic.setIcon(icon);

        pnlCenter = new JPanel();
        pnlCenter.add(lblPic);

        setLayout(new BorderLayout());
        add (pnlCenter, BorderLayout.CENTER);
        add (pnlSouth, BorderLayout.SOUTH);
    }

It all works except for the getScaledInstance method.  What pray-tell am I doing wrong?

TIA
0
Comment
Question by:dds110
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10724383
DO it like this instead

ImageIcon icon = new ImageIcon("fighters_a10_0008.jpg");
Image image = icon.getImage();
Image scaledImage = image.getScaledInstance(100,100 ,Image.SCALE_FAST);
0
 
LVL 8

Author Comment

by:dds110
ID: 10724485
Doesn't the ImageIcon have to be loaded with the scaledImage?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10724514
Well i wasn't sure where the scaled bit came in - trying to get you out of a potential synch loading gotcha, but try it like this first:

image = image.getScaledInstance(100,100 ,Image.SCALE_FAST);
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 10724541
...but if you still find problems then (using first idea):

ImageIcon icon = new ImageIcon("fighters_a10_0008.jpg");
Image image = icon.getImage();
Image scaledImage = image.getScaledInstance(100,100 ,Image.SCALE_FAST);
icon.setImage(scaledImage);
0
 
LVL 8

Author Comment

by:dds110
ID: 10724552
Worked like a charm.

Thanks
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10724571
This is what I do:

private void updatePicture(ImageIcon imageIcon) {
        if (imageIcon==null) return;
        Image img = imageIcon.getImage();
        Dimension d = getScaledSize(imageIcon);
        JLabel imageLbl = new JLabel( new ImageIcon(
            img.getScaledInstance(d.width, d.height, Image.SCALE_SMOOTH) ) );
        imageLbl.setBounds(0, 0, d.width, d.height);
        imagePanel.removeAll();
        imagePanel.add(imageLbl);
        imagePanel.revalidate();
        imagePanel.repaint();
}

private Dimension getScaledSize(ImageIcon image) {
        double imageRatio =
            (double)image.getIconWidth() / (double)image.getIconHeight();
       
        if ( imageRatio > 1.0)
            return new Dimension( MAX_IMG_WIDTH,
                                  (int)(((double)MAX_IMG_HEIGHT)/imageRatio) );
        else
            return new Dimension( (int)(((double)MAX_IMG_WIDTH)*imageRatio),
                                  MAX_IMG_HEIGHT );
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10724587
8-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10724623
I was too late.
Don't forget you probably want to scale proportionally.
cf. My code
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 21
What's wrong with this code? 4 28
Slow Restore if incremental backups using RDiff.exe 4 31
collection output issue 9 39
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question