Results 1 to 3 of 3
  1. #1
    a_klasanov is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Post Visualizing an image on Canvas

    I have to establish a http connection with "google.maps" and after that download a map with a specified coordinates, zoom and size.Then I convert it into an immage and visualize it on a Canvas. I have wrote down the following code, and it seems to connect to "google.maps" but on the display there is nothing. I have made 2 classes , I don't know whether this is correct. There are two methods that I do not use as well but it doesn't matter. So please help me.

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.io.*;
    import java.io.*;

    /**
    *
    */
    public class MidletMappa extends MIDlet implements Runnable{
    byte[] imageData=null;
    static Image mappa;
    static int zoom=10;
    static double lat=43.1;
    static double lon=12.0;
    static String size="512x512";
    static String url="I CANNOT USE LINKS IN THIS FORUM/staticmap?center="+lat+","+lon+"&zoom="+zoom+"&siz e="+size+"&key=ABQIAAAAdtc9p-tYjhOn-cBjT4ZqOhQFw05-dUaae2f4nTjx_OHejRkIjhTD-H8rX2tIVL6gWFMwoXqZwpfMAA";

    private Display mdisplay;

    public void startApp() throws MIDletStateChangeException {
    System.out.println(">>Start midlet");
    mdisplay=Display.getDisplay(this);
    Thread t=new Thread(this);
    t.start();
    CanvasMappa.setImage(mappa);
    mdisplay.setCurrent(new CanvasMappa());
    }//end start

    public void pauseApp() {
    System.out.println("Pause midlet");
    }//end pause

    public void destroyApp(boolean unconditional) {
    System.out.println("Destroy midlet");
    notifyDestroyed();
    }//end destroy

    public void run(){
    DataInputStream in=null;
    HttpConnection connection=null;
    try{
    connection=(HttpConnection)Connector.open(url);//creo una connessione http
    //connection.setRequestMethod(HttpConnection.GET);
    if(connection.getResponseCode()==HttpConnection.HT TP_OK){// connessione ok
    int length=(int)connection.getLength();

    if (length!=-1){
    imageData=new byte[length];
    in=new DataInputStream(connection.openDataInputStream());
    in.readFully(imageData);

    }
    else{
    int chunkSize=512;
    int index=0;
    int readLength=0;
    in=new DataInputStream(connection.openInputStream());
    imageData=new byte[chunkSize];
    do{
    if(imageData.length<index+chunkSize){
    byte[] newData=new byte[index+chunkSize];
    System.arraycopy(imageData, 0, newData, 0, imageData.length);
    imageData=newData;
    }//end if
    readLength=in.read(imageData,index,chunkSize);
    index+=readLength;
    }while(readLength==chunkSize); //end do-while
    length=index;


    }
    mappa=Image.createImage(imageData, 0, length);//eventualmente si puo' usare un altro metodi create image

    }//end connessione ok

    else{//connessione no ok
    Alert a=new Alert("Connessione non riuscita");
    a.setTimeout(3000);
    mdisplay.setCurrent(a);
    }//end connessione no OK


    }//end try
    catch(Exception e){
    e.printStackTrace();
    Alert a=new Alert(e.getMessage());
    mdisplay.setCurrent(a);
    }//end catch


    }//end run

    public static void setParameters(int zo, double lattitudine, double longitudine, String si, String indirizzo ){
    zoom=zo;
    lat=lattitudine;
    lon=longitudine;
    size=si;
    url=indirizzo;

    }//end setParameters


    }//end class









    public class CanvasMappa extends Canvas {

    static Image mappa;
    static int zoom=10;
    static double lat=43.1;
    static double lon=12.0;
    static String size="512x512";
    static String url=I CANNOT USE THE LINK IN THIS FORUM staticmap?center="+lat+","+lon+"&zoom="+zoom+"&siz e="+size+"&key=ABQIAAAAdtc9p-tYjhOn-cBjT4ZqOhQFw05-dUaae2f4nTjx_OHejRkIjhTD-H8rX2tIVL6gWFMwoXqZwpfMAA";


    public static void setImage(Image im){
    mappa=im;

    }//end setImage

    protected void paint(Graphics g) {
    g.drawImage(mappa, getWidth(), getHeight(), Graphics.HCENTER|Graphics.VCENTER);

    }//end paint




    public static void setParameters(int zo, double lattitudine, double longitudine, String si, String indirizzo ){
    zoom=zo;
    lat=lattitudine;
    lon=longitudine;
    size=si;
    url=indirizzo;

    }//end setParameters





    }//end class

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    t seems to connect to "google.maps"
    What does google.maps return?
    I don't see a main() method to execute the above. How is it started?

  3. #3
    a_klasanov is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    Let me give U a piece of advice. Take the book "THE COMPLETE IDIOT'S GUIDE JAVA 2". There's the method startApp() that starts the application. By the way, yesterday I solved the problem. Thank U even though U know less than me and my outstanding instructor Piercosimo.

Similar Threads

  1. Using a Canvas
    By a_klasanov in forum CLDC and MIDP
    Replies: 3
    Last Post: 12-08-2008, 10:36 AM
  2. Canvas Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:22 PM
  3. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  4. Using SWT Canvas
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:06 AM

Posting Permissions

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