ImageIcon Link Error

I want to pass an instance of ImageIcon by value, since we know that by default it is a link to a link by value. Is there any way to solve this problem?

ImageIcon img1 = new ImageIcon(); ImageIcon img2 = new ImageIcon(); img1 = img2; // both share the same reference....... 

I tried using ImageIcon getImage() and setImage() .

But in fact, my program includes writing the image to a file and the image quality seems to be changing.

In short, there is a way in which when using the function

 img1 = func(img2); // both do not share the same refernce. But the value is copied 

At least how can I maintain the exact image quality when using getImage() or setImage() ?

Additional information on request.

The one I use is a jpg image, and I included the function in the same way as image = img1.getimage ();
img2.setImage (image);

I supported a separate java class, where it contained the default image ImageIcon containg jpg, and I extract the image from it using some of the class methods, for example

 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. 
+4
source share
1 answer

If you want to copy ImageIcon, you can try: img2 = new ImageIcon(img1.getImage()); I would make sure to read and write .png (lossless) images, not .jpg (lossy) images.

But this should not affect the quality of the image, and I doubt that you will solve your problem. I am afraid that your problem lies elsewhere in code that is not shown, that you can degrade the image quality by resizing the image, or save it and extract it using lossy methods such as .jpg files, or by reducing the number of bytes per pixel ..

Otherwise, if you still have problems, consider learning more about the problems, because again I am afraid that the problem is due to loss of image quality than when exchanging links.

+7
source

All Articles