java在运行时调整图像图标(或图像)的大小
我尝试在运行时使用线程定期增加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);
有人能帮我吗?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);
}
}
# 1 楼答案
由于您已经在加载映像,只需在} 以定义初始大小,并选择合适的布局来控制大小调整行为。使用^{cd4>}来控制插值类型,如图here所示。还考虑^ {< CD5> },检查here和here。
paintComponent()
的实现中draw()
它即可。重写^{