0

我正在尝试在运行时使用线程定期增加 imageIcon 的高度和宽度。

我试过这个但没有成功

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);

有人能帮我吗?谢谢

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);
    }
}
4

1 回答 1

2

由于您已经在加载图像,只需draw()paintComponent(). 覆盖get\[Preferred|Maximum|Minimum\]Size()以定义初始大小并选择合适的布局来控制调整大小行为。用于AffineTransformOp控制插值类型,如下所示。还要考虑getScaledInstance()在这里这里检查。

于 2013-10-03T19:13:27.380 回答