⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 g2outputtarget.java

📁 swing编写的库存管理程序。毕业设计类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        (float) currentPageFormat.getImageableHeight() + 1);
    g2.clip(bounds);
    g2.transform(AffineTransform.getTranslateInstance(
        currentPageFormat.getImageableX() + pageBounds.getX(),
        currentPageFormat.getImageableY() + pageBounds.getY()));
    savedState = saveState();
  }

  /**
   * A page has ended.  This target restores the state of the Graphics2D device.
   *
   * @throws OutputTargetException if there is a problem with the target.
   */
  public void endPage() throws OutputTargetException
  {
    final PageFormat currentPageFormat = currentPage.getPageFormat();
    final Rectangle2D pageBounds = currentPage.getBounds();
    g2.setClip(originalClip);
    g2.transform(AffineTransform.getTranslateInstance(0 - currentPageFormat.getImageableX()
        - pageBounds.getX(),
        0 - currentPageFormat.getImageableY()
        - pageBounds.getY()));
    restoreState();
  }

  /**
   * Sets the font.
   *
   * @param font  the font.
   */
  public void setFont(final FontDefinition font)
  {
    if (font == null)
    {
      throw new NullPointerException();
    }
    this.fontDefinition = font;
    g2.setFont(font.getFont());
  }

  /**
   * Returns the current font.
   *
   * @return the font.
   */
  public FontDefinition getFont()
  {
    return fontDefinition;
  }

  /**
   * Sets the paint.
   *
   * @param paint The paint.
   */
  public void setPaint(final Paint paint)
  {
    g2.setPaint(paint);
  }

  /**
   * Returns the current paint.
   *
   * @return the paint.
   */
  public Paint getPaint()
  {
    return g2.getPaint();
  }

  /**
   * Returns the Stroke for the Graphics2D context.
   *
   * @return the stroke.
   */
  public Stroke getStroke()
  {
    return g2.getStroke();
  }

  /**
   * Sets the Stroke for the Graphics2D context.
   *
   * @param stroke  the stroke.
   *
   * @throws OutputTargetException this exception is not thrown here.
   */
  public void setStroke(final Stroke stroke) throws OutputTargetException
  {
    g2.setStroke(stroke);
  }

  /**
   * Draws a shape. The shape is drawn using Graphics2D.draw.  A paint and a stroke have to be
   * set separately.
   * <P>
   *
   * @param shape The shape.
   */
  public void drawShape(final Shape shape)
  {
    g2.draw(shape);
  }

  /**
   * Fills a shape. The shape is drawn using Graphics2D.draw. A paint and a stroke have to be
   * set separately.
   * <P>
   *
   * @param shape The shape.
   */
  public void fillShape(final Shape shape)
  {
    g2.fill(shape);
  }

  /**
   * Draws the image contained in the given ImageReference.
   *
   * @param image the image reference used to contain the image.
   */
  public void drawImage(final ImageReference image)
  {
    final Rectangle2D myBounds = image.getBoundsScaled();
    final Rectangle2D bounds = getOperationBounds();

    if (image.getImage() != null)
    {
      final Shape s = g2.getClip();
      final AffineTransform transform = g2.getTransform();
      try
      {
        g2.clip(
            new Rectangle2D.Float(0, 0,
                (float) (Math.min(bounds.getWidth(), myBounds.getWidth())),
                (float) (Math.min(bounds.getHeight(), myBounds.getHeight())))
        );
        g2.transform(AffineTransform.getScaleInstance(image.getScaleX(), image.getScaleY()));
        while (g2.drawImage(image.getImage(),
            (int) -myBounds.getX(), (int) -myBounds.getY(), null) == false)
        {
          final WaitingImageObserver obs = new WaitingImageObserver(image.getImage());
          obs.waitImageLoaded();
          if (obs.isError())
          {
            Log.warn("The image observer detected an error while loading the Image");
            break;
          }
        }
      }
      catch (Throwable th)
      {
        // just in case the image drawing caused trouble ..
        Log.warn(new Log.MemoryUsageMessage("Failure at drawImage"));
        Log.warn(th);
      }
      g2.setTransform(transform);
      g2.setClip(s);
    }
    else
    {
      Log.warn("The image-reference contained no content!");
    }
  }

  /**
   * Draws a string inside a rectangular area (the lower edge is aligned with the baseline of
   * the text).
   *
   * @param text The text.
   */
  public void drawString(final String text)
  {
    // Draw the string on the given location. The fontmetrics is not correct for
    // the fonts I tested ("Arial 10 Plain -> FM.height = 13, Ascent = 10, Descent = 2"
    // while iText found out that the correct Ascent must be 7.2802734 and the descent
    // for that font is 2.104492.
    //
    // I don't trust java, and iText reads the font descriptor from the files, so I
    // correct the AWT-fontMetrics. This correction is not 100% perfect, but it is
    // perfect enough for printing ...
    final FontMetrics fm = g2.getFontMetrics();
    final float baseline = (float) fm.getAscent();
    final float cFact = getFont().getFont().getSize2D() / fm.getHeight();

    final float correctedBaseline = baseline * cFact;
    g2.drawString(text, 0.0f, correctedBaseline);
    if (getFont().isUnderline())
    {
      final float l = (getFont().getFont().getSize2D() + correctedBaseline) / 2.0f;
      final Line2D line = new Line2D.Float(0, l, (float) getOperationBounds().getWidth(), l);
      g2.draw(line);
    }
    if (getFont().isStrikeThrough())
    {
      final float fontHeight = getFont().getFont().getSize2D();
      final Line2D line = new Line2D.Float(0, fontHeight / 2,
          (float) getOperationBounds().getWidth(), fontHeight / 2);
      g2.draw(line);
    }
  }

  /**
   * Restores the state of this graphics.
   *
   * @throws OutputTargetException if the argument is not an instance of G2State.
   */
  private void restoreState() throws OutputTargetException
  {
    savedState.restore(this);
  }

  /**
   * Saves the state of this graphics object. Use restoreState to restore a previously saved
   * state.
   *
   * @return the state container.
   */
  private G2State saveState()
  {
    return new G2State(this);
  }

  /**
   * Returns a reference to the used graphics2D instance.
   *
   * @return the graphics2D instance used in this target.
   */
  protected Graphics2D getGraphics2D()
  {
    return g2;
  }

  /**
   * Creates an output target that mimics a real output target, but produces no output.
   * This is used by the reporting engine when it makes its first pass through the report,
   * calculating page boundaries etc.  The second pass will use a real output target.
   *
   * @return a dummy output target.
   */
  public OutputTarget createDummyWriter()
  {
    return new DummyOutputTarget(this);
  }

  /**
   * Configures the output target.
   * <p>
   * There are no configuration parameters that affect this output target, so this method
   * does nothing.
   *
   * @param config  the configuration.
   */
  public void configure(final ReportConfiguration config)
  {
    // nothing to do here, G2OuputTarget is not configured in any way.
  }

  /**
   * Creates a size calculator for the current state of the output target. The calculator
   * is used to calculate the string width and line height and later maybe more.
   *
   * @param font  the font.
   *
   * @return the size calculator.
   */
  public SizeCalculator createTextSizeCalculator(final FontDefinition font)
  {
    return DefaultSizeCalculator.getDefaultSizeCalculator(font);
  }

  /**
   * Sets the operation bounds.
   *
   * @param bounds  the bounds.
   */
  public void setOperationBounds(final Rectangle2D bounds)
  {
    final Rectangle2D oldBounds = super.getOperationBounds();
    // undo the last bounds operation
    g2.transform(AffineTransform.getTranslateInstance(0 - oldBounds.getX(), 0 - oldBounds.getY()));
    super.setOperationBounds(bounds);
    // then apply the new bounds operation
    g2.transform(AffineTransform.getTranslateInstance(bounds.getX(), bounds.getY()));
  }

  /**
   * Draws a drawable relative to the current position.
   *
   * @param drawable the drawable to draw.
   */
  public void drawDrawable(final DrawableContainer drawable)
  {

    // only the drawable clippingbounds region will be drawn.
    // the clipping is set to the clipping bounds of the drawable

    // the clipping bounds are relative to the drawable dimension,
    // they are not influenced by the drawables position on the page

    final Rectangle2D clipBounds = drawable.getClippingBounds();

    final Graphics2D target = (Graphics2D) g2.create();
    target.translate(-clipBounds.getX(), -clipBounds.getY());
    target.clip(new Rectangle2D.Float(0, 0,
        (float) clipBounds.getWidth(),
        (float) clipBounds.getHeight()));

    final Dimension2D drawableSize = drawable.getDrawableSize();
    final Rectangle2D drawBounds = new Rectangle2D.Float(0, 0,
        (float) drawableSize.getWidth(),
        (float) drawableSize.getHeight());
    drawable.getDrawable().draw(target, drawBounds);
    target.dispose();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -