• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Copy Picture (Image)

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for being a dufus. but I cant work out how I can make a copy of an Image object

What I need is 2 images

1 holds the current image and paints it if the display needs refreshing

The other retrieves an image from a URL


So what I have is this shortened for readability

Image i = getImage(URL(...));
Image c = null;

tracker = new MediaTracker(this);
tracker.addImage(i, 0);

// this runs in a thread
tracker.waitForID(0)
c = i; //copy image ???

...

later in the component..

void paintComponent(Graphics g) {
if (c != null)
g.paintimage(c,0,0);
....
}

The trouble is c seems to be invalid and I usually get rubbish painted

The question is
How do I copy 1 Image to another ?
c = i does not work.

Regards
Chris
[ September 07, 2005: Message edited by: Chris Topher ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c = i; //copy image ???
The copys the refrence.
Read: http://www.javaranch.com/campfire/StoryCups.jsp

You need to create a new Image, then draw the old one onto the new one.
 
Chris James
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quote ===================
c = i; //copy image ???
The copys the refrence.
Read: http://www.javaranch.com/campfire/StoryCups.jsp

You need to create a new Image, then draw the old i1 onto the new i1.
========================

My original post was a bit hard to follow sorry about that
I understand References, (at least in a c++ sense)

The following is a simplified version of what I am trying to accomplish

Shouldnt the following be valid
CODE -----------------------------------------------
Image i1; // null image reference
Image i2; // ditto

i1 = getImage(...); //new image created and its reference assigned to i1

i2 = i1; // now both i1 and i2 'refer' to the same image

i1 = getImage(...); // now i1 is assigned a reference to another new image

// i1 no longer points ('refers') to the same image as i2
--------------------------------------------------

I would have expected the last comment to be true, it appears it is not.

Instead it appears that getImage( ... ) re-uses the image referenced by i1 therefore invalidating i2 at the same time.

I will try the following to see if it makes a difference

CODE-------------------------------------------
Image i1; // null image reference
Image i2; // ditto

i1 = getImage(...); //new image created and its reference assigned to i1

i2 = i1; // now both i1 and i2 'refer' to the same image

*** i1 = null; **** i1 no longer points ('refers') to the same image as i2

i1 = getImage(...); // now i1 is assigned a reference to another new image

// i1 and i2 should now contain references to two distinct image objects
-----------------------------------------------

At this point I should have two images I can use as required

Is that correct or am I missing something fundamental here ?
[ September 07, 2005: Message edited by: Chris Topher ]
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm unable to replicate what you describe. The only thing I can offer is a suggestion to look at the method detail in the Toolkit api for the methods 'getImage' and 'createImage'.
This seems to work okay:
 
Chris James
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that.

The code you supplied was for a java application. I am having a problem when it is an Applet.

when I developed the applet it worked perfectly in the IDE which wraps the applet in an application to test it

But when the applet was loaded in a browser thats when the problem occured both in IE and Firefox using JRE 1.5

I didn't use the toolkit getImage() instead I used the Applet getImage().

I don't know if that has something to do with it

I will create an applet using your code and see if I can get it to fail.

When I added in the line...

i1 = null;

The problem was solved so I assume that perhaps as an efficiency applet.getImage() reuses the image already referenced by i1.

I cannot think of another cause for the symptoms I noted.

Regards
Chris
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic