簡體   English   中英

緩沖的圖像不會加載到JPanel對象上

[英]Buffered Image wouldn't load onto the JPanel object

public BufferedImage createImage(JPanel panel) {
//Get top-left coordinate of drawPanel w.r.t screen
Point p = new Point(0, 0);
SwingUtilities.convertPointToScreen(p, panel);

//Get the region with width and height of panel and 
// starting coordinates of p.x and p.y
Rectangle region = panel.getBounds();
region.x = p.x;
region.y = p.y;

//Get screen capture over the area of region
BufferedImage bi = null;
try {
    bi = new Robot().createScreenCapture( region );
} catch (AWTException ex) {
    Logger.getLogger(MyPaintBrush.class.getName()).log(Level.SEVERE, null, ex);
}
return bi; 
}

現在,我希望能夠將圖像重新加載到JPanel drawPanel 以下是我的嘗試,但不起作用:

try {
    BufferedImage img = ImageIO.read(new File("D:\\Work Space\\Java\\Eclipse\\MyPaintBrush\\MyImage.png")); 
    JLabel picLabel = new JLabel(new ImageIcon(img));
    drawPanel.add(picLabel);
} catch (IOException e) {
    e.printStackTrace();
}

請告訴我它是如何完成的。

您需要更改創建url對象的方法。

URL url = getClass().getResource(saveImage);

getResource()方法並非設計用於訪問磁盤上的文件,而是用於jar / war中。 我跳過這一行,直接打開文件,因為它在文件系統上:

 BufferedImage img = ImageIO.read(new File("/path/to/file/name.png"));

切記為文件系統使用正確的路徑格式。

我花了整整一天的時間弄清楚(這里需要很多幫助)如何將JPanel的緩沖圖像保存到硬盤上。 我使用以下代碼來做到這一點:

我更喜歡使用Screen Image類。 它打包為可重用的類。 同樣,JComponent()的paint()方法比使用機器人更快。

現在,我希望能夠將圖像重新加載到JPanel drawPanel上。

創建文件時,您並未指定所有目錄信息,所以為什么要在嘗試讀取文件時對目錄路徑進行硬編碼。 只需使用相同的文件名即可讀取用於寫入文件的字段。

這是使用ScreenImage類的示例,該示例寫入圖像並立即讀取圖像:

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

                    imageLabel.setIcon( new ImageIcon(ImageIO.read( new File(imageName) ) ) );
                }
                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();
            }
        });
    }
}

請注意,此版本僅更改標簽的圖標。 如果要在可見的GUI上創建新的JLabel,則還需要在面板上調用revalidate(),以便標簽具有要繪制的適當大小。

暫無
暫無

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

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