This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Copy Picture (Image) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Copy Picture (Image)" Watch "Copy Picture (Image)" New topic
Author

Copy Picture (Image)

Chris James
Greenhorn

Joined: Sep 07, 2005
Posts: 19
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 ]
Michael Lloyd Lee
Greenhorn

Joined: Sep 07, 2005
Posts: 22
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.


Please please please use code tags!<br /> <br /><a href="http://java.sun.com/j2se/1.5.0/docs/api/" target="_blank" rel="nofollow">Java API</a> - <a href="http://java.sun.com/docs/books/tutorial/index.html" target="_blank" rel="nofollow">Java Tutorials</a>
Chris James
Greenhorn

Joined: Sep 07, 2005
Posts: 19
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 ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Sep 07, 2005
Posts: 19
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copy Picture (Image)
 
Similar Threads
ANT - and was unexpected at this time.
Load image fromt disk to servlet
How to make sense of my binary data
Image to BufferedImage converter
How to transfer a bufferedImage Object?