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

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

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

Graphics2D.setColor介绍

暂无

代码示例

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

private static BufferedImage createImage (int width, int height, Color color) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D g = image.createGraphics();
    g.setColor(color);
    g.fillRect(0, 0, width, height);
    g.dispose();
    return image;
  }
}

代码示例来源: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: libgdx/libgdx

public void draw (BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) {
  g.setColor(color);
  try {
    g.fill(glyph.getShape()); // Java2D fails on some glyph shapes?!
  } catch (Throwable ignored) {
  }
}

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

private void drawImage( SwingUniversalImage img, int centerX, int centerY, double angle, int imageSize ) {
 if ( isDrawingPixelatedImages() && img.isBitmap() ) {
  BufferedImage bi =  img.getAsBitmapForSize( imageSize, imageSize, angle );
  int offx = centerX + xOffset - bi.getWidth() / 2;
  int offy = centerY + yOffset - bi.getHeight() / 2;
  for ( int x = 0; x < bi.getWidth( observer ); x++ ) {
   for ( int y = 0; y < bi.getHeight( observer ); y++ ) {
    int rgb = bi.getRGB( x, y );
    gc.setColor( new Color( rgb ) );
    gc.setStroke( new BasicStroke( 1.0f ) );
    gc.drawLine( offx + x, offy + y, offx + x, offy + y );
   }
  }
 } else {
  gc.setBackground( Color.white );
  gc.clearRect( centerX, centerY, imageSize, imageSize );
  img.drawToGraphics( gc, centerX, centerY, imageSize, imageSize, angle );
 }
}

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

protected void drawGraphic(String randomString, BufferedImage image){
  Graphics2D g = image.createGraphics();
  g.setColor(getRandColor(210, 250));
  g.fillRect(0, 0, WIDTH, HEIGHT);
  for(int i = 0; i < 20; i++){
    color = getRandColor(120, 200);
    g.setColor(color);
    String rand = String.valueOf(charArray[random.nextInt(charArray.length)]);
    g.drawString(rand, random.nextInt(WIDTH), random.nextInt(HEIGHT));
    g.setColor(color);
  g.setColor(color);
  BasicStroke bs = new BasicStroke(3);
  g.setStroke(bs);
  g.draw(curve);
  g.dispose();

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

private void renderPoly(Graphics2D graphics, Color color, Polygon polygon)
  {
    if (polygon != null)
    {
      graphics.setColor(color);
      graphics.setStroke(new BasicStroke(2));
      graphics.draw(polygon);
      graphics.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), 20));
      graphics.fill(polygon);
    }
  }
}

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

/**
 * 创建{@link Graphics2D}
 * 
 * @param image {@link BufferedImage}
 * @param color {@link Color}背景颜色以及当前画笔颜色
 * @return {@link Graphics2D}
 * @since 3.2.3
 */
public static Graphics2D createGraphics(BufferedImage image, Color color) {
  final Graphics2D g = image.createGraphics();
  // 填充背景
  g.setColor(color);
  g.fillRect(0, 0, image.getWidth(), image.getHeight());
  return g;
}

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

Graphics2D g = newImage.createGraphics();
g.setFont(font);
g.setColor(c);
g.setComposite(
    AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)
g.dispose();

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

public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) {
  int w = image.getWidth();
  int h = image.getHeight();
  BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

  Graphics2D g2 = output.createGraphics();

  // This is what we want, but it only does hard-clipping, i.e. aliasing
  // g2.setClip(new RoundRectangle2D ...)

  // so instead fake soft-clipping by first drawing the desired clip shape
  // in fully opaque white with antialiasing enabled...
  g2.setComposite(AlphaComposite.Src);
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g2.setColor(Color.WHITE);
  g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));

  // ... then compositing the image on top,
  // using the white shape from above as alpha source
  g2.setComposite(AlphaComposite.SrcAtop);
  g2.drawImage(image, 0, 0, null);

  g2.dispose();

  return output;
}

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

public void draw (BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) {
  g = (Graphics2D)g.create();
  if (stroke != null)
    g.setStroke(stroke);
  else
    g.setStroke(getStroke());
  g.setColor(color);
  g.draw(glyph.getShape());
  g.dispose();
}

