簡體   English   中英

相框顯示相同的圖片

[英]Frame displays the same picture

我的Java程序根據緯度和經度從Google下載圖像。 圖像保存到我的桌面。 然后,我調用一個新框架以打開並查看圖像。 這是錯誤。 當我嘗試下載一個圖像並查看它時,它工作正常,但是當我嘗試下載另一個圖像時,它覆蓋了前一個圖像,該框架顯示了前一個圖像,而不是新的圖像。

                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);
    }
}

由於我還有另一個框架處於打開狀態,因此將“父”框架設置為在關閉時退出的默認關閉操作會關閉所有框架。

我能做什么?

您需要強制從磁盤重新加載映像。 這可以通過以下方式完成:

  1. 使用ImageIO讀取圖像。
  2. 使用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類隨着時間的變化動態創建標簽的圖像。

我按照建議進行了操作,沒​​有將圖像保存到驅動器中,而是將圖像直接傳遞到了圖像查看器。 這是代碼。

                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”,因為由於任何原因它都不需要在那里。

再次感謝所有答復的人:) :) :)謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM