2013-07-11 5 views
8

ここに私のコードです。コンパイルエラーはありませんが、出力が得られません。マップが表示されません。 JPanelでGoogleの静的マップを開き、ローカルドライブに保存したいと考えています。これは私が使用しているコードです。私が間違っているところを親切に案内します。Google Maps in JAVAスイング

try { 
     String imageUrl = 
       "http://maps.google.com/staticmap?center=40,26&zoom=1&size=150x112&maptype=satellite&key=ABQIAAAAgb5KEVTm54vkPcAkU9xOvBR30EG5jFWfUzfYJTWEkWk2p04CHxTGDNV791-cU95kOnweeZ0SsURYSA&format=jpg"; 
     String destinationFile = "image.jpg"; 
     str = destinationFile; 
     URL url = new URL(imageUrl); 
     InputStream is = url.openStream(); 
     OutputStream os = new FileOutputStream(destinationFile); 

     byte[] b = new byte[2048]; 
     int length; 

     while ((length = is.read(b)) != -1) { 
      os.write(b, 0, length); 
     } 

     is.close(); 
     os.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     System.exit(1); 
    } 
    lp2_1.setIcon(new ImageIcon((new ImageIcon("image.jpg")).getImage() 
      .getScaledInstance(630, 600, java.awt.Image.SCALE_SMOOTH))); 

} 
+0

を適合させることができる:Iこれに新しいので、良い文書、編集のための感謝についてはあまり知らない... –

答えて

15

私はこれを試してみて、それが魅力のように働いていた:あなたは、パネル上のマップを取得いけない場合lp2_1背後

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.URL; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class Snippet { 
    public static void main(String[] args) throws IOException { 
     JFrame test = new JFrame("Google Maps"); 

     try { 
      String imageUrl = "http://maps.google.com/staticmap?center=40,26&zoom=1&size=150x112&maptype=satellite&key=ABQIAAAAgb5KEVTm54vkPcAkU9xOvBR30EG5jFWfUzfYJTWEkWk2p04CHxTGDNV791-cU95kOnweeZ0SsURYSA&format=jpg"; 
      String destinationFile = "image.jpg"; 
      String str = destinationFile; 
      URL url = new URL(imageUrl); 
      InputStream is = url.openStream(); 
      OutputStream os = new FileOutputStream(destinationFile); 

      byte[] b = new byte[2048]; 
      int length; 

      while ((length = is.read(b)) != -1) { 
       os.write(b, 0, length); 
      } 

      is.close(); 
      os.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      System.exit(1); 
     } 

     test.add(new JLabel(new ImageIcon((new ImageIcon("image.jpg")).getImage().getScaledInstance(630, 600, 
       java.awt.Image.SCALE_SMOOTH)))); 

     test.setVisible(true); 
     test.pack(); 

    } 
} 

いただきました実際に、このコントロールが問題である可能性があります。

+0

@AbhilashGoyalはあなたを助けましたか? –

+0

私は最近、上記のコードを使用しました。 ImageIconがJFrameに追加された行を理解できませんでした - test.add(新しいJLabel ...)。 ImageIconオブジェクトを作成し、最終的にJLabelに渡される別のImageIconで使用する点は何ですか? JLabel内でImageIconコンストラクタを直接呼び出すとエラーが発生します。 – Sarvavyapi

+0

すぐに使えるコードをありがとう! "String str = destinationFile;"を削除できます。 これは決して使用されません。 –