mapexplorer.java

来自「java调用ie浏览器demo源码,可以用在windows或者linux」· Java 代码 · 共 924 行 · 第 1/3 页

JAVA
924
字号
    
    org.jdic.web.BrMapOver brMapBack;
    
    class MapClipper extends BrMap  implements Printable {
        public Rectangle rcArea;

        public MapClipper(){
            super();
        }
        public MapClipper(
            int _iMapIndex,
            double _viewCenterLatitude,
            double _viewCenterLongitude,
            int _viewZoomLevel,
            int _viewType)
        {
            super(
                _iMapIndex,
                _viewCenterLatitude,
                _viewCenterLongitude,
                _viewZoomLevel,
                _viewType);
        }
        public void addBoundAsSprite()
        {
            if(null!=rcArea){
                BrMapSprite pg = new BrMapSprite();
                pg.createFromPoints(this, new Point[] {
                    new Point(rcArea.x, rcArea.y),
                    new Point(rcArea.x + rcArea.width, rcArea.y),
                    new Point(rcArea.x + rcArea.width, rcArea.y + rcArea.height),
                    new Point(rcArea.x, rcArea.y + rcArea.height)
                });
               getSprites().add(pg);
            }
        }
        public void paintGrid(Graphics g) {
            if(isMapReady() && cbGrid.isSelected()){
                Graphics2D g2 = (Graphics2D)g;
                try{
                    double gridStep = Double.parseDouble(edScale.getText());

                    String result = execJS( "_fromPointToLatLng("
                        + (rcArea.x + rcArea.width/2) + ","
                        + (rcArea.y + rcArea.height/2) + ")" );
                    double m2p = getMeter2pixel( Double.parseDouble( result.split(",")[0]) );

                    double step = gridStep/m2p;
                    if( (step >= 3.0) && ((rcArea.height/step)<1000.0) ){
                        //the number of point in grid is big enouth
                        double iX0 = rcArea.x;
                        double iY0 = rcArea.y;
                        if(true){
                            //grid lines
                            final Color colorsR[] = new Color[] {
                                new Color(1.0F, 0.0F, 0.0F, 1.0F),
                                new Color(0.0F, 1.0F, 0.0F, 1.0F),
                                new Color(0.0F, 0.0F, 1.0F, 1.0F),
                                new Color(1.0F, 1.0F, 0.0F, 1.0F),
                                new Color(0.0F, 1.0F, 1.0F, 1.0F),

                                new Color(0.5F, 0.0F, 0.0F, 1.0F),
                                new Color(0.0F, 0.5F, 0.0F, 1.0F),
                                new Color(0.0F, 0.0F, 0.5F, 1.0F),
                                new Color(0.5F, 0.5F, 0.0F, 1.0F),
                                new Color(0.0F, 0.5F, 0.5F, 1.0F)
                            };

                            int iC = 0;
                            for(double iX = iX0; iX<(rcArea.x + rcArea.width); iX += step){
                                g2.setColor( colorsR[ iC % colorsR.length ] );
                                g2.drawLine((int)iX, rcArea.y, (int)iX, rcArea.y + rcArea.height - 1);
                                ++iC;
                            }
                            iC = 0;
                            for(double iY = iY0; iY<(rcArea.y + rcArea.height); iY += step){
                                g2.setColor( colorsR[ iC % colorsR.length ] );
                                g2.drawLine(rcArea.x, (int)iY, rcArea.x + rcArea.width - 1, (int)iY);
                                ++iC;
                            }
                        } else {
                            final Color colorsR[] = new Color[] {
                                new Color(1.0F, 0.0F, 0.0F, 0.15F),
                                new Color(0.0F, 1.0F, 0.0F, 0.15F),
                                new Color(0.0F, 0.0F, 1.0F, 0.15F)
                            };

                            final Color colorsC[] = new Color[] {
                                new Color(0.5F, 0.5F, 0.0F, 0.15F),
                                new Color(0.0F, 0.5F, 0.5F, 0.15F),
                                new Color(0.5F, 0.0F, 0.5F, 0.15F)
                            };

                            boolean bStartLine = false;
                            int iRow = 0;
                            for(double iX = iX0; iX<(rcArea.x + rcArea.width); iX += step){
                                ++iRow;
                                int iCol = 0;
                                bStartLine = !bStartLine;
                                boolean bDraw = bStartLine;
                                for(double iY = iY0; iY<(rcArea.y + rcArea.height); iY += step){
                                   ++iCol;
                                   g2.setColor( bDraw
                                     ? colorsR[ iRow % colorsR.length ]
                                     : colorsC[ iCol % colorsC.length ]);
                                   Rectangle rc = new Rectangle(
                                           (int)iX, (int)iY,
                                           (int)Math.min( step - 0.5, rcArea.x + rcArea.width - iX),
                                           (int)Math.min( step - 0.5, rcArea.y + rcArea.height - iY) );
                                   g2.fill(rc);
                                   bDraw = !bDraw;
                                }
                            }
                        }
                    }
                }catch(Exception e){}
            }
        }

        @Override
        public void paintContent(Graphics g) {
            super.paintContent(g);
            if(null!=rcArea){
                paintGrid(g);
                Graphics2D g2 = (Graphics2D)g;
                g2.setColor(Color.BLACK);
                g2.drawRect(
                    rcArea.x, rcArea.y,
                    rcArea.width-1, rcArea.height-1
                );
            }
        }
        
        public boolean bAutoArea = true;

        @Override
        public void onSelectionRectChanged(Rectangle rc, boolean bShow)
        {
            if(cbSelectWorkingArea.isSelected()){
                ((MapClipper)brMap).rcArea = rc;
                if(!bShow){
                    SwingUtilities.invokeLater(new Runnable(){ public void run() {                    
                        bAutoArea = false;                    
                        btResetSelection.setEnabled(true);
                        if(null!=brMapBack){
                            brMapBack.setBounds(((MapClipper)brMap).rcArea);
                        }
                        repaint();                                        
                    }});
                } else {
                    repaint();
                }        
            }
        }
        public void saveMap(String stFN)
        {
            System.out.println("save to " + stFN);
            if(null!=rcArea){
                Image im = new BufferedImage(
                        rcArea.width, 
                        rcArea.height, 
                        BufferedImage.TYPE_INT_BGR);
                if(null!=im){
                    Graphics g = im.getGraphics();
                    try{
                       g.translate(-rcArea.x, -rcArea.y); 
                       paintClientArea(g, true);
                    } finally {
                       g.dispose(); 
                    }
                    try{
                        stFN = stFN.toLowerCase();
                        if(!stFN.endsWith(".bmp")){
                           stFN += ".bmp";
                        }
                        File fn = new File(stFN);
                        ImageIO.write((RenderedImage)im, "BMP", fn);

                        int read = 1024;
                        InputStreamReader rd = new InputStreamReader(
                                getClass().getResourceAsStream("temp_ozf.map"));
                        char[] buf = new char[read];
                        StringBuffer sb = new StringBuffer(read);
                        while( -1 != (read = rd.read(buf, 0, read)) ){
                           sb.append(buf, 0, read);
                        }
                        rd.close();
                        String stFormat = sb.toString();


                        String result = execJS("_fromPointToLatLng("
                            + rcArea.x + "," + rcArea.y + ","
                            + (rcArea.x + rcArea.width) + "," + rcArea.y + ","
                            + (rcArea.x + rcArea.width) + "," + (rcArea.y + rcArea.height) + ","
                            + rcArea.x + "," + (rcArea.y + rcArea.height)+ ");"
                        );
                        String args[] = result.split(",");
                        for(int i=0; i<4; ++i){
                            double d = Double.parseDouble(args[i*2]);
                            String toRepl = String.format("$(4lt%d)", i+1);
                            stFormat = stFormat.replace(toRepl, "" + (int)Math.floor(d));

                            toRepl = String.format("$(mlat%d)", i+1);
                            String that = String.format("%2.6f", ((d - Math.floor(d))*60.0));
                            stFormat = stFormat.replace(toRepl, that );

                            d = Double.parseDouble(args[i*2+1]);
                            toRepl = String.format("$(4ln%d)", i+1);
                            stFormat = stFormat.replace(toRepl, "" + (int)Math.floor(d));

                            toRepl = String.format("$(mlng%d)", i+1);
                            that = String.format("%2.6f", ((d - Math.floor(d))*60.0));
                            stFormat = stFormat.replace(toRepl, that );
                        }

                        stFormat = stFormat.replace("$(8lat1)", args[0]);
                        stFormat = stFormat.replace("$(8lng1)", args[1]);
                        stFormat = stFormat.replace("$(8lat2)", args[2]);
                        stFormat = stFormat.replace("$(8lng2)", args[3]);
                        stFormat = stFormat.replace("$(8lat3)", args[4]);
                        stFormat = stFormat.replace("$(8lng3)", args[5]);
                        stFormat = stFormat.replace("$(8lat4)", args[6]);
                        stFormat = stFormat.replace("$(8lng4)", args[7]);

                        stFormat = stFormat.replace("$(scale)", "not impl yet");//scale in metters/pixel

                        String w5 = String.format("%5d", rcArea.width);
                        stFormat = stFormat.replace("$(5w)", w5);
                        String h5 = String.format("%5d", rcArea.height);
                        stFormat = stFormat.replace("$(5h)", h5);
                        stFormat = stFormat.replace("$(w)", ""+ rcArea.width);
                        stFormat = stFormat.replace("$(h)", ""+ rcArea.height);

                        stFormat = stFormat.replace("$(BMP)", fn.getName());

                        int iPos = stFN.lastIndexOf('.');
                        String stBMP = stFN.substring(0, iPos);
                        String stOZF2 = stBMP;
                        stBMP += "_ozf.map";
                        stOZF2 += ".ozf2";

                        stFormat = stFormat.replace("$(OZF2)", stOZF2);

                        FileWriter wt = new FileWriter(stBMP);

                        wt.write(stFormat);
                        wt.close();
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }
                }
            }
        }

        @Override
        public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
            if(pageIndex == 0 && null!=rcArea) {
                    Graphics2D g2d = (Graphics2D)g;
                    g2d.translate(
                       pf.getImageableX() - rcArea.x, 
                       pf.getImageableY() - rcArea.y);
                    g2d.scale(
                            pf.getImageableWidth()/rcArea.width, 
                            pf.getImageableHeight()/rcArea.height);
                    paintClientArea(g2d, true);
                    return Printable.PAGE_EXISTS;
            }
            return Printable.NO_SUCH_PAGE;
        }

        @Override
        public void reshape(int x, int y, int width, int height)
        {
            super.reshape(x, y, width, height);
            if( bAutoArea ){
                rcArea = new Rectangle(68, 32, width - 68 - 2, height - 32 - 28);
            }
            if(null!=brMapBack){
                brMapBack.setBounds(rcArea);
            }    
        }

        final Color colorSuccess = new Color(128, 255, 128);
        final Color colorFail = new Color(255, 128, 128);
        @Override
        public void onFoundAddress(final String[] args) {
            SwingUtilities.invokeLater(new Runnable(){ public void run() {
                edAddress.setBackground(
                   ( 4 == args.length)
                   ? colorSuccess
                   : colorFail);
                if(4 == args.length){
                    setZoomLevel(Math.max(14, getZoomLevel()));
                    setViewCenter(args[2] + "," + args[3]);
                    BrMapBalloonSprite bl = new BrMapBalloonSprite(
                        "<html><b>Location:</b><br><b style=\"color:green\">" + edAddress.getText() + "</b></html>",
                        getPoint(getViewCenter())
                    );
                    bl.add(brMap);
                }        
            }});//end posponed operation
        }
        
    }
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?