/**
   * Renders the shape that is currently being sized by the user as they drag the mouse across the
   * canvas. We render this separately because we have not yet put it in the shapes list for the
   * pose.
   *
   * @param g2 The graphics context for this canvas.
   * @param poseArea The area in the middle of the canvas where the pose will be rendered.
   */
  private void renderShapeInProgress(Graphics2D g2, Rectangle2D.Double poseArea) {
    AnimatedSpriteEditor singleton = AnimatedSpriteEditor.getEditor();
    PoseurStateManager poseurStateManager = singleton.getStateManager().getPoseurStateManager();
    PoseurShape shapeInProgress = poseurStateManager.getShapeInProgress();

    // ONLY DO THIS ON THE RIGHT SIDE
    if (state.isZoomable() && (shapeInProgress != null)) {
      float zoomLevel = state.getZoomLevel();
      shapeInProgress.render(g2, (int) poseArea.getX(), (int) poseArea.getY(), zoomLevel, true);
    }
  }
  /**
   * Renders all the shapes in the corresponding canvas state object for this canvas.
   *
   * @param g2 The graphics context of this panel.
   */
  private void renderShapes(Graphics2D g2, Rectangle2D.Double poseArea) {
    // LET'S GET THE POSE AREA AND THE POSE
    PoseurPose pose = state.getPose();
    float zoomLevel = state.getZoomLevel();

    // RENDER THE ENTIRE POSE
    Iterator<PoseurShape> shapesIt = pose.getShapesIterator();
    AnimatedSpriteEditor singleton = AnimatedSpriteEditor.getEditor();
    PoseurStateManager poseurStateManager = singleton.getStateManager().getPoseurStateManager();
    while (shapesIt.hasNext()) {
      PoseurShape shape = shapesIt.next();
      boolean isSelected = poseurStateManager.isSelectedShape(shape);

      // NOTE THAT WE NEVER DEPICT SELECTED SHAPES DIFFERENTLY
      // IN THE TRUE CANVAS, ONLY THE ZOOMABLE CANVAS
      if (!state.isZoomable()) {
        isSelected = false;
      }
      shape.render(g2, (int) poseArea.getX(), (int) poseArea.getY(), zoomLevel, isSelected);
    }
  }
  /**
   * This method renders what would be in the pose area into the imageToPaintTo image argument. The
   * paintComponent method paints to a canvas (i.e. the screen), which this one is paints to an
   * image that can then be saved to a file
   *
   * @param imageToPaintTo The image we will paint what is in this canvas' pose area to.
   */
  public void paintToImage(BufferedImage imageToPaintTo) {
    Graphics2D imageG2 = (Graphics2D) imageToPaintTo.getGraphics();
    imageG2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    AnimatedSpriteEditor singleton = AnimatedSpriteEditor.getEditor();
    PoseurStateManager poseurStateManager = singleton.getStateManager().getPoseurStateManager();
    PoseurPose pose = poseurStateManager.getPose();

    // FIRST LET'S PUT A TRANSPARENT BACKGROUND THE SIZE OF THE POSE
    Rectangle2D.Double transparentRect =
        new Rectangle2D.Double(0, 0, pose.getPoseWidth(), pose.getPoseHeight());
    imageG2.setColor(TRANSPARENT_BACKGROUND_COLOR);
    imageG2.fill(transparentRect);
    imageG2.draw(transparentRect);

    // NOW LET'S DRAW ALL THE SHAPES ON TOP, BUT WE'LL
    // FAKE THE POSE AREA SO WE ONLY DRAW THE MIDDLE PART
    Rectangle2D.Double poseArea =
        new Rectangle2D.Double(0, 0, pose.getPoseWidth(), pose.getPoseHeight());
    renderShapes(imageG2, poseArea);

    // ALL DONE, WE'VE JUST PAINTED TO THE IMAGE WHAT WE
    // WOULD NORMALLY DRAW INSIDE THE POSE AREA
  }