Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 Captain Goatse   Banned   -  Reputation: 100

Like
Likes
Like

Posted 01 February 2000 - 04:37 AM

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

Sponsor:

#2 Cloxs   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 February 2000 - 04:52 AM

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

#3 Cloxs   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 February 2000 - 04:53 AM

Hehe, it''d help if I''d realized that was a java forum... anyway, try it, it might work

#4 joeG   Members   -  Reputation: 172

Like
Likes
Like

Posted 01 February 2000 - 06:23 AM

>>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

#5 javanerd   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 February 2000 - 08:43 AM

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

#6 Captain Goatse   Banned   -  Reputation: 100

Like
Likes
Like

Posted 02 February 2000 - 12:07 AM

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...

#7 Alaspin   Members   -  Reputation: 122

Like
Likes
Like

Posted 02 February 2000 - 12:13 AM

Just wanted to say: put the image drawing thing into ''repaint'' - this way you should be able to avoid the ''loop''.


#8 Captain Goatse   Banned   -  Reputation: 100

Like
Likes
Like

Posted 02 February 2000 - 12:32 AM

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...

#9 Smoo   Members   -  Reputation: 122

Like
Likes
Like

Posted 02 February 2000 - 01:46 PM

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.

#10 Captain Goatse   Banned   -  Reputation: 100

Like
Likes
Like

Posted 03 February 2000 - 01:01 AM

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...

#11 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 03 February 2000 - 01:42 AM

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.


#12 javanerd   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 February 2000 - 03:08 AM

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?

#13 Captain Goatse   Banned   -  Reputation: 100

Like
Likes
Like

Posted 03 February 2000 - 03:56 AM

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...

#14 Captain Goatse   Banned   -  Reputation: 100

Like
Likes
Like

Posted 03 February 2000 - 04:21 AM

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...

#15 javanerd   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 February 2000 - 07:15 PM

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?

#16 Captain Goatse   Banned   -  Reputation: 100

Like
Likes
Like

Posted 05 February 2000 - 03:31 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS