×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Super Contributor
    Join Date
    Mar 2003
    Posts
    580

    Overdrawing an image using Netbeans

    I added an ImageItem to a form using Netbeans; this is the resulting code:

    Code:
        public ImageItem getImageItem() {
            if (imageItem == null) {                                 
                // write pre-init user code here
                imageItem = new ImageItem("Grande Raccordo Anulare", getImage1(), ImageItem.LAYOUT_DEFAULT, "<Missing Image>");                                   
                // write post-init user code here
            }                         
            return imageItem;
        }
        //</editor-fold>                       
    
        //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">                                   
        /**
         * Returns an initiliazed instance of image1 component.
         * @return the initialized component instance
         */
        public Image getImage1() {
            if (image1 == null) {                                 
                // write pre-init user code here
                try {                                                
                    image1 = Image.createImage("/gra-num.png");
                } catch (java.io.IOException e) {                                              
                    e.printStackTrace();
                }                                   
                // write post-init user code here
            }                         
            return image1;
        }
    Now how can I draw over the image?
    I'm trying doing it in startapp:

    Code:
        public void startApp() {
            if (midletPaused) {
                resumeMIDlet ();
            } else {
                initialize ();
                startMIDlet ();
            }
            midletPaused = false;        //InvioRichiesta("http://www.baluardoproductions.com/gra/");
             try {
                        getViaStreamConnection("http://www.baluardoproductions.com/gra/");
              } catch (IOException e) {
                        //Handle Exceptions any other way you like.
                        System.out.println("IOException " + e);
                        e.printStackTrace();
              }        
            Graphics graphics = image.getGraphics();
            graphics.setColor(255, 0, 0);
            graphics.fillArc(10, 10, 60, 50, 180, 180);
            display.setCurrentItem(getImageItem());
        }
    But it does not work: how do I get image refreshed?!?

  2. #2
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,405

    Re: Overdrawing an image using Netbeans

    Hi cassioli,

    you cannot modify an Image loaded an external resource, since, as from JavaDocs, it's an Immutable image:
    Immutable images are generally created by loading image data from resource bundles, from files, or from the network. They may not be modified once created.
    The only way to get a mutable image would be to create an Image with the createImage(int width, int height) method, and then using the getGraphics() method the way you do.
    So, in your case, you could try first copying the immutable Image data into a mutable one, and then working on it.

    Pit

  3. #3
    Super Contributor
    Join Date
    Mar 2003
    Posts
    580

    Re: Overdrawing an image using Netbeans

    Quote Originally Posted by jappit View Post
    Hi cassioli,

    you cannot modify an Image loaded an external resource, since, as from JavaDocs, it's an Immutable image:

    The only way to get a mutable image would be to create an Image with the createImage(int width, int height) method, and then using the getGraphics() method the way you do.
    So, in your case, you could try first copying the immutable Image data into a mutable one, and then working on it.

    Pit
    yes, it was hard to find out this method, but I eventually got it.
    createImage(x,y) creates a mutable image
    createImage("file") creates an immutable image
    Graphics offGrap = mut_image.getGraphics() gets a "working copy" of mutable image
    offGrap.drawImage(imm_image, 0, 0, 0) copies loaded image into working copy
    Then you can draw over working copy, and eventually "commit changes" using g.drawImage(mut_image, 0,0, Graphics.LEFT | Graphics.TOP);

    All of this must be done in paint() method, in Canvas() class appended to midlet class:
    Code:
    protected void paint(Graphics g) {
    Graphics offGrap = mut_image.getGraphics(); // Get a working copy (=graphics) of the mutable image for drawing on it.
    offGrap.setColor(0xff0000);
    offGrap.drawImage(imm_image, 0, 0, 0); // Copy immutable (loaded) image into mutable image.
    for (int i=0; i<=23; i++) {
    offGrap.setColor(mTrafficLC.Colors[i][0],mTrafficLC.Colors[i][1],mTrafficLC.Colo
    rs[i][2]);
    offGrap.drawLine(mTrafficLC.Segments[i][0],mTrafficLC.Segments[i][1],mTrafficLC.
    Segments[i+1][0],mTrafficLC.Segments[i+1][1]);
    }
    g.drawImage(mut_image, 0,0, Graphics.LEFT | Graphics.TOP);
    }
    
    public void commandAction(Command command, Displayable display) {
    if (command == exit) {
    mTrafficLC.exitMIDlet();
    }
    }

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    7

    Re: Overdrawing an image using Netbeans

    How do I get to have persistent modification to pixels of the image?

  5. #5
    Nokia Developer Champion
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Overdrawing an image using Netbeans

    I guess that would be the part that says :
    "Then you can draw over working copy, and eventually "commit changes" using g.drawImage(mut_image, 0,0, Graphics.LEFT | Graphics.TOP);"
    In that moment of time you actually have created a new Image (mut_image)...
    Or are you saying you want to save it ? In that case you will have to do so within the RMS (or a file via PIM), it's not like you can add it to the JAR file like you porbably did with the original Image (which would be packaged within the JAR)...

Similar Threads

  1. read image from jar - NETBEANS
    By js001 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 4
    Last Post: 2008-04-16, 21:19
  2. Opening a JPEG Image
    By ummarbhutta in forum Mobile Java Media (Graphics & Sounds)
    Replies: 8
    Last Post: 2007-02-15, 06:34
  3. how to cut some part of Image
    By mshouab in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2006-08-04, 09:05
  4. HELP: Mutable Image to Immutable Image?
    By rj_cybersilver in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2005-03-26, 09:58
  5. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31

Posting Permissions

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