java.awt.Graphics2D.setComposite()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(601)

本文整理了Java中java.awt.Graphics2D.setComposite()方法的一些代码示例,展示了Graphics2D.setComposite()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graphics2D.setComposite()方法的具体详情如下:
包路径:java.awt.Graphics2D
类名称:Graphics2D
方法名:setComposite

Graphics2D.setComposite介绍

[英]Sets the Composite for the Graphics2D context. The Composite is used in all drawing methods such as drawImage, drawString, draw, and fill. It specifies how new pixels are to be combined with the existing pixels on the graphics device during the rendering process.

*Note: This operation is subject to restriction in this Profile.*If the Composite is a custom object rather than an instance of the AlphaComposite class then IllegalArgumentException is thrown.
[中]为Graphics2D上下文设置CompositeComposite用于所有绘图方法,如drawImagedrawStringdrawfill。它指定在渲染过程中如何将新像素与图形设备上的现有像素组合。
注意:此操作受此配置文件中的restriction约束**如果Composite是自定义对象而不是AlphaComposite类的实例,则会抛出IllegalArgumentException

代码示例

代码示例来源:origin: libgdx/libgdx

/** Returns an image that can be used by effects as a temp image. */
static public BufferedImage getScratchImage () {
  Graphics2D g = (Graphics2D)scratchImage.getGraphics();
  g.setComposite(AlphaComposite.Clear);
  g.fillRect(0, 0, GlyphPage.MAX_GLYPH_SIZE, GlyphPage.MAX_GLYPH_SIZE);
  g.setComposite(AlphaComposite.SrcOver);
  g.setColor(java.awt.Color.white);
  return scratchImage;
}

代码示例来源:origin: pentaho/pentaho-kettle

public void setAlpha( int alpha ) {
 this.alpha = alpha;
 AlphaComposite alphaComposite = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha / 255 );
 gc.setComposite( alphaComposite );
}

代码示例来源:origin: coobird/thumbnailator

public BufferedImage apply(BufferedImage img)
{
  int width = img.getWidth();
  int height = img.getHeight();
  
  BufferedImage finalImage =
    new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  
  Graphics2D g = finalImage.createGraphics();
  g.setComposite(composite);
  g.drawImage(img, 0, 0, null);
  g.dispose();
  
  return finalImage;
}

代码示例来源:origin: looly/hutool

@Override
public Image createImage(String code) {
  final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  final Graphics2D g = ImageUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
  
  // 随机画干扰圈圈
  drawInterfere(g);
  
  // 画字符串
  // 抗锯齿
  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g.setFont(font);
  final FontMetrics metrics = g.getFontMetrics();
  final int minY = metrics.getAscent() - metrics.getLeading() - metrics.getDescent();
  int len = code.length();
  int charWidth = width / len;
  for (int i = 0; i < len; i++) {
    // 指定透明度
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
    g.setColor(ImageUtil.randomColor());
    g.drawString(String.valueOf(code.charAt(i)), i * charWidth, RandomUtil.randomInt(minY, this.height));
  }
  
  return image;
}

代码示例来源:origin: apache/geode

@Override
 public void paint(Graphics g) {
  super.paint(g);

  if (zoomBoxStartX != -1 && zoomBoxStartY != -1 && zoomBoxWidth != -1 && zoomBoxHeight != -1) {
   Graphics2D g2 = (Graphics2D) g.create();

   Composite old = g2.getComposite();
   g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
   g2.setColor(Color.BLUE);

   // g2.drawRect(zoomBoxStartX, zoomBoxStartY, zoomBoxWidth, zoomBoxHeight);
   // g2.setBackground(Color.BLUE);
   g2.fillRect(getBoxX(), getBoxY(), getBoxWidth(), getBoxHeight());
   g2.setComposite(old);
  }
 }
}

代码示例来源:origin: stackoverflow.com

Rectangle2D txRect = new Rectangle2D.Double(0, 0, textureImg.getWidth(), textureImg.getHeight());
TexturePaint txPaint = new TexturePaint(textureImg, txRect);
g2d.setPaint(txPaint);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
g2d.fillRect(0, 0, getWidth(), getHeight());
Graphics2D g2d = (Graphics2D) g.create();
if(gradientImage==null || gradientImage.getHeight() != getHeight())
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);

代码示例来源:origin: geotools/geotools

