title: How to clip a BufferedImage with a shape created with Graphics2D?(Java)
body: <p>Currently, I'm working on my first Java application with which a user can look through photos, cut them and rotate. I have an issue with clipping an image. What I want to achieve is the following:</p>
  
  <ul>
  <li>User clicks on the "Cut" option </li>
  <li>Rectangle shape called by repaint method appears on the image </li>
  <li>By stretching the rectangle user chooses the area for cutting </li>
  <li>When the user releases the mouse(which stops stretching the rectangle) the area that is surrounded with the rectangle is left and all the rest of the image is cut.</li>
  </ul>
  
  <p>As of for now I have several issues:</p>
  
  <ol>
  <li>My image is centralized on JLabel which in its turn is added to JPanel and the last is added to JFrame, so now, when I want to add a rectangle above JLable (so it is to be located on the picture) it's invisible and is added only on JPanel directly.</li>
  <li>I drew an image with paintComponent but can't figure out how to move and stretch it and repaint the rectangle again.</li>
  </ol>
  
  <p>Below is the part of my code which (I hope) will describe my problems more precisely:</p>
  
  <pre><code>public class GraphicalUserInterface {
  
  
  static JPanel background;
  static JLabel labelIcon;
  
  public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
              new GraphicalUserInterface().go();
          }
      });
  }
  
  
  public void go() {
      buildGui();
  }
  
  public void buildGui() {
  
      frame = new JFrame("PicMove");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      BorderLayout layout = new BorderLayout();
      background = new JPanel(layout);
  
     /**To center picture on the background**/
      labelIcon = new JLabel();
      labelIcon.setHorizontalAlignment(JLabel.CENTER);
      labelIcon.setVerticalAlignment(JLabel.CENTER);
  
      background.add(BorderLayout.SOUTH, bottom);
      background.add(BorderLayout.PAGE_START, bar);
      background.add(BorderLayout.CENTER, labelIcon);
      background.add(BorderLayout.EAST, chatPanel);
  
      frame.getContentPane().add(BorderLayout.CENTER, background);
      frame.setJMenuBar(menuBar);
      frame.setVisible(true);
      frame.setSize(1300, 1200);}
  
  
  static class CutImage extends JPanel implements Runnable {
      boolean clip;
  
      @Override
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
  
          Graphics2D g2d = (Graphics2D) g;
  
          if (clip) {
              BasicStroke bs = new BasicStroke(50, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
                      10, null, 0);
              g2d.setStroke(bs);
              QuadCurve2D.Float qc = new QuadCurve2D.Float(20, 50, 100, 140, 460, 170);
              g2d.setClip(qc);
          }
          BasicStroke bs = new BasicStroke(5, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
                  10, new float[]{10}, 0);
          g2d.setStroke(bs);
          g2d.drawRect(260, 50, 80, 120);
  
      }
  
      @Override
      public void run() {
          CutImage cutPanel = new CutImage();
          GraphicalUserInterface.background.add(cutPanel).repaint();
      }
  }
  public class PicChanges implements Runnable{
  
  static BufferedImage newImage;
  static File [] selectedFile;
  static int currentImage;
  
  
  
  FileNameExtensionFilter filter;
  JFileChooser fileChooser;
  
  public void openPic() {
      currentImage = 0;
      try {
          fileChooser = new JFileChooser();
          fileChooser.setCurrentDirectory(new java.io.File((System.getProperty("user.home"))));
          filter = new FileNameExtensionFilter("*.images", "jpg", "gif", "png");
          fileChooser.addChoosableFileFilter(filter);
          fileChooser.setMultiSelectionEnabled(true);
          int result = fileChooser.showOpenDialog(null);
          if (result == JFileChooser.OPEN_DIALOG) {
              selectedFile = fileChooser.getSelectedFiles();
              for (File image : selectedFile) {
                  if ((image.isFile()) &amp;&amp; (selectedFile.length &gt; 0)){
                      newImage = ImageIO.read(selectedFile[0]);
                      GraphicalUserInterface.labelIcon.setIcon(new ImageIcon(
                              new ImageIcon(newImage).getImage().getScaledInstance(
                                      450, 620, Image.SCALE_DEFAULT)));
                  } else if (result == JFileChooser.CANCEL_OPTION) {
                      System.out.println("No Pics Selected");
                  }
              }
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  @Override
  public void run() {
      Thread.currentThread().interrupt();
      openPic();
  }
  
  public static void nextPic() {
      currentImage++;
      try {
          newImage = ImageIO.read(selectedFile[currentImage]);
      } catch (IOException e) {
          e.printStackTrace();
          System.out.println("No pictures left");
          System.out.println("next"+currentImage);
      }
      GraphicalUserInterface.labelIcon.setIcon(new ImageIcon(
              new ImageIcon(newImage).getImage().getScaledInstance(
                      450, 620, Image.SCALE_DEFAULT)));
  }
  
  static class NextPicture implements Runnable{
      @Override
      public void run() {
          Thread.currentThread().interrupt();
          nextPic();
      }
  }
  
  public static void previousPic () {
      currentImage--;
      try {
          newImage = ImageIO.read(selectedFile[currentImage]);
      } catch (IOException e) {
          e.printStackTrace();
          System.out.println("previous "+currentImage);
      }
      GraphicalUserInterface.labelIcon.setIcon(new ImageIcon(
              new ImageIcon(newImage).getImage().getScaledInstance(
                      450, 620, Image.SCALE_DEFAULT)));
  }
  
  static class PreviousPic implements Runnable{
      @Override
      public void run() {
          Thread.currentThread().interrupt();
          previousPic();
      }
  }
  }
  </code></pre>
  
  <p>My idea was to add MouseListeners but can I add it to the shape created with Graphics2D?
  I would be greatful for the help :)
  Thank you</p>