Solved

scale an image down!! (or up)

Posted on 2004-03-31
8
1,114 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

751 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