private BufferedImage blend(BufferedImage src, BufferedImage dst) {
  BufferedImage blend = new BufferedImage(src.getWidth(), dst.getWidth(), src.getType());
  Graphics2D graphics = (Graphics2D) blend.getGraphics();
  graphics.drawRenderedImage(src, new AffineTransform());
  graphics.setComposite(composite);
  graphics.drawRenderedImage(dst, new AffineTransform());
  graphics.dispose();
  return blend;
}

代码示例来源:origin: haraldk/TwelveMonkeys

static BufferedImage createClear(int pWidth, int pHeight, int pType, int pTransparency, Color pBackground, GraphicsConfiguration pConfiguration) {
  // Create
  int transparency = (pBackground != null) ? pBackground.getTransparency() : pTransparency;
  BufferedImage image = createBuffered(pWidth, pHeight, pType, transparency, pConfiguration);
  if (pBackground != null) {
    // Clear image with clear color, by drawing a rectangle
    Graphics2D g = image.createGraphics();
    try {
      g.setComposite(AlphaComposite.Src);  // Allow color to be translucent
      g.setColor(pBackground);
      g.fillRect(0, 0, pWidth, pHeight);
    }
    finally {
      g.dispose();
    }
  }
  return image;
}

代码示例来源:origin: libgdx/libgdx

public void draw (BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) {
  g = (Graphics2D)g.create();
  g.translate(xDistance, yDistance);
  g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), Math.round(opacity * 255)));
  g.fill(glyph.getShape());
  // Also shadow the outline, if one exists.
  for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext();) {
    Effect effect = (Effect)iter.next();
    if (effect instanceof OutlineEffect) {
      Composite composite = g.getComposite();
      g.setComposite(AlphaComposite.Src); // Prevent shadow and outline shadow alpha from combining.
      g.setStroke(((OutlineEffect)effect).getStroke());
      g.draw(glyph.getShape());
      g.setComposite(composite);
      break;
    }
  }
  g.dispose();
  if (blurKernelSize > 1 && blurKernelSize < NUM_KERNELS && blurPasses > 0) blur(image);
}

代码示例来源:origin: nodebox/nodebox

public void draw(Graphics2D g) {
  setupTransform(g);
  // You can only position an image using an affine transformation.
  // We use the transformation to translate the image to the specified
  // position, and scale it according to the given width and height.
  Transform imageTrans = new Transform();
  // Move to the image position. Convert x, y, which are centered coordinates,
  // to "real" coordinates. 
  double factor = getScaleFactor();
  double finalWidth = image.getWidth() * factor;
  double finalHeight = image.getHeight() * factor;
  imageTrans.translate(x - finalWidth / 2, y - finalHeight / 2);
  // Scaling only applies to image that have their desired width and/or height set.
  // However, getScaleFactor return 1 if height/width are not set, in effect negating
  // the effect of the scale.
  imageTrans.scale(getScaleFactor());
  double a = clamp(alpha);
  Composite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) a);
  Composite oldComposite = g.getComposite();
  g.setComposite(composite);
  g.drawRenderedImage(image, imageTrans.getAffineTransform());
  g.setComposite(oldComposite);
  restoreTransform(g);
}

代码示例来源:origin: stackoverflow.com

public class ContentPane extends JPanel {

  public ContentPane() {

    setOpaque(false);

  }

  @Override
  protected void paintComponent(Graphics g) {

    // Allow super to paint
    super.paintComponent(g);

    // Apply our own painting effect
    Graphics2D g2d = (Graphics2D) g.create();
    // 50% transparent Alpha
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));

    g2d.setColor(getBackground());
    g2d.fill(getBounds());

    g2d.dispose();

  }

}

代码示例来源:origin: geotools/geotools

@Override
  void execute() {
    if (graphics instanceof DelayedBackbufferGraphic) {
      ((DelayedBackbufferGraphic) graphics).init();
    }
    final BufferedImage image =
        ((DelayedBackbufferGraphic) compositingGroup.graphics).image;
    // we may have not found anything to paint, in that case the delegate
    // has not been initialized
    if (image != null) {
      compositingGroup.graphics.dispose();
      Composite composite = compositingGroup.composite;
      if (composite == null) {
        graphics.setComposite(AlphaComposite.SrcOver);
      } else {
        graphics.setComposite(composite);
      }
      graphics.drawImage(image, 0, 0, null);
    }
  }
}

代码示例来源:origin: davidmoten/rtree

