我试图用 x 和 y 坐标绘制一些点并将输出保存到图像文件中,但我不能。(没有必要在 JFrame 上看到它们)据我通过搜索了解到,我可以创建绘图并将其显示在 JFrame 上,但我无法将此输出保存到文件中。
public static void main(String[] args) {
try {
final JFrame frm = new JFrame("Points");
final Panel pnl = new Panel();
pnl.setPreferredSize(new Dimension(1000, 1000));
frm.setContentPane(pnl);
frm.pack();
frm.setVisible(true);
frm.repaint();
Image img;
img = frm.createImage(1000, 1000);
ImageIO.write((RenderedImage) img, "jpeg", new File("C:/.../p.jpeg"));
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (final Exception e) {
e.printStackTrace();
}
}
public static class Panel extends JPanel {
@Override
public void paintComponent(final Graphics g) {
g.setColor(Color.RED);
for (final Point p : CandidatePoints) {
g.fillRect((int) …Run Code Online (Sandbox Code Playgroud)