[java] Drawing Image problems

Started by
3 comments, last by TreizeSG 20 years, 8 months ago
public void paint(Graphics g){ g.drawImage(getToolkit().getImage("Images" + java.io.File.separator + game.currentMap() + ".gif"), 0, 0, this); if(game.getFighters() != null){ Vector over = game.getFighters(); Fighter f; for(int index = 0; index<over.size(); index++){ f = (Fighter)over.elementAt(index); g.drawImage(getToolkit().getImage("Images" + java.io.File.separator + f.getArmor().getImageName() + ".gif") , f.getLocation().getCol()*40, f.getLocation().getRow()*40, this); g.drawImage(getToolkit().getImage("Images" + java.io.File.separator + f.getWeapon().getImageName() + ".gif") , f.getLocation().getCol()*40, f.getLocation().getRow()*40, this); g.drawImage(getToolkit().getImage("Images" + java.io.File.separator + f.getClas() + ".gif") , f.getLocation().getCol()*40, f.getLocation().getRow()*40, this); } } } This is a paint() method from a subclass of Canvas that I wrote. However, when I display it on the screen, the characters, their armor and weapons flash quickly and then disappear. The Map shows fine. game.getCurrentMap() gets the name of the map to draw onto the screen. Fighter.getClas() returns the fighter''s game class, not the Java class, so it knows what to draw. I know class is misspelled. getClass() is final.
Project ARPEG: Product, Darkness SeigeThe seige begins...
Advertisement
Doh, and then people say java is slow!
quote:Original post by TreizeSG g.drawImage(getToolkit().getImage("Images" + java.io.File.separator + game.currentMap() + ".gif"), 0, 0, this);

This is a perfect example how to waste ressources and killing performances! ;-)
In this line: you load an image, draw it, and it is then thrown away (and this for each small square on the grid, thousand times per minute...)
Better is: load it once and store it, then draw it when needed.
quote:
if(game.getFighters() != null){
Vector over = game.getFighters();

again, two method calls instead of one... Remember the paint method is called again and again at a high speed rate, and thus, is a one of the critical points of your app! Ok ok, i know, you aren't concerned about speed... but anyway, let's do it right from the beginning, shoudn't we?
It's as simple as changing the 2 lines:
Vector over = game.getFighters();
if(over != null){
Not hard, huh? :-P

quote:
g.drawImage(getToolkit().getImage("Images" + java.io.File.separator + f.getArmor().getImageName() + ".gif") , f.getLocation().getCol()*40, f.getLocation().getRow()*40, this);
g.drawImage(getToolkit().getImage("Images" + java.io.File.separator + f.getWeapon().getImageName() + ".gif") , f.getLocation().getCol()*40, f.getLocation().getRow()*40, this);
g.drawImage(getToolkit().getImage("Images" + java.io.File.separator + f.getClas() + ".gif") , f.getLocation().getCol()*40, f.getLocation().getRow()*40, this);

Oh here again, so many getImage(...) , this is horrible, my eyes are bleeding! (...and people then say java is slow..) Ok ok, i'm cheering you. But loading images should only be done once, and not every time you wanna paint them!


By the way, what's that for a line?!?
quote:
for(int index = 0; index f = (Fighter)over.elementAt(index);

?????????


My first guess is that game.getFighters() returns the list the first time, and then, somehow, returns null . Is it possible?

Good luck!
And take a little more time to read/code and wonder about problems next time! ;-)

[edited by - misterx on August 15, 2003 4:45:03 PM]
I think you''re confusing getImage with createImage. The two are not the same. The toolkit will cache the image the first time it''s retrieved, and each subsequent call pulls from that cache. But, even so, you really should get your own reference to your images and manage themselves, you never know when the toolkit''s cache will become invalid and it suddenly has to load from disk (ouch!)

What do you mean the characters flash? They only appear briefly, or are constantly flashing? It sounds like a problem with double buffering, but that''s just a guess. If you had created a tiny example program that demonstrated the problem you would most likely have figured out the problem yourself. If not, you could have posted the entire program and nobody would have to do any guessing or assuming, we could show you exactly what the problem is. Just because the problem appears to be in paint(), doesn''t necessarily mean it is. The code you posted appears to be fine.
oops, yeah, you''re right i''ve wrongly merged getImage and createImage. Sorry for my misleading post then.
Grrr... I half-fixed it. I just rearranged the code in paint() so that if the vector was empty, don't bother to do anything. So it only paints if there are characters. Just sort of a brute force method for now, until I realize when I'm implementing something else that... "oh shit, THAT's what went wrong!"

Thanks for the pointers MX.

EDIT: Is there anyway to set a background Image? or can that only be done with colors?

Another EDIT: I figured out that i really only did half-fix it. It seems that anything coming from the update checking thread returns a 0 size vector. If it comes directly from the key-type, it gives the correct vector. I'll be able to fix that.

[edited by - TreizeSG on August 16, 2003 9:20:03 AM]

[edited by - TreizeSG on August 16, 2003 9:31:14 AM]
Project ARPEG: Product, Darkness SeigeThe seige begins...

This topic is closed to new replies.

Advertisement