cuốn sách gpt4 ai đã làm

java - 将 JPanel 保存为图像(不是文件,而是变量)

In lại Tác giả: Walker 123 更新时间:2023-11-29 05:57:32 26 4
mua khóa gpt4 Nike

好的,我的问题很简单:我制作了画图应用程序,我想提供某种“撤消”功能。我读到了撤消管理器等等,但这种方法对我来说似乎很难理解,所以我决定设计自己的方法。我的想法很简单:因为我在 JPanel 上绘图,所以我将在进行任何绘图操作之前保存其当前内容。撤消按钮只会恢复之前的按钮。

问题是:我无法“恢复”保存的图像并将其放在 JPanel 上。我很困惑,因为我已经在硬盘上实现了“保存”当前图像,并且还从硬盘“打开”了任意图像。这两个都工作得很好。

不幸的是,对我的“撤消”系统尝试完全相同的方法不起作用。

看代码:(为了简化,我手动“保存”当前图片 -> 用于测试目的)

public class PictureEdit { //class for saving the current JPanel drawing for undo
public BufferedImage saveBI;
public Graphics2D saveG2D;
}

public void actionPerformed(ActionEvent e) { //action for "UNDO" button, not working
//drawingField - instance of JPanel
//pe - instance of PictureEdit class
drawingField.setBufferedImg(drawingField.pe.saveBI);
drawingField.updateArea(drawingField.getBufferedImg());
}

public void actionPerformed(ActionEvent e) { //action for manual "save" (for undo)
drawingField.pe.saveBI=drawingField.getBufferedImg();
drawingField.pe.saveG2D=(Graphics2D)drawingField.getBufferedImg().getGraphics();

}

现在是我的工作解决方案示例,但与硬盘上的 wiking witk 文件相关:

public void actionPerformed(ActionEvent e){ //Opening file from harddrive - works fine
JFileChooser jfc = new JFileChooser();
int selection = jfc.showOpenDialog(PaintUndo.this);
if (selection == JFileChooser.APPROVE_OPTION){
thử {
drawingField.setBufferedImg(ImageIO.read(jfc.getSelectedFile()));
drawingField.updateArea(drawingField.getBufferedImg());

} catch (IOException ex) {
Logger.getLogger(PaintUndo.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(PaintUndo.this, "Could not open file");
}
}
}


public void actionPerformed(ActionEvent e) { //Saving to harddrive - works fine
int n = JOptionPane.showConfirmDialog(PaintUndo.this, "Are you sure you want to save?", "Saving image...", JOptionPane.OK_CANCEL_OPTION);
if (n == JOptionPane.YES_OPTION){
JFileChooser jfc = new JFileChooser();
int nn = jfc.showSaveDialog(PaintUndo.this);
if (nn == JFileChooser.APPROVE_OPTION){
File saveFile = new File(jfc.getSelectedFile()+".bmp");
thử {
ImageIO.write(drawingField.getBufferedImg(), "bmp", saveFile);
} catch (IOException ex) {
Logger.getLogger(PaintUndo.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(PaintUndo.this, "Error while saving file");
}
}
}
}

以防万一有人好奇:updateArea 函数(代表提到的 drawingField 的扩展 JPanel 类的成员):

public void updateArea(BufferedImage img){ //it is just resizing the current picture (if necessary) and then assigns the new Graphics.
area.height=img.getHeight();
area.width=img.getWidth();
this.setPreferredSize(area);
g2d = (Graphics2D)this.getGraphics();
}

基本上过程是完全一样的,并且可以处理文件,而当我对变量进行操作时我无法保存和恢复图像...可能是哪里出了问题?有任何想法吗?当我使用 Undo/UndoSave 按钮时,我的 JPanel (drawingField) 绝对没有任何变化

感谢任何帮助

câu trả lời hay nhất

在您显示的代码中,您正在复制对图像的引用,但两个引用仍指向同一个图像对象 - 如果您执行任何不重新分配引用的操作(=) 它将反射(reflect)在两个 引用中!为了保存图像的旧版本,您需要复制实际对象,而不仅仅是引用。

我建议使用 Java 已建立的撤消管理方法,但如果您想继续使用自己的方法,则应查看这些方法:

//BufferedImage image;
Raster raster = image.getData(); // save
image.setData(raster); // restore

关于java - 将 JPanel 保存为图像(不是文件,而是变量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11503515/

26 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress