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

Started by
14 comments, last by Arch@on 24 years, 2 months ago
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
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
Hehe, it''d help if I''d realized that was a java forum... anyway, try it, it might work
>>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
joeG
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
-Pasi Keranen
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...
Just wanted to say: put the image drawing thing into ''repaint'' - this way you should be able to avoid the ''loop''.
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...
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.
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...

This topic is closed to new replies.

Advertisement