Package java.awt

Examples of java.awt.Graphics2D


            int width  = getWidth();
            int height = getHeight();
           
            double maxY = 0.0;
           
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHints(hints);
           
            g2.setColor(new Color(255, 255, 255, (int) (alphaLevel * shield)));
            g2.fillRect(0, 0, getWidth(), getHeight());
           
            for (int i = 0; i < ticker.length; i++) {
                int channel = 224 - 128 / (i + 1);
                g2.setColor(new Color(channel, channel, channel, alphaLevel));
                g2.fill(ticker[i]);
               
                Rectangle2D bounds = ticker[i].getBounds2D();
                if (bounds.getMaxY() > maxY)
                    maxY = bounds.getMaxY();
            }
           
            if (text != null && text.length() > 0) {
                FontRenderContext context = g2.getFontRenderContext();
                TextLayout layout = new TextLayout(text, getFont(), context);
                Rectangle2D bounds = layout.getBounds();
                g2.setColor(getForeground());
                layout.draw(g2, (float) (width - bounds.getWidth()) / 2,
                        (float) (maxY + layout.getLeading() + 2 * layout.getAscent()));
            }
        }
    }
View Full Code Here


    this.bounds = (Rectangle2D) bounds.clone();
    if (chartRenderingInfo != null)
    {
      this.chartRenderingInfo.clear();
    }
    final Graphics2D g2 = (Graphics2D) graphics2D.create();
    this.chart.draw(g2, bounds, chartRenderingInfo);
    g2.dispose();

    if (chartRenderingInfo == null || debugRendering == false)
    {
      return;
    }
View Full Code Here

    final Rectangle2D otherArea = new Rectangle2D.Double();

    if ((ObjectUtilities.equal(bounds, this.bounds)) == false)
    {
      final BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
      final Graphics2D graphics = image.createGraphics();
      draw(graphics, bounds);
      graphics.dispose();
    }

    final ImageMap map = new ImageMap();
    final EntityCollection entityCollection = chartRenderingInfo.getEntityCollection();
    final int count = entityCollection.getEntityCount();
View Full Code Here

  /* ========== Paint ========== */

  public void paint(Graphics g, JComponent component) {
    if (component != view || view.getModel() == null)
      return;
    Graphics2D g2 = (Graphics2D) g;

    ListModel model = view.getModel();
    Rectangle paintBounds = g.getClipBounds();
    Insets insets = view.getInsets();
    Dimension size = view.getSize();
    int modelSize = model.getSize();
    int xcell = view.getXCellSize(), ycell = view.getYCellSize(), counter = 0;
    metrics = g.getFontMetrics(view.getFont());
   
    if (metrics == null) {
      throw new RuntimeException("Could not resolve font metrics");
    }

    textHeight = metrics.getHeight();

    /* Fix sizes */
    if (paintBounds.x < insets.left)
      paintBounds.x = insets.left;
    if (paintBounds.width > size.width - insets.left - insets.right)
      paintBounds.width = size.width - insets.left - insets.right;
    if (paintBounds.y < insets.top)
      paintBounds.y = insets.top;
    if (view.getParent() instanceof JViewport) {
      paintBounds.height += 2 * ycell;
      if (paintBounds.height > size.height - insets.top - insets.bottom + ycell)
        paintBounds.height = size.height - insets.top - insets.bottom + ycell;
    } else {
      if (paintBounds.height > size.height - insets.top - insets.bottom)
        paintBounds.height = size.height - insets.top - insets.bottom;
    }

    /* Clear background */
    g.setColor(view.getBackground());
    g.fillRect(paintBounds.x, paintBounds.y, paintBounds.width, paintBounds.height);
    g.setColor(view.getForeground());

    int result[] = new int[(paintBounds.width / xcell) * (paintBounds.height / ycell)];
    for (int y = paintBounds.y; y < paintBounds.y + paintBounds.height; y += ycell) {
      for (int x = paintBounds.x; x <= paintBounds.x + paintBounds.width; x += xcell) {
        int index = getIndexForXY(x, y);
        if (index == -1)
          continue;
        int pos[] = getRowColumnForIndex(index);
        int xpos = convertColumnToX(pos[1]);
        int ypos = convertRowToY(pos[0]);
        IconElement element = (IconElement) model.getElementAt(index);
        ImageIcon icon = element.getIcon();
        String name = element.getText();
        int stringWidth = metrics.stringWidth(name);
        int iconHeight = icon.getIconHeight();
        int spacing = view.getIconTextSpacing();
        int center = ycell / 2 - (textHeight + iconHeight + spacing) / 2;

        if (stringWidth > xcell) {
          while (stringWidth > xcell) {
            name = name.substring(0, name.length() - 1);
            stringWidth = metrics.stringWidth(name + "..");
          }
          name += "..";
        }

        g.drawImage(icon.getImage(), xpos + (xcell - icon.getIconWidth()) / 2, ypos + center, null);
        if (view.getSelectedIndex() == index) {
          if (view.hasFocus()) {
            g2.setColor(Color.darkGray);
            g2.setStroke(
              new BasicStroke(
                1.0f,
                BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_MITER,
                1.0f,
                new float[] { 1.0f },
                0.0f));
            g2.draw(
              new Rectangle2D.Double(
                xpos + (xcell - icon.getIconWidth()) / 2 - 1,
                ypos + center - 2,
                icon.getIconWidth() + 2,
                iconHeight + 2));
View Full Code Here

    return true;
  }

  protected void renderBoxBorderAndBackground(final RenderBox box)
  {
    final Graphics2D g2 = getGraphics();
    if (outlineMode)
    {
      if (drawOutlineBox(g2, box))
      {
        return;
      }
    }

    if (box.getBoxDefinition().getBorder().isEmpty() == false)
    {
      borderRenderer.paintBackgroundAndBorder(box, g2);
    }
    else
    {
      final Color backgroundColor = (Color) box.getStyleSheet().getStyleProperty(ElementStyleKeys.BACKGROUND_COLOR);
      if (backgroundColor != null)
      {
        final double x = StrictGeomUtility.toExternalValue(box.getX());
        final double y = StrictGeomUtility.toExternalValue(box.getY());
        final double w = StrictGeomUtility.toExternalValue(box.getWidth());
        final double h = StrictGeomUtility.toExternalValue(box.getHeight());
        boxArea.setFrame(x, y, w, h);
        g2.setColor(backgroundColor);
        g2.fill(boxArea);
      }
    }
  }
View Full Code Here

    }
  }

  private void drawTextDecoration(final FontDecorationSpec decorationSpec)
  {
    final Graphics2D graphics = (Graphics2D) this.graphics.create();
    graphics.setColor(decorationSpec.getTextColor());
    graphics.setStroke(new BasicStroke((float) decorationSpec.getLineWidth()));
    graphics.draw(new Line2D.Double(decorationSpec.getStart(), decorationSpec.getVerticalPosition(),
        decorationSpec.getEnd(), decorationSpec.getVerticalPosition()));
    graphics.dispose();
  }
View Full Code Here

    return textEllipseBox.getWidth();
  }

  protected void drawReplacedContent(final RenderableReplacedContentBox content)
  {
    final Graphics2D g2 = getGraphics();

    final Object o = content.getContent().getRawObject();
    if (o instanceof Image)
    {
      drawImage(content, (Image) o);
View Full Code Here

    }

    final Rectangle2D.Double drawAreaBounds = new Rectangle2D.Double(x, y, width, height);
    final AffineTransform scaleTransform;

    final Graphics2D g2;
    if (shouldScale == false)
    {
      double deviceScaleFactor = 1;
      final double devResolution = metaData.getNumericFeatureValue(OutputProcessorFeature.DEVICE_RESOLUTION);
      if (metaData.isFeatureSupported(OutputProcessorFeature.IMAGE_RESOLUTION_MAPPING))
      {
        if (devResolution != 72.0 && devResolution > 0)
        {
          // Need to scale the device to its native resolution before attempting to draw the image..
          deviceScaleFactor = (72.0 / devResolution);
        }
      }

      final int clipWidth = Math.min(width, (int) Math.ceil(deviceScaleFactor * imageWidth));
      final int clipHeight = Math.min(height, (int) Math.ceil(deviceScaleFactor * imageHeight));
      final ElementAlignment horizontalAlignment =
          (ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.ALIGNMENT);
      final ElementAlignment verticalAlignment =
          (ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.VALIGNMENT);
      final int alignmentX = (int) RenderUtility.computeHorizontalAlignment(horizontalAlignment, width, clipWidth);
      final int alignmentY = (int) RenderUtility.computeVerticalAlignment(verticalAlignment, height, clipHeight);

      g2 = (Graphics2D) getGraphics().create();
      g2.clip(drawAreaBounds);
      g2.translate(x, y);
      g2.translate(alignmentX, alignmentY);
      g2.clip(new Rectangle2D.Float(0, 0, clipWidth, clipHeight));
      g2.scale(deviceScaleFactor, deviceScaleFactor);

      scaleTransform = null;
    }
    else
    {
      g2 = (Graphics2D) getGraphics().create();
      g2.clip(drawAreaBounds);
      g2.translate(x, y);
      g2.clip(new Rectangle2D.Float(0, 0, width, height));

      final double scaleX;
      final double scaleY;

      final boolean keepAspectRatio = layoutContext.getBooleanStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO);
      if (keepAspectRatio)
      {
        final double scaleFactor = Math.min(width / (double) imageWidth, height / (double) imageHeight);
        scaleX = scaleFactor;
        scaleY = scaleFactor;
      }
      else
      {
        scaleX = width / (double) imageWidth;
        scaleY = height / (double) imageHeight;
      }

      final int clipWidth = (int) (scaleX * imageWidth);
      final int clipHeight = (int) (scaleY * imageHeight);

      final ElementAlignment horizontalAlignment =
          (ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.ALIGNMENT);
      final ElementAlignment verticalAlignment =
          (ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.VALIGNMENT);
      final int alignmentX = (int) RenderUtility.computeHorizontalAlignment(horizontalAlignment, width, clipWidth);
      final int alignmentY = (int) RenderUtility.computeVerticalAlignment(verticalAlignment, height, clipHeight);

      g2.translate(alignmentX, alignmentY);

      final Object contentCached = content.getContent().getContentCached();
      if (contentCached instanceof Image)
      {
        image = (Image) contentCached;
        scaleTransform = null;
      }
      else if (metaData.isFeatureSupported(OutputProcessorFeature.PREFER_NATIVE_SCALING) == false)
      {
        image = RenderUtility.scaleImage(image, clipWidth, clipHeight, RenderingHints.VALUE_INTERPOLATION_BICUBIC,
            true);
        content.getContent().setContentCached(image);
        obs = new WaitingImageObserver(image);
        obs.waitImageLoaded();
        scaleTransform = null;
      }
      else
      {
        scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
      }
    }

    while (g2.drawImage(image, scaleTransform, obs) == false)
    {
      obs.waitImageLoaded();
      if (obs.isError())
      {
        LogicalPageDrawable.logger.warn("Error while loading the image during the rendering.");
        break;
      }
    }

    g2.dispose();
    return true;
  }
