I want to pass an ImageIcon instance by value as we know that by default it is pass reference by value. Is there any way to sort out this issue?
ImageIcon img1 = new ImageIcon();
ImageIcon img2 = new ImageIcon();
img1 = img2; // both share the same reference.......
I tried using ImageIcon’s getImage() and setImage().
But actually my program includes writing image to file and the quality of the image seems to change.
In short is there a way by which on using a function
img1 = func(img2); // both do not share the same refernce. But the value is copied
At least how can I retain the exact image quality on using getImage() or setImage()?
Some additional information as per request.
The one I’m using is a jpg image, and I’ve included the function exactly as
image = img1.getimage();
img2.setImage(image);
I’ve maintained a separate java class where it holds the default ImageIcon containg jpg image and I’m retrieving the image from it using some of the class methods like
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.
If you want to copy an ImageIcon you could try:
img2 = new ImageIcon(img1.getImage());I would make sure that I read in and wrote out .png (lossless) images, not .jpg (lossy) images.But this should have no bearing on image quality, and I doubt will solve your problem. I fear your problem lies elsewhere in code not shown, that you may be degrading your image quality by resizing the image or by storing and retrieving it via lossy methods such as with .jpg files, or by reducing the number of bytes per pixel…
Otherwise if you’re still having problems, consider telling us more about the problems because again I fear that the problem is more with the loss of image quality than in the sharing of references.