×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    41

    Drawing on image in res folder

    Hi,

    I am fairly new to the graphics side of things and was wondering how I can draw on an image which is in my res folder. My current code is :

    Image im = Image.createImage("/imagefromresfolder.png");
    drawdots(im,xco,yco);//xco,xyco are ints but not relevant to this example

    public void drawdots(Image img, int x, int y) {

    img.getGraphics().setColor(0xFF0000);
    img.getGraphics().drawLine(0, 0, 100, 100);
    }

    I get an illegal state exception which leads me to believe that I am unable to get the graphics and manipulate images in this way. I would greatly appreciate any help. Many thanks

    Jules
    Last edited by jules_; 2009-09-15 at 12:19. Reason: comments

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

    Re: Drawing on image in res folder

    Image objects are either "mutable" (you can draw on them) or "immutable" (you cannot draw on them).

    You can find out which kind of image you have, using the isMutable() method. If this returns true, then getGraphics() will return a Graphics object. If false, then getGraphics() will throw an IllegalStateException.

    Images created from a resource file are always immutable. You can check the JavaDocs for the kind of Image returned from each method.

    The only way to create a mutable Image is to use createImage(width,height). This creates an image, in which all the pixels are white.

    You can:

    Code:
    public static Image createMutableImage(String filename) throws IOException {
        Image imgResource = Image.createImage(filename);
        Image imgToDrawOn = Image.createImage(imgResource.getWidth(), imgResource.getHeight());
        Graphics g = imgToDrawOn.getGraphics();
        g.drawImage(imgResource, 0, 0, Graphics.TOP | Graphics.LEFT);
        return imgToDrawOn;
    }
    However, since the mutable image starts with all white pixels, if the resource-image you draw on it contains any transparent pixels, the original white pixels will show through. As a result, you cannot create a mutable image in which any pixels are transparent.

    (You can create a mutable image with transparent pixels by using the Nokia UI API, but you will be unable to use your code on non-Nokia devices.)

    Graham.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    41

    Re: Drawing on image in res folder

    Thanks for the reply.

    I'll give it a go shortly and post my results.

    Jules

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

    Re: Drawing on image in res folder

    Hi

    That worked perfectly. Thank you again.

    Jules

  5. #5
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Drawing on image in res folder

    yay ! Graham scores ! .... again....

Similar Threads

  1. [eSWT] How to use images in res folder in Browser?
    By nwoedf in forum Mobile Java General
    Replies: 0
    Last Post: 2009-09-01, 10:09
  2. --- ???save image problem??? ---
    By ferenn in forum Mobile Java Media (Graphics & Sounds)
    Replies: 6
    Last Post: 2007-10-01, 14:33
  3. Drawing Lines on an Image (image graphics object returns null)
    By nicenouman in forum Mobile Java General
    Replies: 4
    Last Post: 2006-09-21, 10:15
  4. saving a file in the image folder of nokia phones
    By jesanjose in forum Mobile Java General
    Replies: 2
    Last Post: 2005-02-03, 14:15
  5. saving jpeg image on grid list
    By flicker82 in forum Symbian
    Replies: 0
    Last Post: 2005-01-21, 04:22

Posting Permissions

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