×

Discussion Board

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

    insert an image into a canvas

    hi, i'm writing a program in j2me which uses the canvas. I'm trying to insert an image into the canvas, but my WTK 2.5.2 returns an error about loading the image.
    this is the code:
    PHP Code:

        import  javax
    .microedition.midlet.*;
        
    import  javax.microedition.lcdui.*;
        
    import  java.io.*;
        
        
        public class 
    ProvaGrafica1 extends MIDlet implements CommandListener
          
    {
            
    Command exitCommand;
            
    Form form;

            public 
    ProvaGrafica1() {  }

            private 
    Image image;
            
            private final static 
    String IMAGE_PATH "/baby1.png";

            public 
    void setFullScreenMode(boolean mode) {  }

            public 
    void startApp()
              {
                
    exitCommand = new Command("exit",Command.EXIT,0);
                
                
    image null;
                
                try
                  { 
    image Image.createImage(IMAGE_PATH); 
                  
    System.out.println("immagine caricata");}
                catch(
    IOException ioe
                  { 
                  
    System.out.println("errore nel caricare l'immagine");
                    
    Alert errorAlert = new Alert("Errore","Image not present",null,AlertType.ERROR);
                    
    errorAlert.setTimeout(Alert.FOREVER);
                    
    Display.getDisplay(this).setCurrent(errorAlert); 
                  }
                
                
    Canvas1 movableCanvas = new Canvas1();
                
    movableCanvas.setFullScreenMode(true);
                
    Display.getDisplay(this).setCurrent(movableCanvas);
                      
              }
              
              
            public 
    Form getForm()
              {
                if (
    form == null)
                  {
                    
    form = new Form("Welcome", new Item[] {});
                    
    form.setCommandListener(this);
                  }
                return 
    form;
              }

            public 
    void commandAction(Command cDisplayable d)
              {
                if (
    c==exitCommand && d==getForm()){Display.getDisplay(this).setCurrent(getForm());}
              }
        
            public 
    void pauseApp() {  }
            public 
    void destroyApp(boolean unconditional) {  }


      class 
    Canvas1 extends Canvas{


              private final static 
    int RAGGIO 20;
              private final static 
    int STEP 5;
              private 
    int x,y;
              
              public 
    void paint(Graphics g){
            
    // Determiniamo le dimensioni del Canvas
                
    int canvasWidth   getWidth();
                
    int canvasHeight  getHeight();
                
    g.setColor(0x0000ff);
                
    g.fillRect(0,0,canvasWidth,canvasHeight);
                
    g.setColor(0xff0000);
                
    g.fillArc(x-RAGGIO,y-RAGGIO,RAGGIO*2,RAGGIO*2,0,360);
                if (
    image== nullSystem.out.println("immagine inesistente");
                
    g.drawImage(image, (360-(360/20)-40), (20+40), Graphics.BASELINE Graphics.LEFT);
              }
    // fine
              
    protected void pointerPressed(int xint y){
                
    move(xy);
              }
    // fine

              
    public void move(int moveXint moveY)
               {
                
    x=moveX;
                
    y=moveY;
                if ((
    >= (360-(360/20)-40)) && (<= (360-(360/20))) && (>= 20) && (<= 60)) commandAction(exitCommandgetForm());
                
    repaint();
                
    serviceRepaints();
              }

              }
            public 
    void exitMIDlet() 
              {
                
    destroyApp(true);
                
    notifyDestroyed();
              }  
                 

    the image is in the correct folder, end this is the error:

    java.lang.IllegalArgumentException:
    at ProvaGrafica1$Canvas1.paint(+92)
    at javax.microedition.lcdui.Canvas.callPaint(+85)
    at javax.microedition.lcdui.Display.repaint(+82)
    at javax.microedition.lcdui.Display.registerNewCurrent(+235)
    at javax.microedition.lcdui.Display.access$700(+6)
    at javax.microedition.lcdui.Display$DisplayAccessor.foregroundNotify(+46)
    at javax.microedition.lcdui.Display$DisplayManagerImpl.notifyWantsForeground(+152)
    at javax.microedition.lcdui.Display$DisplayManagerImpl.access$100(+6)
    at javax.microedition.lcdui.Display.setCurrent(+70)
    at ProvaGrafica1.startApp(+102)
    at javax.microedition.midlet.MIDletProxy.startApp(+7)
    at com.sun.midp.midlet.Scheduler.schedule(+270)
    at com.sun.midp.main.Main.runLocalClass(+28)
    at com.sun.midp.main.Main.main(+80)

    i made some tests and i noticed that the image image is loaded and the error is in the function g.drawImage(); why? (the image is smaller than the canvas!!)

    how can I do?? help me please!!

    (sorry for my english, but i'm italian...!)

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

    Re: insert an image into a canvas

    Bongiorno!

    Quote Originally Posted by diegoves View Post
    Code:
        g.drawImage(image, (360-(360/20)-40), (20+40), Graphics.BASELINE | Graphics.LEFT);
    drawImage() throws an IllegalArgumentException if the "anchor" is incorrect. In this case, BASELINE is used only for drawing text. Use BOTTOM instead, you should be fine. BASELINE is the only option that doesn't work for images. Likewise, you can't use VCENTER for text, only images.

    Cheers,
    Graham.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    6

    Re: insert an image into a canvas

    thanks for your answer! it's all right!
    but now i've a new problem...!

    how can i set the portrait mode also when the device is in landscape mode?

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

    Re: insert an image into a canvas

    As a rule, you cannot.

    On Series 60 5th Edition, you might be able to add to the JAD or MANIFEST:

    Code:
    Nokia-MIDlet-App-Orientation: portrait
    (or "landscape").

    However, I haven't tried this, and people report that it doesn't work (certainly on earlier firmware versions of the 5800 XpressMusic). It may work on later devices. It won't work on Series 60 devices before 5th Ed, and it won't work on non-Series 60s.

    Graham.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    6

    Re: insert an image into a canvas

    Quote Originally Posted by grahamhughes View Post
    As a rule, you cannot.

    On Series 60 5th Edition, you might be able to add to the JAD or MANIFEST:

    Code:
    Nokia-MIDlet-App-Orientation: portrait
    (or "landscape").

    However, I haven't tried this, and people report that it doesn't work (certainly on earlier firmware versions of the 5800 XpressMusic). It may work on later devices. It won't work on Series 60 devices before 5th Ed, and it won't work on non-Series 60s.

    Graham.
    it works on my N97! but I always have to insert the string after the emulator creates the jar file, there is a way to make it automatically from the WTK?

    how can I enlarge the display of the emulator devices up to 360x640?

    is there an emulator devices with the same display of the N97 (360x640) to be included in wtk 2.5.2?
    Last edited by diegoves; 2009-09-30 at 10:55.

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

    Re: insert an image into a canvas

    peccato that you are using the WTK :P
    I know how to set JAD parameters in NetBeans and somewhere in the Forum I've read thats also possible with Eclipse...
    Which version of the WTK are you using ? Cause 3.0 is actually a stripped down Netbeans...

Similar Threads

  1. Replies: 0
    Last Post: 2009-08-13, 02:21
  2. switch between image canvas and console output ?
    By sptkrishnan in forum Symbian
    Replies: 2
    Last Post: 2008-11-18, 14:25
  3. Image loading on canvas
    By pieterb23 in forum Mobile Java General
    Replies: 2
    Last Post: 2007-06-29, 16:35
  4. Opening a JPEG Image
    By ummarbhutta in forum Mobile Java Media (Graphics & Sounds)
    Replies: 8
    Last Post: 2007-02-15, 06:34
  5. problems displaying video
    By gorsken in forum Mobile Java Media (Graphics & Sounds)
    Replies: 6
    Last Post: 2006-06-21, 17:18

Posting Permissions

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