Solved

scale an image down!! (or up)

Posted on 2004-03-31
8
1,119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

635 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