• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] Embarrassing question about my own code...

This topic is 6562 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have filtered my code many hours, but I can''t find the problem, This one is really basic so I hope you can find error what I can''t: This one is from engineController object, this is just simple test, because after I messed my code just before taking backups, I tried to make it very simple to find error.. ...snip... part of engineController class EngineCanvas extends Canvas { Image Background; public void paint(Graphics g) { Tausta = getToolkit().getImage("dildo2.gif"); MediaTracker mt = new MediaTracker (this); mt.addImage (Background, 0); try { mt.waitForAll(); System.out.println("Image is loaded"); } catch (Exception e) { e.printStackTrace(); } g.drawImage(Background, 0, 0, this); } } heheh dildo2.gif is picture from my PC, I named it dildo because it looked... I''m not going to continue I''m calling engine canvas in my main object which tests the game. Here is how i load my canvas Canvas GoDView = new EngineCanvas(); It lies in borderlayout center, Jim told me that I should use setSize/setBounds, tried with it, negative

Share this post


Link to post
Share on other sites
Advertisement
I''m not sure this is it, and I''m not a c++ person (i like C), but make the "public void paint(Graphics g) {" line into this:
public:
void paint(Graphics g) {
I''m not sure if that''ll fix it, but try

Share this post


Link to post
Share on other sites
Hehe, it''d help if I''d realized that was a java forum... anyway, try it, it might work

Share this post


Link to post
Share on other sites
>>Tausta = getToolkit().getImage("dildo2.gif");


I hope you meant Background instead of Tausta, because I didn''t see "Tausta" anywhere else in the snippet.

JoeG

Share this post


Link to post
Share on other sites
I tried your code and got it working by setting the ImageObserver to null in the paint method. The problem seemed to be that the Component''s (the baseclass of Canvas) imageUpdate() method is trying to repaint the screen when new info about the image appers, but upon repaint you try to load a new image. And when that image is being loaded you get a repaint and when you get a repaint you start to load a new image and... Well you get the point

It''s not exactly a infinite loop as the repaint is scheduled by the repaint/event dispatch thread, thus there are some breaks in the loop that allow it to end. The image drawing just fails for some reason.

The practice of loading images in the paint() method is really not a recommended way to do image loading. At least doing it the way you''ve done it creates one image and one MediaTracker object per repaint. But since the code was only example code to make a point, I''ll forgive you

Share this post


Link to post
Share on other sites
That Tausta thing was that I translated Tausta to english background
And I''m drawing that in there, because I tried to make my code so simple that first-timer can get it working...

Share this post


Link to post
Share on other sites
Just wanted to say: put the image drawing thing into ''repaint'' - this way you should be able to avoid the ''loop''.

Share this post


Link to post
Share on other sites
I can''t get this work, it''s in repaint...I just can''t realize what I messed up, before this it painted all images, I wonder what I can do anymore? Can anyone of you tell me other way to draw images?
Btw javanerd you mentioned something about right way to load images? I''m loading my images in separate object called imageController (I got this cool style to name images from JGDC), I just draw them in paint, but before that they''ll have some fun with maploader... Njha, I won''t even try it today, I just play games and stuff, yeah...

Share this post


Link to post
Share on other sites
Well I''m going out on a limb here and I could be totally off (which wouldn''t surprise me) but you have:
Canvas GoDView = new EngineCanvas();
shouldn''t it be:
EngineCanvas GoDView = new EngineCanvas();

just outta curiosity?

-- From someone who can''t find his left from his other left.

Share this post


Link to post
Share on other sites
Well I''m going out on a limb here and I could be totally off (which wouldn''t surprise me) but you have:
Canvas GoDView = new EngineCanvas();
shouldn''t it be:
EngineCanvas GoDView = new EngineCanvas();
---

Both works, I don''t know which is right way...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Canvas GoDView = new EngineCanvas();
vs.
EngineCanvas GoDView = new EngineCanvas();
----
There is no "right way" for this - it depends on what you need. If you want to transparently use an EngineCanvas in place of Canvas then the first way will do just fine. The second way is useful if you know you need to talk to an instance of EngineCanvas - so you can call Engine Canvas specific methods that aren''t found on Canvas.

Share this post


Link to post
Share on other sites
Moving the painting to repaint() doesn''t change anything as the repaint() actually invokes paint() after clearing the background...

Just change the line reading:
g.drawImage( Background, 0, 0, this );
to
g.drawImage( Background, 0, 0, null );

in the example and it works. At least in the example you don''t need the ImageObserver. Also in your original code you don''t seem to need it if you are handling the loading in different place anyhow and make sure the images are loaded before you draw them.

I was just commenting that loading an image in paint() sounds like a bit odd idea as image loading can take quite a long time. Doing the image loading in some init() method would be more appropriate, but that was not the point of the example anyhow...

Maploader? Now I''m getting confused... imageController sounds like a controller of all used images, but what on earth does a maploader do in this mess? Well, anyhow you asked why your example code doesn''t work and after that change it''ll work, so that should answer your original question, right?

Share this post


Link to post
Share on other sites
Maploader is actually named wrong, it should be mapCreator, because I''ll create dynamic maps except world and town maps, I once did this with C++ and with that experience I think I''ll do it in different object...

Share this post


Link to post
Share on other sites
Something is perfectly messed... while I can draw retangles and ovals I can''t draw image anyhow... None of these examples didn''t work, I quess there is little object structure fault which causes that I can''t draw images on a canvas... Weird huh? That null thing didn''t work I quess I''m now out of ways to do this in this way...

Hey don''t call my thingie mess, It''s not mess, it''s just... complicated...

Share this post


Link to post
Share on other sites
Messy, complicated, whatever... Either way in real workplace you would be put to write it again

But hey, this is the reason I''m writing things on my spare time, nobody is watching over the shoulder

One thing came to my mind. Are you sure that a window peer has been created when you load the images? In other words, a window has been shown and it has received addNotify() notification? Or was this only needed for that createImage(int, int) method?

Share this post


Link to post
Share on other sites
Yeah, addNotify just throws hhuuge bunch of errors after I runned it, compieler did not error at all... You said you got this working somehow by seeting imageObserver null? Can you throw that one in my E-mail which is Archaon@online.tietokone.fi

Share this post


Link to post
Share on other sites

  • Advertisement