代码示例来源:origin: FudanNLP/fnlp

public static void printTree(Cluster a,String file) {  
  int depth = getDepth(a);
  
  width =  wunit*(depth+1);
  height = hunit*(depth+1);
  BufferedImage image  = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g = image .createGraphics();
  
  g.setColor(new Color(0,0,0)); 
  g.setStroke(new BasicStroke(1)); 
  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  Font font = new Font("宋体", Font.BOLD, 20); 
  
  g.setFont(font);   
  
  drawTree(a, g, width/2, 0 , 1);
  //释放对象 
  g.dispose(); 
  // 保存文件    
  try {
    ImageIO.write(image, "png", new File(file));
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  } 
}

代码示例来源:origin: prestodb/presto

Graphics2D g = image.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
  g.setColor(Color.getHSBColor(relDepth, 1.0f, 0.9f));
  g.fillRect(x1, EXT_PAD, x2 - x1, GRAPH_HEIGHT);
  g.setColor(Color.getHSBColor(relDepth, 1.0f, 0.9f));
  int y1 = HEIGHT * (depth - minDepth) / (maxDepth - minDepth + 1);
  int y2 = HEIGHT * (depth + 1 - minDepth) / (maxDepth - minDepth + 1);
  g.fillRect(0, y1, SCALE_WIDTH, y2 - y1);
  g.setColor(Color.getHSBColor(relDepth, 1.0f, 0.9f));
  g.fillRect(lastX, GRAPH_HEIGHT + EXT_PAD + PAD, w, GRAPH_HEIGHT);
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(2.0f));
g.drawRect(SCALE_WIDTH + EXT_PAD, EXT_PAD, WIDTH - EXT_PAD * 2 - SCALE_WIDTH, GRAPH_HEIGHT);
g.drawRect(SCALE_WIDTH + EXT_PAD, GRAPH_HEIGHT + EXT_PAD + PAD, WIDTH - EXT_PAD * 2 - SCALE_WIDTH, GRAPH_HEIGHT);
g.setStroke(new BasicStroke(1.0f));
g.drawLine(SCALE_WIDTH + EXT_PAD, GRAPH_HEIGHT * 2 + EXT_PAD + PAD + PAD, WIDTH - EXT_PAD, GRAPH_HEIGHT * 2 + EXT_PAD + PAD + PAD);
g.drawLine(SCALE_WIDTH + EXT_PAD, GRAPH_HEIGHT * 2 + EXT_PAD + PAD + PAD - 5, SCALE_WIDTH + EXT_PAD, GRAPH_HEIGHT * 2 + EXT_PAD + PAD + PAD + 5);
g.drawLine(WIDTH - EXT_PAD, GRAPH_HEIGHT * 2 + EXT_PAD + PAD + PAD - 5, WIDTH - EXT_PAD, GRAPH_HEIGHT * 2 + EXT_PAD + PAD + PAD + 5);
g.setColor(Color.BLACK);
g.drawString(labelDense, WIDTH / 2 - 50, 2 * GRAPH_HEIGHT + EXT_PAD + 2 * PAD + 20);

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

private void drawImage( SwingUniversalImage img, int locationX, int locationY, int imageSize ) {
 if ( isDrawingPixelatedImages() && img.isBitmap() ) {
  BufferedImage bi = new BufferedImage( imageSize, imageSize, BufferedImage.TYPE_INT_ARGB );
  Graphics2D g2 = (Graphics2D) bi.getGraphics();
  g2.setColor( Color.WHITE );
  g2.fillRect( 0, 0, imageSize, imageSize );
  g2.drawImage( img.getAsBitmapForSize( imageSize, imageSize ), 0, 0, observer );
  g2.dispose();
  for ( int x = 0; x < bi.getWidth( observer ); x++ ) {
   for ( int y = 0; y < bi.getHeight( observer ); y++ ) {
    int rgb = bi.getRGB( x, y );
    gc.setColor( new Color( rgb ) );
    gc.setStroke( new BasicStroke( 1.0f ) );
    gc.drawLine( locationX + xOffset + x, locationY + yOffset + y, locationX + xOffset + x, locationY
     + yOffset + y );
   }
  }
 } else {
  gc.setBackground( Color.white );
  gc.clearRect( locationX, locationY, imageSize, imageSize );
  img.drawToGraphics( gc, locationX, locationY, imageSize, imageSize );
 }
}

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

