aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Quick Question about Images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Quick Question about Images" Watch "Quick Question about Images" New topic
Author

Quick Question about Images

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I was wondering wether it is more computationally expensive to say draw an image 200 by 200...

g.drawImage(image,x,y,this);

than it is to just draw a rectangle / polygon of the same size...

g.setColor(Color.yellow);
g.fillRect(x,y,200,200);

I am doing an animation, and would like to know if there is any difference, I guessed drawing an image would be more costly, but I'm not sure.

Thanks for any help.
dave taubler
Ranch Hand

Joined: May 15, 2001
Posts: 132
I'm not an expert at graphics per se, but I understand a bit. I believe that drawing an actual in-memory image via drawImage() is more efficient (i.e. faster) than drawing shapes yourself (e.g. fillRect(), drawLine(), etc). In either case, your app will need to draw in image to the screen, but when you use fillRect(), etc, it also needs to perform the calculations and actually create the image.

In the example you give, the difference would probably be unnoticeable, but I suspect you were just providing a simplistic example.

If you're talking about persisting the images, though, then storing the image itself would generally (but not always) be more expensive than storing the instructions to render the image. Take your example. To store that particular image, depending on your file format, you'd have to save out every byte that makes up the 200x200 yellow square (although of course, formats like GIF and PNG dramatically help out). To save out the instructions, you'd simply need to save something that says to draw a 200x200 yellow rectangle. That's why "vector" graphics (e.g. that describe the geometric shapes to be drawn) often result in much more compact file sizes than "raster" graphics (e.g. that describe how each pixel in the image must be drawn).

But back to your question... at the point of doing your animation, you'll already have everything in memory that you need to draw the image. So doing the calculations over and over again is just a waste of cycles. The one caveat, of course, is that I am assuming that the frames of your animation are not going to change.


Dave Taubler<br />Specializing in <a href="http://taubler.com/articles/" target="_blank" rel="nofollow">Java and Web Development</a>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
We had some discussion about performance recently. Here.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
I couldn't get that link to work, is that page still available?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
The URL was misspelt. Sorry. Try Here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quick Question about Images