Java 缓冲图像处理';t加载到JPanel对象上
现在,我希望能够将图像加载回JPanelJava 缓冲图像处理';t加载到JPanel对象上,java,swing,Java,Swing,现在,我希望能够将图像加载回JPaneldrawPanel。以下是我的尝试,但无效: 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
drawPanel
。以下是我的尝试,但无效:
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;
}
请告诉我是如何完成的。您需要更改创建url对象的方法
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();
}
getResource()
方法不是为访问磁盘上的文件而设计的,而是在jar/war中设计的。我会跳过这一行,直接打开一个文件,因为它位于文件系统中:
URL url = getClass().getResource(saveImage);
请记住为您的文件系统使用正确的路径格式
我花了一整天的时间才弄明白(这里有很多需要的帮助)如何将JPanel的BuffereImage保存到我的硬盘上。我使用了以下代码来执行此操作:
我更喜欢上课。它被打包为一个可重用类。此外,JComponent()的paint()方法比使用机器人更快
现在,我希望能够将图像加载回JPanel drawPanel
创建文件时没有指定所有目录信息,因此在尝试读取文件时为什么要硬编码目录路径。只需使用相同的文件名来读取用于写入文件的字段
下面是一个使用ScreenImage类编写图像并立即读取图像的示例:
BufferedImage img = ImageIO.read(new File("/path/to/file/name.png"));
注意:此版本仅更改标签的图标。如果要在可见GUI上创建新的JLabel,则还需要在面板上调用revalidate(),以便标签具有要绘制的适当大小。Class.getResource不是读取文件的常规方法;它用于读取嵌入在应用程序的.jar文件中的数据。您可能想将
文件
传递给ImageIO.read方法,而不是URL。哦……我刚从这里的一个问题中选择了它。您能展示一下您的版本吗?我用以下内容替换了URL部分:BufferedImage img=ImageIO.read(新文件(“D:/workspace/Java/Eclipse/MyImage.png”)代码>但仍然不起作用:(文件名不应以.png
结尾-看起来您传递了MyPaintBrush
的路径,而不是MyPaintBrush.png
。顺便说一句,对我来说,MyPaintBrush
似乎是一个目录(您的项目名称),不是一个文件我的坏。我马上编辑了它,它仍然无法工作。我键入的文件路径格式正确吗?不,对于windows,它应该是D:\\folder\\etc,将evey/
更改为“\\”。磁盘上的图像是否写入此路径?你能用默认图像查看器打开它吗?我如何将其应用于上面发布的代码的我的版本?请取消明白所有这些对我来说都是全新的,我内心有点害怕。我花了很多时间在我的代码上。我只是想告诉我如何将这种方法应用到我的代码中。你的代码不完整。这是一些随机的代码行。我不知道你的代码使用的上下文。我给了你一个完整的代码来下载您需要花几分钟的时间查看代码,然后实现“概念”将代码导入程序。创建图像需要3行代码,读取图像需要一行代码。即使不使用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();
}
});
}
}