使用getImage()和getScaledInstance()调整java.awt.Image的异常大小

时间:2012-07-05 09:54:11

标签: java image swing rmi imageicon

我正在制作一个java rmi based application我在那里传递并从服务器接收一个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;

现在我遇到的问题很奇怪...... 当我点击此按钮时,我想要重新绘制图像......

But when i download the recently uploaded image next time on Button press A....it is displayed either magnified or reduced to size even though i included the getScaledInstance method.... 像这样...

When i click the image. It embedds correctly enter image description here

我正在处理的图像是jpg图像.... 我甚至检查了服务器目录中的图像.....在从客户端上传到服务器的文件上没有发生大小更改。但是当它被下载并嵌入到jlabel中时会发生变化...... 任何人都可以帮我解决这个问题...... ??

1 个答案:

答案 0 :(得分:1)

不知何故,它缩小了两倍。要么缩小它,然后将其发送到缩小它的服务器,要么两个按钮点击它。消除服务器端的缩小代码,看看会发生什么。