×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    18

    Graphics.drawRegion versus setClip and translate

    Hi guys,

    I am scrolling a large image in a J2ME application on a regular Canvas, but have a question regarding performance and best practice.

    In terms of readability and implementation, the g.translate and g.setClip approach is far simpler. But, will I notice significant (or even any) performance improvements with the g.drawRegion approach?

    Many thanks,
    Lea Hayes

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Graphics.drawRegion versus setClip and translate

    Hmmm... you might find that which performs better varies from device to device. For Nokias, I'd guess it would make no difference.

    You might like to implement it both ways.

    Code:
    if (Device.USE_DRAWREGION) {
        // draw screen using drawRegion()
    } else {
        // draw screen using setClip()/drawImage()
    }
    Then have a class like:

    Code:
    public class Device {
        public static final boolean USE_DRAWREGION = false;
    }
    You can have different versions of this class for different devices, kind of like having different .h files for a C program.

    (Principle: when the expression in an "if" can be computed at compile time, because the values in it are either literal values or "static final", then the "if" is removed, and only the "true" code included in the compiler's output. This functions like conditional-compilation in C/C++ programs.)

    Graham.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    18

    Re: Graphics.drawRegion versus setClip and translate

    Hi Graham,

    I'll have to give it a try on some different phones. The clipping approach appears to work fine on my Nokia phone.

    My concern was with the underlying API enumerating each pixel of the source image despite the clipping rectangle being considerably smaller.

    Thanks for your advice, the macro-type approach may well be the way I need to go.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Graphics.drawRegion versus setClip and translate

    Quote Originally Posted by kruncher View Post
    My concern was with the underlying API enumerating each pixel of the source image despite the clipping rectangle being considerably smaller.
    Painting through a clip region doesn't usually appear to be a problem. Most of the games I've worked on used setClip/drawImage in preference to drawRegion, since we usually wanted to support some MIDP-1 devices (drawRegion is new in MIDP-2). I have a feeling that some older Samsung devices (pre D500) might paint more slowly when painting through a clip region, but I never got time to validate this, and newer Samsung devices (D500 and later) perform much better.

    I'm pretty sure you'll be safe either way, and I'd recommend doing whichever you're happy with. However, you can keep your options open easily enough.

    You can read about several approaches to writing code for multiple devices in Java Porting in the wiki.

    Graham.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    18

    Re: Graphics.drawRegion versus setClip and translate

    Thanks for the fantastic link, I hadn't realized how many different issues there were in terms of porting. I am going to spend some time reading through the different Wiki pages, there is a lot of interesting info there.

Similar Threads

  1. SVG, translate() and setClip()
    By mammal2 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 3
    Last Post: 2007-01-17, 17:21

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×