I have SWing code that creates some graphics. I have a button "Run Again"
which can re-create the graphics.
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);
}
});
The save button is for saving current graphics as an image. However, when I save it doesn't save the current graphics, but, only the initial one.
The method that saves is provided below,
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");
}
}
How do I correct this?
Here is a small example, how I would implement your app (recreate + save image).
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();
}
}
}
}
My mistake was that I was passing the JFrame
instead of the JComponent
which owns the graphics. I corrected the code,
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.