title: Create Image from Jframe
body: <p>I created a pie chart using JFrame and swings. But I want to convert the pie chart into the image and save in desktop/local path. But not having idea how to create image using JFrame Pie.</p>
  
  <pre><code>package test;
  
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.Rectangle;
  
  import javax.swing.JComponent;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  class Main1 {
    double value;
    Color color;
  
  
    public Main1(double value, Color color) {
      this.value = value;
      this.color = color;
    }
  }
  
  class MyComponent extends JComponent {
      Main1[] slices = { new Main1(1, Color.black), new Main1(1, Color.green),
        new Main1(1, Color.yellow), new Main1(1, Color.red) };
  
    MyComponent() {
  
    }
    public void paint(Graphics g) {
      drawPie((Graphics2D) g, getBounds(), slices);
    }
  
    void drawPie(Graphics2D g, Rectangle area, Main1[] slices) {
      double total = 0.0D;
      for (int i = 0; i &lt; slices.length; i++) {
        total += slices[i].value;
      }
  
      double curValue = 0.0D;
      int startAngle = 0;
      for (int i = 0; i &lt; slices.length; i++) {
        startAngle = (int) (curValue * 360 / total);
        int arcAngle = (int) (slices[i].value * 360 / total);
  
        g.setColor(slices[i].color);
        g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);
        curValue += slices[i].value;
      }
    }
  }
  
  public class Main2 {
       public JPanel contentPane;
    public static void main(String[] argv) {
      JFrame frame = new JFrame();
      frame.getContentPane().add(new MyComponent());
      frame.setSize(300, 200);
      frame.setVisible(true);   
    }
  
  }
  </code></pre>