Solved

scale an image down!! (or up)

Posted on 2004-03-31
8
1,086 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

896 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

13 Experts available now in Live!

Get 1:1 Help Now