Arj*_*K P 4 java swing pass-by-reference pass-by-value imageicon
我希望按值传递ImageIcon实例,因为我们知道默认情况下它是按值传递引用.有没有办法解决这个问题?
ImageIcon img1 = new ImageIcon();
ImageIcon img2 = new ImageIcon();
img1 = img2; // both share the same reference.......
Run Code Online (Sandbox Code Playgroud)
我尝试使用ImageIcon getImage()和setImage().
但实际上我的程序包括将图像写入文件,图像的质量似乎发生了变化.
简而言之,有一种使用函数的方法
img1 = func(img2); // both do not share the same refernce. But the value is copied
Run Code Online (Sandbox Code Playgroud)
至少我怎么能留住上使用完全相同的图像质量getImage()还是setImage()?
根据要求提供一些其他信息.
我正在使用的是jpg图像,我将函数完全包含在image = img1.getimage();
img2.setImage(图像);
我维护了一个单独的java类,它保存了包含jpg图像的默认ImageIcon,我正在使用一些类方法从中检索图像
ImageIcon defaulticon = new ImageIcon("d:\\default.jpg");
public void getdefaultimage(ImageIcon img)
{
img.setImage(defaulticon.getImage());
}
// The class which I maintained specifically for this purpose is a pure java class.
Run Code Online (Sandbox Code Playgroud)
如果你想复制一个ImageIcon你可以尝试:img2 = new ImageIcon(img1.getImage());我会确保我读入并写出.png(无损)图像,而不是.jpg(有损)图像.
但这应该与图像质量无关,我怀疑会解决你的问题.我担心你的问题出现在未显示的代码的其他地方,你可能会通过调整图像大小或通过有损方法(如.jpg文件)或通过减少每个像素的字节数来存储和检索它来降低图像质量. .
否则,如果你仍然遇到问题,请考虑告诉我们更多关于这些问题的信息,因为我再次担心问题更多的是图像质量的损失,而不是参考共享.
| 归档时间: |
|
| 查看次数: |
340 次 |
| 最近记录: |