public BufferedImage createImage() {
  final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  final Graphics2D g = (Graphics2D) image.getGraphics();
  g.setBackground(Color.white);
  g.clearRect(0, 0, width, height);
  g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f));
  if (tree.root().isPresent()) {
    final List<RectangleDepth> nodeDepths = getNodeDepthsSortedByDepth(tree.root().get());
    drawNode(g, nodeDepths);
  }
  return image;
}

代码示例来源:origin: com.l2fprod.common/l2fprod-common-shared

public void paint(Graphics g) {
 Graphics2D g2d = (Graphics2D)g;
 Composite oldComp = g2d.getComposite();
 Composite alphaComp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
  alpha);
 g2d.setComposite(alphaComp);
 super.paint(g2d);
 g2d.setComposite(oldComp);
}

代码示例来源:origin: ron190/jsql-injection

@Override 
protected void paintComponent(Graphics g) {
  this.tabbedPane.getDropLineRect().ifPresent(rect -> {
    Graphics2D g2 = (Graphics2D) g.create();
    Rectangle r = SwingUtilities.convertRectangle(this.tabbedPane, rect, this);
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
    g2.setPaint(Color.RED);
    g2.fill(r);
    g2.dispose();
  });
}

代码示例来源:origin: graphhopper/graphhopper

public void makeTransparent(Graphics2D g2) {
  Color col = g2.getColor();
  Composite comp = null;
  // force transparence of this layer only. If no buffering we would clear layers below
  if (buffering) {
    comp = g2.getComposite();
    g2.setComposite(AlphaComposite.Clear);
  }
  g2.setColor(new Color(0, 0, 0, 0));
  g2.fillRect(0, 0, bounds.width, bounds.height);
  g2.setColor(col);
  if (comp != null) {
    g2.setComposite(comp);
  }
}

代码示例来源:origin: geotools/geotools

@Override
  public void paintReservedAreas(Graphics2D g2d) {
    g2d.setStroke(new BasicStroke(0.5f));
    g2d.setColor(Color.LIGHT_GRAY);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.XOR));
    for (Object bound : qt.queryAll()) {
      LiteShape ls = new LiteShape((Geometry) bound, new AffineTransform(), false);
      g2d.draw(ls);
    }
  }
}

代码示例来源:origin: stackoverflow.com

BufferedImage createResizedCopy(Image originalImage, 
     int scaledWidth, int scaledHeight, 
     boolean preserveAlpha)
 {
   System.out.println("resizing...");
   int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
   BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
   Graphics2D g = scaledBI.createGraphics();
   if (preserveAlpha) {
     g.setComposite(AlphaComposite.Src);
   }
   g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); 
   g.dispose();
   return scaledBI;
 }

代码示例来源:origin: looly/hutool

@Override
public Image createImage(String code) {
  final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  final Graphics2D g = ImageUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
  
  // 随机画干扰圈圈
  drawInterfere(g);
  
  // 画字符串
  // 抗锯齿
  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g.setFont(font);
  final FontMetrics metrics = g.getFontMetrics();
  final int minY = metrics.getAscent() - metrics.getLeading() - metrics.getDescent();
  int len = code.length();
  int charWidth = width / len;
  for (int i = 0; i < len; i++) {
    // 指定透明度
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
    g.setColor(ImageUtil.randomColor());
    g.drawString(String.valueOf(code.charAt(i)), i * charWidth, RandomUtil.randomInt(minY, this.height));
  }
  
  return image;
}

代码示例来源:origin: marytts/marytts

if (positionCursor != null) {
  int x = positionCursor.getX(this);
  g.setColor(positionCursor.getColor());
  g.drawLine(x, positionCursor.getYMin(this), x, positionCursor.getYMax(this));
if (rangeCursor != null) {
  int x = rangeCursor.getX(this);
  g.setColor(rangeCursor.getColor());
  g.drawLine(x, rangeCursor.getYMin(this), x, rangeCursor.getYMax(this));
  Composite origC = g.getComposite();
  AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
  g.setComposite(ac);
  g.fillRect(positionCursor.getX(this), positionCursor.getYMin(this),
      rangeCursor.getX(this) - positionCursor.getX(this),
      rangeCursor.getYMax(this) - positionCursor.getYMin(this));
  g.setComposite(origC);
  g.setColor(valueLabel.getColor());
  g.drawString(valueLabel.getText(), valueLabel.getX(this), valueLabel.getY(this));

相关文章

Graphics2D类方法