×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    5

    color n color component

    hi all,

    i have been tryin to convert these statements of java to j2me but no success.
    can anybody do the needful.

    Color c = new Color(img.getRGB(i,j));
    if ((c.getBlue() <= 127) && (c.getRed() <= 127) && (c.getGreen() <= 127))

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

    Re: color n color component

    You should consider reading the MIDP API Javadocs... they will tell you exactly what you can and can't do.

    There is no AWT, so java.awt.Image must be replaced with javax.microedition.lcdui.Image. This class is not identical. MIDP API classes are generally far more limited in functionality than J2SE equivalents. Remember that the J2SE API is at around 100 times larger than MIDP.

    There is no Color class.

    You cannot retrieve the value of one pixel. You can retrieve an entire array of pixels, much like the way PixelGrabber works in AWT.

    You cannot replace a single pixel, but you can create a new image from a pixel array.

    I say "you can": you can on MIDP-2 devices, not on MIDP-1.

    PHP Code:
    // get image dimensions
    int width img.getWidth();
    int height img.getHeight();

    // create array large enough to hold all the pixels
    int[] pixels = new int[width height];
    // grab a copy of the pixels
    img.getRGB(pixels0width00widthheight);

    // compute the index for a pixel from (x,y)
    int i = (width) + x;
    // get the pixel value
    int pixel pixels[i];

    // deconstruct the colour
    int alpha = (pixel >> 24) & 0xff;
    int red   = (pixel >> 16) & 0xff;
    int green = (pixel >>  8) & 0xff;
    int blue  =  pixel        0xff;

    // construct a colour
    int newPixel = ((alpha 0xff) << 24) |
                   ((
    red   0xff) << 16) |
                   ((
    green 0xff) <<  8) |
                    (
    blue  0xff);

    // replace old pixel value in the pixel array
    pixels[i] = newPixel;

    // create a new image from the modified pixel array
    Image newImage Image.createRGBImage(pixelswidthheighttrue); 
    This can be very memory hungry. The int[] needs four bytes for every pixel (for an image 1024x1024, that would be 4Mb). The Image objects may need a similar amount of memory each.

    Cheers,
    Graham.

Posting Permissions

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