Solved

scale an image down!! (or up)

Posted on 2004-03-31
8
1,079 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now