g.setColor(Color.white);
g.fillRect(chartX, chartY, chartWidth, chartHeight);
g.setColor(Color.black);
g.drawRect(chartX, chartY, chartWidth, chartHeight);
    int x = (int)(yAxisWidth + chartWidth * percent);
    if (i != 0 && i != xSplit) {
      g.setColor(Color.lightGray);
      g.drawLine(x, chartY + 1, x, chartY + chartHeight);
      g.setColor(Color.black);
    if (isExpanded) g.drawString(label, yAxisWidth - 6 - labelWidth, y + numberHeight / 2);
    if (i != 0 && i != ySplit) {
      g.setColor(Color.lightGray);
      g.drawLine(chartX, y, chartX + chartWidth - 1, y);
      g.setColor(Color.black);
  int x = yAxisWidth + chartWidth / 2 - titleWidth / 2;
  int y = chartY + chartHeight / 2 - numberHeight / 2;
  g.setColor(Color.white);
  g.fillRect(x - 2, y - 2, titleWidth + 4, numberHeight + 4);
  g.setColor(Color.lightGray);
  g.drawString(title, x, y + numberHeight);
g.setColor(Color.blue);
g.setStroke(new BasicStroke(isExpanded ? 3 : 2));
int lastX = -1, lastY = -1;
for (Point point : points) {

代码示例来源:origin: sarxos/webcam-capture

g2.fillRect(0, 0, getWidth(), getHeight());
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.LIGHT_GRAY);
g2.fillRoundRect(cx, cy, 70, 40, 10, 10);
g2.setColor(Color.WHITE);
g2.fillOval(cx + 5, cy + 5, 30, 30);
g2.setColor(Color.LIGHT_GRAY);
g2.fillOval(cx + 10, cy + 10, 20, 20);
g2.setColor(Color.WHITE);
g2.fillOval(cx + 12, cy + 12, 16, 16);
g2.fillRoundRect(cx + 50, cy + 5, 15, 10, 5, 5);
g2.fillRect(cx + 63, cy + 25, 7, 2);
g2.fillRect(cx + 63, cy + 28, 7, 2);
g2.fillRect(cx + 63, cy + 31, 7, 2);
g2.setColor(Color.DARK_GRAY);
g2.setStroke(new BasicStroke(3));
g2.drawLine(0, 0, getWidth(), getHeight());
g2.drawLine(0, getHeight(), getWidth(), 0);
g2.setColor(Color.WHITE);
g2.drawString(str, x, y);

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

/** Extract a ninepatch from a texture atlas, according to the android specification.
 * @see <a href="http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch">ninepatch specification</a>
 * @param page The image file related to the page the region is in
 * @param region The region to extract */
private BufferedImage extractNinePatch (BufferedImage page, Region region, File outputDirFile) {
  BufferedImage splitImage = extractImage(page, region, outputDirFile, NINEPATCH_PADDING);
  Graphics2D g2 = splitImage.createGraphics();
  g2.setColor(Color.BLACK);
  // Draw the four lines to save the ninepatch's padding and splits
  int startX = region.splits[0] + NINEPATCH_PADDING;
  int endX = region.width - region.splits[1] + NINEPATCH_PADDING - 1;
  int startY = region.splits[2] + NINEPATCH_PADDING;
  int endY = region.height - region.splits[3] + NINEPATCH_PADDING - 1;
  if (endX >= startX) g2.drawLine(startX, 0, endX, 0);
  if (endY >= startY) g2.drawLine(0, startY, 0, endY);
  if (region.pads != null) {
    int padStartX = region.pads[0] + NINEPATCH_PADDING;
    int padEndX = region.width - region.pads[1] + NINEPATCH_PADDING - 1;
    int padStartY = region.pads[2] + NINEPATCH_PADDING;
    int padEndY = region.height - region.pads[3] + NINEPATCH_PADDING - 1;
    g2.drawLine(padStartX, splitImage.getHeight() - 1, padEndX, splitImage.getHeight() - 1);
    g2.drawLine(splitImage.getWidth() - 1, padStartY, splitImage.getWidth() - 1, padEndY);
  }
  g2.dispose();
  return splitImage;
}

代码示例来源:origin: kiegroup/optaplanner

g.setStroke(TangoColorFactory.NORMAL_STROKE);
for (Customer customer : solution.getCustomerList()) {
  Location location = customer.getLocation();
  int x = translator.translateLongitudeToX(location.getLongitude());
  int y = translator.translateLatitudeToY(location.getLatitude());
  g.setColor(TangoColorFactory.ALUMINIUM_4);
  g.fillRect(x - 1, y - 1, 3, 3);
  String demandString = Integer.toString(customer.getDemand());
  g.drawString(demandString, x - (g.getFontMetrics().stringWidth(demandString) / 2), y - TEXT_SIZE / 2);
  if (customer instanceof TimeWindowedCustomer) {
    TimeWindowedCustomer timeWindowedCustomer = (TimeWindowedCustomer) customer;
    g.setColor(TangoColorFactory.ALUMINIUM_3);
    int circleX = x - (TIME_WINDOW_DIAMETER / 2);
    int circleY = y + 5;
    if (timeWindowedCustomer.getArrivalTime() != null) {
      if (timeWindowedCustomer.isArrivalAfterDueTime()) {
        g.setColor(TangoColorFactory.SCARLET_2);
      } else if (timeWindowedCustomer.isArrivalBeforeReadyTime()) {
        g.setColor(TangoColorFactory.ORANGE_2);
      } else {
        g.setColor(TangoColorFactory.ALUMINIUM_6);
g.setColor(TangoColorFactory.ALUMINIUM_3);
for (Depot depot : solution.getDepotList()) {
  int x = translator.translateLongitudeToX(depot.getLocation().getLongitude());
  int y = translator.translateLatitudeToY(depot.getLocation().getLatitude());
  g.fillRect(x - 2, y - 2, 5, 5);
  g.drawImage(depotImageIcon.getImage(),

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

public BufferedImage createBufferedImage() {
  final BufferedImage im = new BufferedImage(widthCell * 15, heightCell * 15, BufferedImage.TYPE_INT_RGB);
  final Graphics2D g2d = im.createGraphics();
  g2d.setColor(Color.WHITE);
  g2d.fillRect(0, 0, im.getWidth(), im.getHeight());
  g2d.setColor(Color.BLACK);
  for (ANode n : board.getNodes()) {
    final int x = board.getCol(n) * widthCell;
    final int y = n.getRow() * heightCell;
    g2d.drawString(n.getCode(), x + 5, y + heightCell / 2 - 5);
    g2d.drawOval(x, y, widthCell / 2, heightCell / 2);
  }
  for (ALink link : board.getLinks()) {
    final ANode n1 = link.getNode1();
    final ANode n2 = link.getNode2();
    final int x1 = 10 + board.getCol(n1) * widthCell;
    final int y1 = 10 + n1.getRow() * heightCell;
    final int x2 = 10 + board.getCol(n2) * widthCell;
    final int y2 = 10 + n2.getRow() * heightCell;
    g2d.drawLine(x1, y1, x2, y2);
  }
  return im;
}

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

public static void renderHoverableArea(Graphics2D graphics, Area area, net.runelite.api.Point mousePosition, Color fillColor, Color borderColor, Color borderHoverColor)
{
  if (area != null)
  {
    if (area.contains(mousePosition.getX(), mousePosition.getY()))
    {
      graphics.setColor(borderHoverColor);
    }
    else
    {
      graphics.setColor(borderColor);
    }
    graphics.draw(area);
    graphics.setColor(fillColor);
    graphics.fill(area);
  }
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.lightGray);
g2.fillRect(0, 0, width, height);
g2.setColor(Color.cyan);
g2.drawRect(0, 0, width, height);
g2.setColor(new Color(0, 0, 128));
g2.dispose();

相关文章

Graphics2D类方法