Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Fastest way to draw only a part of an image  (Read 1757 times)
0 Members and 2 Guests are viewing this topic.
Offline stefanha

JGO n00b
*

Posts: 19


I love YaBB 1G - SP1!


« on: 2002-12-20 23:14:52 »

I'm wondering what the fastest way to draw only a portion of an Image is.  Right now I am using g.drawImage ( Image image, int sx1, int sy1, int sx2, int sy2, int dx1, int dy1, int dx2, int dy2, ImageObserver observer ).  However, this function can also do scaling, so I'm not sure if it is optimized to deal with the case when no scaling has to be done.

The other way I know is to do g.setClip (...) and then g.drawImage().  But then I have create a compatible Graphics object from g, as to not screw up the original clipping area.  I think that might slow things down...

Which way is faster?

from, Stefan
Offline stefanha

JGO n00b
*

Posts: 19


I love YaBB 1G - SP1!


« Reply #1 on: 2002-12-23 23:19:20 »

Okay, I've done some testing and figured out the fastest way to do it.  Both the scaled drawImage() and g.create() are not the fastest.  The best way to do it is:
1  
2  
3  
4  
5  
6  
7  
paint (Graphics g)
{
Shape oldClip = g.getClip ();
g.setClip (x, y, width, height);
g.drawImage (sx, sy, x - sx, y - sy, null );
g.setClip (oldClip);
}


The cool thing about this approach is that if you have a loop that renders several pieces from different images, you can take the "Shape oldClip = g.getClip()" and "g.setClip (oldClip)" out of the loop and only call them once the loop is done, saving yourself some processor cycles.

from, Stefan
Offline pepe

Sr. Member
**

Posts: 402


Nothing unreal exists


« Reply #2 on: 2003-01-03 03:04:47 »

i seem to recall from an old thread of the previous board that setClip() creates garbage.
Can anyone confirm?

Home page: http://frederic.barachant.com
------------------------------------------------------
GoSub: java2D gamechmark http://frederic.barachant.com/GoSub/GoSub.jnlp
Games published by our own members! Go get 'em!
Offline cknoll

Full Member
**

Posts: 126


Flame On!


« Reply #3 on: 2003-01-03 16:51:39 »

I hae used set clip to redraw various sprites on a game board and did not notice gc pauses due to excessive garbage collecton (and the game was running at 60fps adn there were about 5 set clips per frame).

-Chris
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.066 seconds with 19 queries.