Swing 将显示好的JPanel保存为一张图片

在JFrame中添加如下代码


   //自己的JPanel

DrawPanel drawPanel = new DrawPanel(list, width, height, start, end);

// 必须要有大小
drawPanel.setSize(width, height);

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
drawPanel.paint(g2);
try {
ImageIO.write(image, "jpeg", new File("jpanel.jpg"));
} catch (IOException e) {
e.printStackTrace();
}


你可能感兴趣的:(swing)