Java 输出到屏幕时图像的位置不正确
让我问你一个问题。我有一个文件夹,里面有不同大小的图片,从中我随机读取一张图片,缩小它的大小,然后在jpanel上使用JLabel进行推断-这很简单。事实上,一张图片的提取可以显示在正确的坐标上,而另一张图片可以在仪表上反弹。为什么会发生这种情况?随机图片忽略其他容器的立根和边界Java 输出到屏幕时图像的位置不正确,java,swing,graphics,Java,Swing,Graphics,让我问你一个问题。我有一个文件夹,里面有不同大小的图片,从中我随机读取一张图片,缩小它的大小,然后在jpanel上使用JLabel进行推断-这很简单。事实上,一张图片的提取可以显示在正确的坐标上,而另一张图片可以在仪表上反弹。为什么会发生这种情况?随机图片忽略其他容器的立根和边界 private void initialize() throws IOException { frame = new JFrame(); frame.setBounds(100, 100, 11
private void initialize() throws IOException {
frame = new JFrame();
frame.setBounds(100, 100, 1150, 664);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
panel = new JPanel ();
panel.setBounds(10, 10, 1150, 664);
frame.getContentPane().add(panel);
File f = new File ("C:/Users/user/Desktop/Ramms");
String[] names = f.list();
all_Images= new ImageIcon[names.length];
for(int i=0; i<names.length; i++){
all_Images[i]= new ImageIcon ("C:/Users/user/Desktop/Ramms/"+names[i]);
}
selected_Image = all_Images[random(0,all_Images.length)];
w= selected_Image.getIconWidth()/scl;
h= selected_Image.getIconHeight()/scl;
Image img = selected_Image.getImage();
bic = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bic.createGraphics();
g.drawImage(img, 0, 0, w, h, null);
newIcon = new ImageIcon(bic);
panel.setLayout(null);
JLabel label1 = new JLabel(newIcon);
panel.add(label1).setBounds(0,100,w,h);
}
private void initialize()引发IOException{
frame=新的JFrame();
机架立根(1001001150664);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
panel=newjpanel();
嵌板.立根(10,10,1150664);
frame.getContentPane().add(面板);
文件f=新文件(“C:/Users/user/Desktop/Ramms”);
字符串[]名称=f.list();
所有图片=新图片图标[names.length];
对于(int i=0;i
缩小它的尺寸
在我看来,你并不是在缩小它的尺寸:
bic = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bic.createGraphics();
g.drawImage(img, 0, 0, w, h, null);
newIcon = new ImageIcon(bic);
图标的大小是创建的缓冲区图像的大小
它不知道也不关心你在BuffereImage上画了什么
有两种解决方案:
创建buffereImage
时,需要以其缩放大小创建图像
使用Image.getScaleInstance(…)
方法调整图像大小
缩小它的尺寸
在我看来,你并不是在缩小它的尺寸:
bic = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bic.createGraphics();
g.drawImage(img, 0, 0, w, h, null);
newIcon = new ImageIcon(bic);
图标的大小是创建的缓冲区图像的大小
它不知道也不关心你在BuffereImage上画了什么
有两种解决方案:
创建buffereImage
时,需要以其缩放大小创建图像
使用Image.getScaleInstance(…)
方法调整图像大小
1) 为了更快地获得更好的帮助,请发布一个或。2)获取图像的一种方法例如,热链接到中看到的图像。不要使用空布局。Swing设计用于布局管理器。1)为了更快地获得更好的帮助,请发布一个或。2)获取图像的一种方法例如,热链接到中看到的图像。不要使用空布局。Swing设计用于布局管理器。