我正在制作一个基于 java rmi 的应用程序
,我在其中传递和接收来自服务器的 ImageIcon 对象...
(图像存储在服务器中的单独 URL 中)
函数涉及以下....
1. Getting the image from the server at first....(on button press A)
2. Replacing it with a image file in the client[optional]....(on button press B)
3. Remove the image with a default image[optional]....(on button press C)
4. Sending it back to the Server....................(On button press D).....
这里图片显示在一个jlabel
调用img_label
我使用的代码如下.....
使用的变量
java.awt.Image img;
javax.swing.ImageIcon CurrentImageIcon;
javax.swing.ImageIcon DefaultImageIcon;
// CurrentImageIcon contains the image to be displayed in the img_label....
// img is used for copying as well for scaling......
// DefaultImageIcon holds the default Image......
在按钮上按 A
img = temp.getImage();
CurrentImageIcon = new ImageIcon(img);
// Assuming temp holds the ImageIcon taken from the server.......
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
在按钮上按 B
String url_text = jTextField.getText(); // taking the url frm the field.....
CurrentImageIcon = new ImageIcon(url_text);
img=CurrentImageIcon.getImage();
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
在按钮上按 C
img = DefaultImageIcon.getImage();
CurrentImageIcon = new ImageIcon(img);
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
在按钮上按 D
// ImagetoSend is an ImageIcon to be sent to the Server.....
ImagetoSend = CurrentImageIcon;
CurrentImageIcon = null;
现在我遇到的问题很奇怪...... 当我点击这个按钮时,图像正在按照我想要的方式嵌入......
但是,当我下次在按钮上按 A 下载最近上传的图像时......即使我包含了 getScaledInstance 方法,它也会放大或缩小显示......
像这样……
我正在处理的图像是一个 jpg 图像.... 我什至检查了服务器目录中的图像.....从客户端上传到服务器的文件没有发生大小变化。但是当它被下载并嵌入到 jlabel 时会观察到变化...... 谁能帮我解决这个问题...?
最佳答案
不知何故,它被缩小了两倍。您要么缩小它,然后将它发送到缩小它的服务器,要么点击两个按钮来缩放它。消除服务器端的缩减代码,看看会发生什么。
关于java - 使用 getImage() 和 getScaledInstance() 异常调整 java.awt.Image 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341893/