gpt4 book ai didi

java - Swing:保存当前图形

转载 作者:行者123 更新时间:2023-12-02 09:42:44 26 4
gpt4 key购买 nike

我有创建一些图形的 SWing 代码。我有一个按钮“再次运行”,可以重新创建图形。

SwingUtilities.invokeLater(new Runnable() {

public void run() {

final GraphicsDemo_noborders_ML demo = new GraphicsDemo_noborders_ML();

final JFrame jFrame = new JFrame();

jFrame.setTitle("Latest Programic Art");
jFrame.setSize(canvasW, canvasH);

JButton save = new JButton("Save");
JButton run = new JButton("Run Again");

save.setBounds(50, 100, 100, 50);
run.setBounds(50, 500, 100, 50);

final JPanel panel = new JPanel();

panel.add(save);
panel.add(run);

final JComponent canvas = new JComponent() {

@Override
protected void paintComponent(Graphics g) {
demo.paint(g);
}
};

jFrame.getContentPane().add(canvas, BorderLayout.CENTER);
jFrame.getContentPane().add(panel, BorderLayout.PAGE_START);


run.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Graphics g = canvas.getGraphics();
demo.paint(g);
}
});

save.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.showSaveDialog(null);

String location = chooser.getSelectedFile().toString();
demo.saveImageToLocalDisk(jFrame, location);
}
});

jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
});

保存按钮用于将当前图形保存为图像。但是,当我保存时,它不会保存当前图形,而是仅保存初始图形。

下面提供了保存的方法,

private void saveImageToLocalDisk(JFrame jFrame, String location) {

BufferedImage image = null;

try {

Rectangle rectangle = jFrame.getBounds();
image = new Robot().createScreenCapture(rectangle);

} catch (AWTException e1) {
e1.printStackTrace();
}

Graphics2D graphics2D = image.createGraphics();
jFrame.paint(graphics2D);

try {
ImageIO.write(image, "jpeg", new File(location + "/Graphics.jpeg"));
} catch (Exception e) {
System.out.println("error");
}
}

我该如何纠正这个问题?

最佳答案

这是一个小例子,我将如何实现您的应用程序(重新创建+保存图像)。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

/**
* <code>PaintDemo</code>.
*/
public class PaintDemo {

private int recreateIndex = 1;

private BufferedImage image;

private final JLabel imageLabel = new JLabel();

public static void main(String[] args) {
SwingUtilities.invokeLater(new PaintDemo()::startUp);
}

private void startUp() {
recreateImage();
JFrame frm = new JFrame("Paint app");
frm.add(imageLabel);
JPanel buttonBar = new JPanel();
JButton recreate = new JButton("Recreate");
recreate.addActionListener(e -> recreateImage());
buttonBar.add(recreate);
JButton save = new JButton("Save");
save.addActionListener(e -> save());
buttonBar.add(save);
frm.add(buttonBar, BorderLayout.NORTH);
frm.pack();
frm.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}

private void recreateImage() {
image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.BLUE);
g.fillRect(20, 30, 100, 50);
g.setColor(Color.ORANGE);
g.drawOval(200, 150, 50, 50);
g.setColor(Color.GREEN);
g.drawString("Recreated image: " + recreateIndex++, 200, 250);
imageLabel.setIcon(new ImageIcon(image));
}

private void save() {
JFileChooser chooser = new JFileChooser();
int result = chooser.showSaveDialog(imageLabel);
if (result == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
try {
ImageIO.write(image, "jpeg", f);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

关于java - Swing:保存当前图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56928784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com