user2843947
user2843947

Reputation: 1

Resize ImageIcon (or Image) at run time

I'm trying to increase the height and width of a imageIcon periodically at run time with a thread.

I tried with this but did not succeed

Image img = image.getImage();
BufferedImage bi = new BufferedImage(
            img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(img, 0, 0, 100, 100, null);
ImageIcon image = new ImageIcon(bi);

can someone help me? Thx

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ui {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        //set imageicon
        final ImageIcon image = new ImageIcon(
                "C:\\Users\\User\\workspace\\Project\\images\\c.png");
        JLabel imagelabel = new JLabel(image, JLabel.CENTER);
        frame.add(imagelabel);

        ScheduledExecutorService exec = Executors
                        .newSingleThreadScheduledExecutor();
        exec.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() { 
                // ---->How to increase height and width dynamically at runtime?    
            }
        }, 0, 500, TimeUnit.MILLISECONDS);
    }
}

Upvotes: 0

Views: 503

Answers (1)

trashgod
trashgod

Reputation: 205775

As you are already loading the image, simply draw() it in your implementation of paintComponent(). Override get\[Preferred|Maximum|Minimum\]Size() to define the initial size and choose a suitable layout to control resize behavior. Use AffineTransformOp to control the interpolation type, as shown here. Also consider getScaledInstance(), examined here and here.

Upvotes: 2

Related Questions