在搜索类似问题后,我想问下列问题:
我正在尝试从Google静态地图加载图片,但我收到此错误:Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at MapPicturePanel.main(MapPicturePanel.java:18)
以下是此课程的完整代码:
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MapPicturePanel {
private JScrollPane getContent(BufferedImage image) {
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
label.setHorizontalAlignment(JLabel.CENTER);
return new JScrollPane(label);
}
public static void main(String[] args) throws IOException {
String path = "http://maps.googleapis.com/maps/api/staticmap?center=37.426616,-122.176380&zoom=14&size=800x600&scale=2&format=jpg&maptype=hybrid&sensor=false";
URL url = MapPicturePanel.class.getResource(path);
BufferedImage image = ImageIO.read(url);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new MapPicturePanel().getContent(image));
f.setSize(800,800);
f.setLocation(200,200);
f.setVisible(true);
}
}
答案 0 :(得分:1)
您正试图在类路径中找到资源(这是Class.getResource所做的)。但看起来你想从外部网址加载图片。
就这样做
BufferedImage image - ImageIO.read(new URL(path));