[英]how to make image resizeable
i try to make imageviewer, the code is below 我尝试使imageviewer,下面的代码
import javax.swing.*;
import java.awt.event.*;
import java.IO.*;
public class javaImageViewer extends JFrame{
public javaImageViewer(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(200,100);
JButton openButton = new JButton("Open Images");
getContentPane().add(openButton);
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser(".");
int status = chooser.showOpenDialog(javaImageViewer.this);
if(status == JFileChooser.APPROVE_OPTION){
try{
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(chooser.getSelectedFile().toURL()));
frame.add(label);
frame.setSize(500,500);
frame.setVisible(true);
}
catch(Exception e2){
System.out.println("Error"+e2);
}
}
}
});
}
public static void main(String [] args){
javaImageViewer tim = new javaImageViewer();
tim.setVisible(true);
}
}
but when i open image from camera, it always showing over the frame size i dont know how to make the image follow my frame size ? 但是,当我打开相机中的图像时,它始终显示在帧大小上,我不知道如何使图像跟随我的帧大小?
in order to place your image with the Full Size, you can try to make your own JPanel, override the paintComponent method, and inside this method use g.DrawImage , 为了以全尺寸放置图像,您可以尝试制作自己的JPanel,覆盖paintComponent方法,并在此方法中使用g.DrawImage,
other solution and maybe easier is set the JPanel dimesion with the same dimesion of you Image and Add this JPanel to a JScrollPane , in this way is going to show a scrollbars to navigate 其他解决方案,也许更容易的方法是,将JPanel维度设置为与您的Image相同的维度,然后将此JPanel添加到JScrollPane,这样将显示滚动条以进行导航
depends of reall size in pixels 取决于实际大小(以像素为单位)
1) put Image / BufferedImage
as Icon/ImageIcon to the JLabel , then image will be resiziable up to real size in pixels 1)将Image / BufferedImage
作为Icon / ImageIcon放置到JLabel上 ,然后图像将可调整为最大实际大小(以像素为单位)
2) resize Image by usage of Image#getScaledInstance(int width, int height, int hints) 2)通过使用Image#getScaledInstance(int width,int height,int hints)调整图像大小
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.