View Full Code Here

    if ((width < 0 || height < 0) || (width == 0 && height == 0))
    {
      return false;
    }

    final Graphics2D clone = (Graphics2D) g2.create();


    final StyleSheet styleSheet = content.getStyleSheet();
    final Object attribute = styleSheet.getStyleProperty(ElementStyleKeys.ANTI_ALIASING);
    if (attribute != null)
    {
      if (Boolean.TRUE.equals(attribute))
      {
        clone.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      }
      else if (Boolean.FALSE.equals(attribute))
      {
        clone.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
      }

    }
    if (RenderUtility.isFontSmooth(styleSheet, metaData))
    {
      clone.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }
    else
    {
      clone.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    }

    if (strictClipping == false)
    {
      final double extraPadding;
      final Object o = styleSheet.getStyleProperty(ElementStyleKeys.STROKE);
      if (o instanceof BasicStroke)
      {
        final BasicStroke stroke = (BasicStroke) o;
        extraPadding = stroke.getLineWidth() / 2.0;
      }
      else
      {
        extraPadding = 0.5;
      }

      final Rectangle2D.Double clipBounds = new Rectangle2D.Double
          (x - extraPadding, y - extraPadding, width + 2 * extraPadding, height + 2 * extraPadding);

      clone.clip(clipBounds);
      clone.translate(x, y);
    }
    else
    {
      final Rectangle2D.Double clipBounds = new Rectangle2D.Double(x, y, width + 1, height + 1);

      clone.clip(clipBounds);
      clone.translate(x, y);
    }
    configureGraphics(styleSheet, clone);
    configureStroke(styleSheet, clone);
    final Rectangle2D.Double bounds = new Rectangle2D.Double(0, 0, width, height);
    d.draw(clone, bounds);
    clone.dispose();
    return true;
  }
