java 如何拉伸图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4733163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to stretch image
提问by user542719
i want to strech image by using Graphics but unable here is my code it shows image in size that i want but not strach the image
我想通过使用 Graphics 来拉伸图像,但这里无法显示我的代码,它以我想要的大小显示图像但不拉伸图像
void imageload () {
FileDialog fd = new FileDialog(MainFram.this,"Open", FileDialog.LOAD);
fd.show();
if(fd.getFile() == null){
//Label1.setText("You have not chosen any image files yet");
}else{
String d = (fd.getDirectory() + fd.getFile());
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image1 = toolkit.getImage(d);
saveImage = d;//if user want to save Image
ImageIcon icon=new ImageIcon(Image1);
lblImage.setIcon(icon);
lblImage.setMinimumSize(new Dimension(50, 70));
lblImage.repaint();
}
}
回答by dogbane
Call getScaledInstance()
to scale the image to the size you want before you create the ImageIcon
. You don't need to call setMinimumSize
on the label.
调用getScaledInstance()
创建之前缩放你想要的图像尺寸ImageIcon
。你不需要调用setMinimumSize
标签。
Image image = toolkit.getImage("pic.jpg");
Image scaledImage = image.getScaledInstance(50, 70, Image.SCALE_DEFAULT);
ImageIcon icon=new ImageIcon(scaledImage);
回答by Ranjan Singh rinku
TO set background image from filchooser
从 filchooser 设置背景图像
final JFileChooser fc = new JFileChooser();
int r = fc.showOpenDialog(this);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (r == JFileChooser.APPROVE_OPTION) {
String name = fc.getSelectedFile().getAbsolutePath();
JOptionPane.showMessageDialog(null,"ADDED successfully");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage(name);
Image scaledImage = image.getScaledInstance(1366, 768, Image.SCALE_DEFAULT);
ImageIcon icon=new ImageIcon(scaledImage);
my.jLabel10.setIcon(icon);}