×

Discussion Board

Results 1 to 2 of 2

Thread: mobile map

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    7

    Post mobile map

    hi all;
    I wrote this program after reading this link

    I'm developing program for mobile to load and scroll maps.
    I want to know your opinion about this code.
    breif:
    main map divided to small tiles by photoshop
    for show only some tiles


    excuse me for poor english.
    Code:
     package imagePack;
    import java.io.IOException;
    import javax.microedition.lcdui.Canvas; 
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.Image;
    
    public class ImageCanvas extends Canvas
    {
        private int SCRN_WDTH=this.getWidth();
        private int SCRN_HGHT=this.getHeight();
        
        private int TILE_WDTH=115;
        private int TILE_HGHT=130;
        
        /*
         * width of tile is near 115*4 => 115*4= bffrWdth=460;
         * hieght of tile is near 130=>130*4= bffrHght=520;
         */
        private int cntRowTile;
        private int cntColTile;
        
        private int BFFR_WDTH=460;
        private int BFFR_HGHT=520;
        
        private int bffrTop;
        private int bffrLeft;
        
        private int CURRENT_ROW,CURRENT_COL;
        private String [][] tilesName=new String[10][8]; 
        public ImageCanvas()
        {
            try
            {
                this.CURRENT_ROW=4;//srart row  
                this.CURRENT_COL=4;//start col
                
                this.bffrTop=-30;//buffer top 8
                this.bffrLeft=-30;//buffer left
                initialize();
                this.calculateBffrImg();
            } catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
        
        public void initialize()
        {
            String imgNm;
            int cntRow;
            int cntCol;
            //tilesName=new String[10][8];
            for( cntRow=1 ; cntRow<= 10 ;cntRow++)
            {
                for( cntCol=1 ; cntCol<= 8;cntCol++ )
                {
                   imgNm="yazd-" +cntRow+ "("+ cntCol +")" ;
                   tilesName[cntRow-1][cntCol-1]=imgNm;
                }
            }
         //......calculate count of tiles........ ...\\
         cntColTile= SCRN_WDTH/TILE_WDTH +2;
         cntRowTile= SCRN_HGHT/TILE_HGHT+2;
        
         BFFR_WDTH=cntColTile*TILE_WDTH ;
         BFFR_HGHT=cntRowTile*TILE_HGHT;
        }
        public void calculateBffrImg()
        {        
    //-----------move between -----------\\
            //if(bffrTop!=0)
            {
                if(bffrTop>=0) 
                {
                    bffrTop-=TILE_HGHT;
                    CURRENT_ROW-=1;
    
                }
                else if(bffrLeft>=0) 
                {
                    bffrLeft-=TILE_WDTH;
                    CURRENT_COL-=1;
                }
                else if(Math.abs(bffrTop)+SCRN_HGHT+20>= BFFR_HGHT)
                {
                    bffrTop+=TILE_HGHT;
                    CURRENT_ROW+=1;
                }
                else if(Math.abs(bffrLeft)+SCRN_WDTH+20>= BFFR_WDTH)
                {
                    bffrLeft+=TILE_WDTH;
                    CURRENT_COL+=1;
                }
                this.repaint();
           }
        }
        protected void paint(Graphics g)
        {
            int cntRow;
            int cntCol;
            int tilesX=bffrLeft;
            int tilesY=bffrTop;
            String address;
            Image img;
            
            try
            {
                if (CURRENT_ROW<=0)
                {
                    CURRENT_ROW=0;
                    bffrTop-=TILE_HGHT;
                }
                else if (CURRENT_COL<=0) 
                {
                    CURRENT_COL=1;
                }
                else
                {
                    g.setColor(0, 0, 0);
                    g.fillRect(0, 0, this.getWidth(), this.getHeight());
                    for(cntRow=CURRENT_ROW ; cntRow<CURRENT_ROW+ cntRowTile; cntRow++)
                    {
                        for(cntCol=CURRENT_COL ; cntCol<CURRENT_COL+cntColTile ; cntCol++)
                        {
                            address="/"+tilesName[cntRow][cntCol]+".png";
                            img=Image.createImage(address);
                            g.drawImage(img,tilesX,tilesY,0);
                            tilesX+=TILE_WDTH;
                        } 
                     tilesX=bffrLeft; tilesY+=TILE_HGHT;
                    }
                }
           } 
           catch (IOException ex)
           {
               ex.printStackTrace();
           }
        }
    
        protected void keyPressed(int keyCode)
        {
            int gameAction = this.getGameAction(keyCode);
            int MOVE=10;
            if (keyCode== Canvas.KEY_NUM2 || gameAction==Canvas.UP)
            {
                      bffrTop+= MOVE;
            } else if (keyCode == Canvas.KEY_NUM8 || gameAction==Canvas.DOWN)
            {
                bffrTop-= MOVE;
            } else if (keyCode == Canvas.KEY_NUM6 || gameAction==Canvas.RIGHT)
            {
                bffrLeft -= MOVE;
            } else if (keyCode == Canvas.KEY_NUM4 ||gameAction==Canvas.LEFT)
            {
                bffrLeft+= MOVE;
            }
            calculateBffrImg();
        }
        protected void keyRepeated(int keyCode)
        {
           keyPressed(keyCode);
        }
    }
    Last edited by moabiran; 2011-06-30 at 17:20.

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,085

    Re: mobile map

    Are you facing any specific problem in this ?
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

Similar Threads

  1. Mobile Map API on J2ME platform
    By GUIDEBEE in forum Mobile Java General
    Replies: 2
    Last Post: 2010-07-01, 09:08
  2. my own map service in mobile
    By ravi2vit in forum Mobile Java General
    Replies: 7
    Last Post: 2009-08-10, 05:59
  3. Building Offline Map Application in Mobile phones
    By amishra in forum Mobile Java Networking & Messaging & Security
    Replies: 23
    Last Post: 2009-03-17, 08:56
  4. Replies: 2
    Last Post: 2009-02-13, 03:19

Posting Permissions

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