How to setSize of image using RescaleOp
Asked Answered
D

1

3

I am writing a test app. To set Alpha for image I use paintComponent method. Watch next snippet...

    public class TestImage extends JLabel{

        public void paintComponent( Graphics g ) {

                    super.paintComponent( g );

                    Graphics2D g2d=(Graphics2D)g;
                    g2d.drawImage(this.bImage, rop, 0, 0);


            }

    public void setRescaleOp(RescaleOp rop){this.rop=rop;}
}

As you can see,

g2d.drawImage(this.bImage, rop, 0, 0);

does not allow to set width and height as if I use g.drawImage(bImage, 0, 0,width,height, null);

So the question is... How to set width and height for bImage in this case?

Any useful comment is appreciated

Andrew

Disillusionize answered 3/5, 2011 at 2:32 Comment(0)
O
8

First filter(), as shown here, and then scale using drawImage() or AffineTransformOp, as shown here.

Addendum: Alternatively, you can scale the image first (using either approach above) and then use your RescaleOp in drawImage().

As an aside, RescaleOp scales the image's color bands; it does not change the image's dimensions. To avoid confusion, dimensional scaling is sometimes called resampling.

Addendum: Here's an example of using drawImage() to resample and RescaleOp to adjust the alpha of an image.

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @see https://stackoverflow.com/questions/5838842
 * @see https://stackoverflow.com/questions/5864490
 */
public class AlphaTest {

    private static void display() {
        JFrame f = new JFrame("AlphaTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ImageIcon icon = new ImageIcon("image.jpg");
        final AlphaPanel ip = new AlphaPanel(icon, 0.75);
        final JSlider slider = new JSlider();
        slider.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                int v = slider.getValue();
                ip.setAlpha((float) v / slider.getMaximum());
                ip.repaint();
            }
        });
        f.add(ip, BorderLayout.CENTER);
        f.add(slider, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                display();
            }
        });
    }
}

class AlphaPanel extends JPanel {

    private BufferedImage bi;
    private float[] scales = {1f, 1f, 1f, 0.5f};
    private float[] offsets = new float[4];
    private RescaleOp rop;

    public AlphaPanel(ImageIcon icon, double scale) {

        int width = (int) (scale * icon.getIconWidth());
        int height = (int) (scale * icon.getIconHeight());
        this.setPreferredSize(new Dimension(width, height));
        this.bi = new BufferedImage(
            width, height, BufferedImage.TYPE_INT_ARGB);
        this.bi.createGraphics().drawImage(
            icon.getImage(), 0, 0, width, height, null);
        rop = new RescaleOp(scales, offsets, null);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(bi, rop, 0, 0);
    }

    public void setAlpha(float alpha) {
        this.scales[3] = alpha;
        this.rop = new RescaleOp(scales, offsets, null);
    }
}
Only answered 3/5, 2011 at 2:35 Comment(11)
I have tried using filter method but this doesn't help to filter image next and back as the code I mentioned in my question g2d.drawImage(this.bImage, rop, 0, 0);Disillusionize
The thing is I want to set alpha as from 1 to 0 for the same image with dragging a JSlider or clicking JButton... So if I use filter it seems to return an inited BufferedImage what make irreversable changing alpha on fly next and back. That's why I want to see some alt decision :)Disillusionize
The code example download.oracle.com/javase/tutorial/2d/images/drawimage.html shows how to set alpha next to 0 and back to 1 but I don't get it how can I set image width and height in this case?Disillusionize
Can actually RescaleOp class help in this case? Its identificator look like "scale" word so maybe it can control image scale as w/h?Disillusionize
No, it's scaling the color model, not the dimensions.Only
@Only I was trying to replace g2d.drawImage(this.bImage, rop, 0, 0); with the filter() invokation but this can help if I want to change alpha just once. If I use it twice for the same image I get inpredictable result , moreover, image disappears at all... But I am interested to change alpha coeff dynamically for ONE IMAGE and that is the major problem I guess. I am looking for some decision which can help me not to loose the alpha dynamic effect and resizing at the same time.Disillusionize
Quote: "Addendum: Alternatively, you can scale the image first (using either approach above) and then use your RescaleOp in drawImage()."Disillusionize
VALUE_INTERPOLATION_BICUBIC is better.Only
Emm... What class VALUE_INTERPOLATION_BICUBIC for? I don't remember the arg :(Disillusionize
It's one of the RenderingHints.Only
I've added an example with which to experiment.Only

© 2022 - 2024 — McMap. All rights reserved.