title: Java Canvas Draw Image from BufferedImage
body: <p>I am currently working on my personal project. The idea is to import a picture and crop it into n-pieces of bufferedimage then draw them with random sequence to a java canvas. After that save the canvas to image file. Right now iam stuck at saving the canvas and i have searching for the solutions but none of them work. Here my save procedure.
  Please help, thanks in advance</p>
  
  <pre><code> private void save() {    
      int r = jFileChooser1.showSaveDialog(this);
      File file2 = null; 
      if(r == jFileChooser1.APPROVE_OPTION){
      file2 = jFileChooser1.getSelectedFile();
  
      Graphics g2d = canvas.getGraphics();    
      BufferedImage bi = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);  
      Graphics2D g2 = bi.createGraphics();
      canvas.paint(g2);  
      g2.dispose();  
      g2d.dispose();
      try  
      {  
          ImageIO.write(bi, "jpg", file2);  
      }  
      catch (Exception ex) {
          System.out.println("Error saving");
      }
      }
  }
  </code></pre>
  
  <p>Update :
  Now i get it, after using paintComponents now i can save the picture. Thanks @madProgrammer</p>
  
  <pre><code>public class CaptureImage extends javax.swing.JFrame {
  
  MyPanel canvas;
  BufferedImage[] processedImage;
  
  
  public CaptureImage(BufferedImage[] proc) {
      processedImage = proc;
      SwingUtilities.invokeLater(new Runnable() {
  
          public void run() {
              createAndShowGUI();
          }
      });
  
  }
  
  private void createAndShowGUI() {
      System.out.println("Created GUI on EDT? "
              + SwingUtilities.isEventDispatchThread());
      JFrame f = new JFrame("Output Picture");
      f.addWindowListener(new WindowAdapter(){
              public void windowClosing(WindowEvent e){
                  save();
                  System.exit(0);//cierra aplicacion
              }
          });
      canvas = new MyPanel(processedImage);
      f.add(canvas);
      f.setSize(400, 400);
      f.setVisible(true);
  }
  
  public void save() {
      JFileChooser jFileChooser1 = new JFileChooser();
      int r = jFileChooser1.showSaveDialog(this);
      File file2 = null;
      if (r == jFileChooser1.APPROVE_OPTION) {
          file2 = jFileChooser1.getSelectedFile();
  
          BufferedImage bi = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
          Graphics2D g2 = bi.createGraphics();
          canvas.print(g2);
  
          try {
              ImageIO.write(bi, "jpg", file2);
              g2.dispose();
          } catch (Exception ex) {
              System.out.println("Error saving");
          }
      }
  }
  }
  
  
   class MyPanel extends JPanel {
  
  private Image[] processedImage;
  
  public MyPanel(Image[] processedImage) {
      this.processedImage = processedImage;
  
      addMouseListener(new MouseAdapter() {
  
          public void mousePressed(MouseEvent e) {
              repaint();
          }
      });
  
  }
  
  public Dimension getPreferredSize() {
      return new Dimension(300, 300);
  }
  
  public void paintComponent(Graphics g) {
      super.paintComponent(g);
      int loopj = 10;
      int loopi = 10;
      int l = 0;
      int jPieces = 100;
      int[] r = new int[jPieces];
      int rand = 0;
      for (int i = 0; i &lt; r.length; i++) {
          rand = (int) (Math.random() * (r.length));
          if (Arrays.binarySearch(r, 0, r.length, rand) &lt;= -1) {
              r[i] = rand;
          }
      }
      Graphics2D g2d = (Graphics2D) g;
      for (int i = 0; i &lt; loopi; i++) {
          for (int j = 0; j &lt; loopj; j++) {
              g2d.drawImage(processedImage[r[l]], i * 30, j * 30, this);
              l++;
          }
      }
  }
  }
  </code></pre>