Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

draw image

0 views
Skip to first unread message

Mike Crenshaw

unread,
Dec 5, 2002, 4:11:56 PM12/5/02
to
hi
im trying to draw an image.. i've trying something like this but it wont
work.. any ideas?

public void paint(Graphics g)
{
Toolkit t = this.getToolkit();
Image picture = t.createImage("c:\\test.jpg");
g.drawImage(picture,50,50, this);
repaint();
}


Chris Smith

unread,
Dec 5, 2002, 5:30:21 PM12/5/02
to
Mike Crenshaw wrote ...

When you call createImage, a new Image object is created, and that new
Image object KNOWS where to get its data, but hasn't actually GOTTEN
that data yet. You'll need to display the image after it has loaded,
which is generally done by using the MediaTracker class. Yes, this is
insanely complicated, but it's all a hold-over from when Java was
supposed to be used mainly for applets, and images might take a long
time to load over the internet.

You could vastly improve the performance of that program as well by
loading the image only once, and then just doing the g.drawImage in
paint. The image loading could be done in, for example, an overload of
addNotify.

If that's not it, can you define "won't work"? For one thing, calling
repaint() in the end of the paint method seems rather suspicious, but it
shouldn't prevent the code from working.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Cowboy Bob

unread,
Dec 5, 2002, 10:43:48 PM12/5/02
to
Chris Smith wrote:

> If that's not it, can you define "won't work"? For one thing, calling
> repaint() in the end of the paint method seems rather suspicious, but it
> shouldn't prevent the code from working.
>

It will unless you override the update method to stop it blanking the
applet to its background image. Could flicker like a very flickery thing if
the OP uses a MediaTracker to grab the image.

Bob

--
"All I wanted in the end was world domination and a whole lot of money
to spend" - Justin Sullivan

Registered Linux User #228620

William Keith

unread,
Dec 5, 2002, 6:20:51 PM12/5/02
to

Try changing your paint class to something like this:

public void paint(Graphics g)
{
Image picture = Toolkit.getDefaultToolkit().getImage(c:\\test.jpg);
g.drawImage(picture, 50, 50, this);
..
}

Mike Crenshaw

unread,
Dec 6, 2002, 12:22:02 PM12/6/02
to
> When you call createImage, a new Image object is created, and that new
> Image object KNOWS where to get its data, but hasn't actually GOTTEN
> that data yet. You'll need to display the image after it has loaded,
> which is generally done by using the MediaTracker class. Yes, this is
> insanely complicated, but it's all a hold-over from when Java was
> supposed to be used mainly for applets, and images might take a long
> time to load over the internet.

i dont get the mediatracker thing.. but it worked:) here's the snip for
those who wants to know

public void paint(Graphics g)
{
try


{
Toolkit t = this.getToolkit();
Image picture = t.createImage("c:\\test.jpg");

MediaTracker mt = new MediaTracker(this);
mt.addImage(picture,0);
mt.waitForAll();
g.drawImage(picture, 5, 30, this);
}
catch(Exception e){System.out.println(e);}
}
}

> You could vastly improve the performance of that program as well by
> loading the image only once, and then just doing the g.drawImage in
> paint. The image loading could be done in, for example, an overload of
> addNotify.

hehe i think i should concentrate on making it work first.. but thanx for
the tip:)

> for one thing, calling > repaint() in the end of the paint method seems


rather suspicious, but it
> shouldn't prevent the code from working.

hehe dont worry about the repaint.. it wasnt supposed to be there:)


Mike Crenshaw

unread,
Dec 6, 2002, 12:23:17 PM12/6/02
to
> Try changing your paint class to something like this:
>
> public void paint(Graphics g)
> {
> Image picture = Toolkit.getDefaultToolkit().getImage(c:\\test.jpg);
> g.drawImage(picture, 50, 50, this);
> ..
> }

hmm how is this different from what i did? in any case it doesnt work:(


Babu Kalakrishnan

unread,
Dec 7, 2002, 1:54:14 AM12/7/02
to
On Thu, 5 Dec 2002 15:30:21 -0700, Chris Smith <cds...@twu.net> wrote:
> Mike Crenshaw wrote ...
>> hi
>> im trying to draw an image.. i've trying something like this but it wont
>> work.. any ideas?
>>
>> public void paint(Graphics g)
>> {
>> Toolkit t = this.getToolkit();
>> Image picture = t.createImage("c:\\test.jpg");
>> g.drawImage(picture,50,50, this);
>> repaint();
>> }
>
> If that's not it, can you define "won't work"? For one thing, calling
> repaint() in the end of the paint method seems rather suspicious, but it
> shouldn't prevent the code from working.
>

Yes the repaint is call indeed incorrect since it can (and probably
would) cause continuous repainting to occur and you'd find that your app
is taking up 100% CPU.

But I think the problem why this doesn't work is that everytime paint()
is called, the method is trying to paint a new image (the code is
constructing a new one). The image observer interface would trigger
repaints as the image loads, but the paint method doesn't paint that
image object (the one which has been loaded) the next time around - but
instead tries loading another copy of the same.

IIRC there is some caching mechanism inside the AWT that keeps track of
images loaded and tries to reuse them. But if my memory serves me right
it does so for images loaded through URLs.

To the OP:

In any case, moving the first two lines out of the paint method (The
variable picture would have to become an instance variable instead of
local) and deleting the last line should make it work right.

Also if this is a Swing component (JPanel), override the paintComponent
method and not the paint method to perform your custom painting.

BK

Mike Crenshaw

unread,
Dec 7, 2002, 5:40:35 AM12/7/02
to
> Yes the repaint is call indeed incorrect since it can (and probably
> would) cause continuous repainting to occur and you'd find that your app
> is taking up 100% CPU.

the repaint wasnt supposed to be in the post.. it was only a test thing.. i
guess i was tired:)

> In any case, moving the first two lines out of the paint method (The
> variable picture would have to become an instance variable instead of
> local) and deleting the last line should make it work right.

thanx that worked too:).. i made it work with the mediatracker but this
makes much more sense.. moving the image instance out of the paint method..

thanx guys:)


0 new messages