×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Oct 2004
    Posts
    52

    Color class in J2ME

    Hi,
    I am porting code from J2SE to J2ME and I am wondering how to implemet some functions of the Color class.

    The original code I have looks something like this:
    Code:
    Color c = new Color(pixVals[i + j * w]);
    workingRedTable[i * spacing + j * w * spacing * spacing] = c.getRed();
    workingGreenTable[i * spacing + j * w * spacing * spacing] = c.getGreen();
    workingBlueTable[i * spacing + j * w * spacing * spacing] = c.getBlue();
    and I am trying to do it in J2ME like this:
    Code:
    g.setColor(pixVals[i + j * w]);
    workingRedTable[i * spacing + j * w * spacing * spacing] = g.getRedComponent();
    workingGreenTable[i * spacing + j * w * spacing * spacing] = g.getGreenComponent();
    workingBlueTable[i * spacing + j * w * spacing * spacing] = g.getBlueComponent();
    but I am getting a nullPointerException as soon as it hits the setColor.

    This code is not in a paint method and g is a global Graphics variable. I have a feeling it might be a problem in the way I am using Graphics.

    Does anyone know what I am doing wrong here?

    Thanks

  2. #2
    Regular Contributor
    Join Date
    Jan 2004
    Posts
    165
    Have you definitely allocated the memory for the tables pixVals and the working tables. That is the most likely problem.

    Also is your intention for [i + j * w] j to be added to i then multiplied by w, if so then use [(i + j) * w].

  3. #3
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    To what does "g" refer?

    I'm guessing it's null.

    Since the colour number is always 0x00rrggbb, you can use bit manipulation rather than the API. I suspect that this will also speed your program up dramatically.

    So:
    Code:
    int colour = whereverYouGetYourColourFrom;
    int red = (colour >> 16) & 0xff;
    int green = (colour >> 8) & 0xff;
    int blue = colour & 0xff;
    You can put the colour back together with the opposite process.

    Graham.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Talking Re: Color class in J2ME

    Dude , now I know why it says nokia champion next to your name ...

Posting Permissions

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