View Full Code Here

    }

    final long posX = renderableText.getX();
    final long posY = renderableText.getY();

    final Graphics2D g2;
    if (textSpec == null)
    {
      g2 = (Graphics2D) getGraphics().create();
      final StyleSheet layoutContext = renderableText.getStyleSheet();
      configureGraphics(layoutContext, g2);
      g2.setStroke(LogicalPageDrawable.DEFAULT_STROKE);

      if (RenderUtility.isFontSmooth(layoutContext, metaData))
      {
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      }
      else
      {
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
      }
    }
    else
    {
      g2 = textSpec.getGraphics();
    }

    // This shifting is necessary to make sure that all text is rendered like in the previous versions.
    // In the earlier versions, we did not really obey to the baselines of the text, we just hoped and prayed.
    // Therefore, all text was printed at the bottom of the text elements. With the introduction of the full
    // font metrics setting, this situation got a little bit better, for the price that text-elements became
    // nearly unpredictable ..
    //
    // The code below may be weird, but at least it is predictable weird.
    final ExtendedBaselineInfo baselineInfo = renderableText.getBaselineInfo();
    final long baseline = baselineInfo.getBaseline(baselineInfo.getDominantBaseline());

    final FontMetrics fm = g2.getFontMetrics();
    final Rectangle2D rect = fm.getMaxCharBounds(g2);
    final long awtBaseLine = StrictGeomUtility.toInternalValue(-rect.getY());

    final GlyphList gs = renderableText.getGlyphs();
    if (metaData.isFeatureSupported(OutputProcessorFeature.FAST_FONTRENDERING) &&
        isNormalTextSpacing(renderableText))
    {
      final int maxLength = computeMaximumTextSize(renderableText, contentX2);
      final String text = gs.getText(renderableText.getOffset(), maxLength, codePointBuffer);
      final float y = (float) StrictGeomUtility.toExternalValue(posY + awtBaseLine);
      g2.drawString(text, (float) StrictGeomUtility.toExternalValue(posX), y);
    }
    else
    {
      final int maxPos = renderableText.getOffset() + computeMaximumTextSize(renderableText, contentX2);
      long runningPos = posX;
      final long baselineDelta = awtBaseLine - baseline;
      for (int i = renderableText.getOffset(); i < maxPos; i++)
      {
        final Glyph g = gs.getGlyph(i);
        final float y = (float) StrictGeomUtility.toExternalValue(posY + g.getBaseLine() + baselineDelta);
        g2.drawString(gs.getGlyphAsString(i, codePointBuffer), (float) StrictGeomUtility.toExternalValue(runningPos), y);
        runningPos += g.getWidth() + g.getSpacing().getMinimum();
      }
    }
    g2.dispose();
  }
View Full Code Here

TOP

Related Classes of java.awt.Graphics2D

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.