Skip to main content

Better Scaling in JXImageview

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
2 replies [Last post]
Leander_
Offline
Joined: 2012-06-30

Hi,
after a long search i couldn`t find any solution how I can improve the Quality of the Scaling in JXImageview. My Problem is that i have relatively big Pictures, which look rather "pixling" when you scale them down. Is there any solution, sth. i am missing?

Leander

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08

First, you need to file a bug because JXImageView is using the terrible, built-in scaling that comes with the drawImage method. We should be using something from GraphicsUtilities instead.

Second, to workaround the problem in your local, subclass JXImageView and override paintComponent to paint the image using a better scaling mechanism.

Karl

watcher
Offline
Joined: 2010-09-15

Here's my solution.
Look at GraphicsUtilities for more options

 @Override
    protected void paintComponent(Graphics g) {
//         ((Graphics2D)g).setPaint();
        //g.setColor(getBackground());
        g.fillRect(0,0,getWidth(),getHeight());
        if(getImage() != null) {
            Point2D center = new Point2D.Double(getWidth()/2,getHeight()/2);
            if(getImageLocation() != null) {
                center = getImageLocation();
            }
            Point2D loc = new Point2D.Double();
            double width = getImage().getWidth(null)*getScale();
            double height = getImage().getHeight(null)*getScale();
            loc.setLocation(center.getX()-width/2, center.getY()-height/2);
            BufferedImage image =          GraphicsUtilities.createThumbnailFast((BufferedImage)getImage(),(int) width, (int)height);
           
            g.drawImage(image, (int)loc.getX(), (int)loc.getY(),
                    (int)width,(int)height,
                    null);
          
        }
    }