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

在Java RMI 应用中,作者遇到一个问题:从服务器获取的ImageIcon在调整大小后显示异常。在按钮操作A、B、C、D过程中,图片在JLabel中被缩放和替换。尽管使用了`getScaledInstance()`方法,但下次下载图片时,图片会显示为放大或缩小。原始图片在服务器上未发生尺寸变化,问题可能出在图片在客户端的显示上。
摘要由CSDN通过智能技术生成

我做了一个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).....此处图像显示在一个名为img_label的jlabel中

我使用的代码如下.....

使用的变量

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....

喜欢这个...

4LMKG.png

ffe51619a705e0b56d9b1e9c3dc356cb.png

我正在处理的图像是一个jpg图像....

我甚至在服务器目录中检查了镜像.....从客户端上传到服务器的文件没有发生大小变化。但是当它被下载并嵌入到jlabel中时,就会观察到这种变化...

任何人都可以帮我解决这个问题......?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyFrame extends JFrame implements ComponentListener { private JLabel background; private JTextField textField; private JPasswordField passwordField; public MyFrame() { super("My Frame"); setSize(600, 400); setLocationRelativeTo(null); // 设置背景图片 ImageIcon imageIcon = new ImageIcon("src/image/login.jpg"); Image image = imageIcon.getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH); background = new JLabel(new ImageIcon(image)); setContentPane(background); // 添加文本框和密码框 textField = new JTextField(20); passwordField = new JPasswordField(20); JPanel panel = new JPanel(); panel.add(textField); panel.add(passwordField); panel.setOpaque(false); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); add(panel, BorderLayout.CENTER); addComponentListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } // 窗口大小改变时,重新设置背景图片大小 public void componentResized(ComponentEvent e) { ImageIcon imageIcon = new ImageIcon("src/image/login.jpg"); Image image = imageIcon.getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH); background.setIcon(new ImageIcon(image)); } public void componentMoved(ComponentEvent e) {} public void componentShown(ComponentEvent e) {} public void componentHidden(ComponentEvent e) {} public static void main(String[] args) { new MyFrame(); } }这段代码为什么看不到背景图,解释一下
05-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值