我的java程序根据纬度和经度从谷歌下载图像。图像保存到我的桌面。然后我调用一个新框架打开并查看图像。这是错误。当我尝试下载一个图像并查看它时它工作正常,但是当我尝试下载另一个图像时,它会覆盖上一个图像,框架会显示上一个图像而不是新图像。
BufferedImage image = null;
try {
URL url = new URL("https://maps.googleapis.com/maps/api/staticmap?center="+x+","+y+"&zoom=14&size=650x600&maptype=hybrid&markers=color:blue%7Clabel:X%7C"+ll.get(1)+","+ll.get(2)+"&sensor=true");
image = ImageIO.read(url);
ImageIO.write(image, "png",new File("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\locationpic.png"));
new classes.viewPic(); // calls pic viewer
}catch(Exception e){print("Could not download image...",Default);}
图片浏览器
public class viewPic extends JFrame {
public static void main(String [] args) {
new viewPic();
}
public viewPic() {
this.setTitle("Picture Viewer");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel1 = new JPanel();
ImageIcon pic = new ImageIcon("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\locationpic.png");
panel1.add(new JLabel(pic));
this.add(panel1);
this.pack();
this.setVisible(true);
}
}
由于我打开了另一个框架,一个“父”框架,将默认关闭操作设置为关闭时退出将关闭所有框架。
我该怎么办?
答案 0 :(得分:1)
您需要强制从磁盘重新加载图像。这可以通过以下方式完成:
ImageIO
阅读图片。Image.flush()
方法。如下所示:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import java.net.*;
public class ImageReload extends JPanel implements ActionListener
{
JLabel timeLabel;
JLabel imageLabel;
ImageIcon icon = new ImageIcon("timeLabel.jpg");
public ImageReload()
{
setLayout( new BorderLayout() );
timeLabel = new JLabel( new Date().toString() );
imageLabel = new JLabel( timeLabel.getText() );
add(timeLabel, BorderLayout.NORTH);
add(imageLabel, BorderLayout.SOUTH);
javax.swing.Timer timer = new javax.swing.Timer(1000, this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
timeLabel.setText( new Date().toString() );
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
String imageName = "timeLabel.jpg";
BufferedImage image = ScreenImage.createImage(timeLabel);
ScreenImage.writeImage(image, imageName);
// This works using ImageIO
// imageLabel.setIcon( new ImageIcon(ImageIO.read( new File(imageName) ) ) );
// Or you can flush the image
ImageIcon icon = new ImageIcon(imageName);
icon.getImage().flush();
imageLabel.setIcon( icon );
}
catch(Exception e)
{
System.out.println( e );
}
}
});
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new ImageReload() );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
注意:
此示例使用Screen Image类在时间更改时动态创建标签图像。
答案 1 :(得分:0)
我按照建议做了,我将图像直接传递给图像查看器,而不将其保存到我的驱动器中。这是代码。
BufferedImage image = null;
try {
URL url = new URL("https://maps.googleapis.com/maps/api/staticmap?center="+x+","+y+"&zoom=14&size=650x600&maptype=hybrid&markers=color:blue%7Clabel:X%7C"+x+","+y+"&sensor=true");
image = ImageIO.read(url);
//ImageIO.write(image, "png",new File("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\locationpic.png"));
new classes.viewPic(image);
}catch(Exception e){print("Could not download image...",Default);}
现在是图片查看器
public viewPic(BufferedImage image) {
this.setTitle("Picture Viewer");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel1 = new JPanel();
// ImageIcon pic = new ImageIcon("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\locationpic.png");
ImageIcon pic = new ImageIcon(image);
panel1.add(new JLabel(pic));
this.add(panel1);
this.pack();
this.setVisible(true);
}
我注释掉了将图像下载到桌面的行,而是将图像直接传递到了框架中。我还删除了' void main'在图片浏览器代码中,因为它不需要出于任何原因。
再次感谢所有回复的人:) :) :)谢谢