我有创建一些图形的SWing代码。我有一个按钮"Run Again"
可以重新创建图形。
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();
}
}
}
}
我的错误是我传递的是JFrame
而不是拥有图形的JComponent
。我更正了代码,
private void saveImageToLocalDisk(JComponent component, String location) {
try {
BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
paint(graphics);
ImageIO.write(image, "jpeg", new File(location + "/Graphics.jpeg"));
} catch (IOException e) {
e.printStackTrace();
}
}