簡體   English   中英

從URL繪制圖像

[英]Drawing an image from URL

它用於吃豆子游戲,當我從計算機導入它時,它可以完美運行。 但是,當我嘗試從URL中獲取它時,我的游戲開始滯后,並且圖像不顯示。

URL url = new URL("https://www.dropbox.com/s/xpc49t8xpqt8dir/pacman%20down.jpg");
image = ImageIO.read(url);  

G.drawImage(image, x, y, 20,20,null);

圖片

吃豆人

(IMGUR的http://i.stack.imgur.com/Cp1XL.png

https://www.dropbox.com/s/xpc49t8xpqt8dir/pacman%20down.jpg返回HTML文本而不是圖像數據。

這是一種黑客行為,但請嘗試https://www.dropbox.com/s/xpc49t8xpqt8dir/pacman%20down.jpg?dl=1 但是請注意,將來放置框可能會更改此查詢。

在此處輸入圖片說明

public class TestURL02 {

    public static void main(String[] args) {
        new TestURL02();
    }

    public TestURL02() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PacPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PacPane extends JPanel {

        private BufferedImage image;

        public PacPane() {
            InputStream is = null;
            try {
                URL url = new URL("https://www.dropbox.com/s/xpc49t8xpqt8dir/pacman%20down.jpg?dl=1");
//                StringBuilder sb = new StringBuilder(1024);
//                byte[] buffer = new byte[1024 * 1024];
//                is = url.openStream();
//                int in = -1;
//                while ((in = is.read(buffer)) != -1) {
//                    sb.append(new String(buffer));
//                }
//                System.out.println(sb.toString());
                image = ImageIO.read(url);
            } catch (IOException exp) {
                exp.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (Exception e) {
                }
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return image == null ? super.getPreferredSize() : new Dimension(image.getWidth(), image.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (image != null) {
                int x = (getWidth() - image.getWidth()) / 2;
                int y = (getHeight() - image.getHeight()) / 2;
                g.drawImage(image, x, y, this);
            }
        }

    }
}

我認為這可能會滯后,因為每次使用Graphics對象繪制程序時,程序都會下載圖像。 您應該為圖像使用緩存系統,或者為所有程序執行一次下載。

我的猜測,這只是一個猜測,因為您沒有告訴我們,但是您是否可以嘗試通過Swing或AWT paint(...)paintComponent(...)方法從URL中讀取圖像? 如果是這樣,請不要這樣做。 一次讀取圖像,然后在paintComponent(...)方法中使用它。

如果這樣做沒有幫助,請務必告訴我們詳細信息,以便能夠為您提供幫助。

暫無
暫無

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

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