4

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.
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Arjun K P
  • 2,081
  • 4
  • 20
  • 33
  • 2
    [Java doesn't support pass by reference](http://javadude.com/articles/passbyvalue.htm) – gobernador Jun 30 '12 at 18:20
  • nor does it need to. I think that the "pass by reference/ value" issue is a non-issue here, and the real issue is one of image quality. – Hovercraft Full Of Eels Jun 30 '12 at 18:23
  • @HovercraftFullOfEels read the latest post... – Arjun K P Jun 30 '12 at 18:26
  • @akp: what latest post? The image quality is ***not*** degraded by passing by reference. It is only degraded by how it is read in or written out from a source, or what type of Image holds the image, or whether you try to change the size of the image or the other characteristics of the image -- things you're not telling us. – Hovercraft Full Of Eels Jun 30 '12 at 18:27
  • Consider creating and posting an [sscce](http://sscce.org) that demonstrates your problem because so far I think we'll have little luck being able to help you given the information that has been posted. – Hovercraft Full Of Eels Jun 30 '12 at 18:33
  • @HovercraftFullOfEels....hope the recent post is enough for you to understand..... – Arjun K P Jun 30 '12 at 18:44
  • Again, consider creating and posting an [sscce](http://sscce.org). Please have a look at the link as it will explain how this can help us help you as well as the requirements for this construct. – Hovercraft Full Of Eels Jul 01 '12 at 03:51
  • @HovercraftFullOfEels....ok buddy....i agreed i m unsuccessful in providing apt example...... but `atleast tell me if i use the getImage() method for an ImageIcon object constructed with a jpg file. Will it comparatively degrade the quality of the image returned from the function ?...since i want to use this function a number of times.....i.e setting image to an imageicon say A and then getting it from A and setting that image to imageicon B and so on....` – Arjun K P Jul 01 '12 at 08:02
  • @akp: I hate to say this, and I'm sorry for saying this, but *I don't know*. One thing you possibly could do is to read the image in as a BufferedImage using `ImageIO.read(...)` As far as I know, this *shouldn't* degrade the image quality too much, but all .jpg files risk *some* degradation since their stored in a lossy format. I wish I could help you more! – Hovercraft Full Of Eels Jul 01 '12 at 13:36
  • Any way to pastebin your project and resources? – Hovercraft Full Of Eels Jul 01 '12 at 13:43
  • @HovercraftFullOfEels......i m afraid i can't buddy....it's quite large and i think complicated by which i'll receiving 8 downvotes instead of upvotes...when u asked me to post the complete details......i was actually wondering what to post....lolz... – Arjun K P Jul 01 '12 at 18:31
  • @HovercraftFullOfEels : +1 for helping me greatly to your maximum..... – Arjun K P Jul 01 '12 at 18:32

1 Answers1

7

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • The main problem is the size of the image and yeah....of course...the clarity........... – Arjun K P Jun 30 '12 at 18:09
  • 3
    @akp: you'll need to give us more details then since none of the code and information you've given would tell us why this is a problem. – Hovercraft Full Of Eels Jun 30 '12 at 18:10
  • my main issue is to pass an ImageIcon object by value.......avoiding the reference problem......i tried the method as u posted in ur answer.......i've bit of a problem regaring image size and quality...... – Arjun K P Jun 30 '12 at 18:14
  • 4
    @akp: You keep harping on "pass an ImageIcon object by value" when your true problem likely has nothing to do with this. Why do you think that this matters? – Hovercraft Full Of Eels Jun 30 '12 at 18:14
  • 3
    @akp: More on resampling in the article cited [here](http://stackoverflow.com/a/6916719/230513). – trashgod Jun 30 '12 at 19:00
  • @HovercraftFullOfEels : LOL, these are called `VIBES`, just at this time I was watching this answer, couldn't upvote this last night, since was out of limit, we did that exactly the same time :-) – nIcE cOw Jul 01 '12 at 06:25
  • @nIcEcOw: what amazes me are all the upvotes for the question when all his comments suggest that he's barking up the wrong tree, that he doesn't understand the true nature of his problem. He's fixated on how ImageIcon parameters are passed, but doesn't understand that this has absolutely nothing to do with image quality. Hard to get him to believe this though. – Hovercraft Full Of Eels Jul 01 '12 at 06:39
  • @HovercraftFullOfEels : Exactly, that amazes me too. If one looks closely, it appears the OP is confused regarding references. As I looked into the first snippet of code provided by the OP, OP is writing `new ImageIcon()` for both the variables, when the OP should be using only one and assign that to the other. If the OP be able to understand that `ImageIcon img1 = new ImageIcon(); ImageIcon img2 = img1;` is the same thing what he wrote in a completely wrong way. No wonder why the OP is still puzzled :-) – nIcE cOw Jul 01 '12 at 06:51