Jump to content
  • Advertisement

Archived

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

TreizeSG

[java] Drawing Image problems

This topic is 5477 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

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
oops, yeah, you''re right i''ve wrongly merged getImage and createImage. Sorry for my misleading post then.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!