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

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

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

Graphics2D.setFont介绍

暂无

代码示例

代码示例来源:origin: linlinjava/litemall

private void drawTextInImg(BufferedImage baseImage, String textToWrite, int x, int y) {
  Graphics2D g2D = (Graphics2D) baseImage.getGraphics();
  g2D.setColor(new Color(167, 136, 69));
  //TODO 注意,这里的字体必须安装在服务器上
  g2D.setFont(new Font("Microsoft YaHei", Font.PLAIN, 28));
  g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g2D.drawString(textToWrite, x, y);
  g2D.dispose();
}

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

public int getAscent(Font font){
  fakeGraphics.setFont(font);
  FontMetrics metrics = fakeGraphics.getFontMetrics();
  if(DEBUG) System.out.println("Ascent: "+metrics.getAscent());
  return metrics.getAscent();
}

代码示例来源:origin: linlinjava/litemall

private void drawTextInImgCenter(BufferedImage baseImage, String textToWrite, int y) {
  Graphics2D g2D = (Graphics2D) baseImage.getGraphics();
  g2D.setColor(new Color(167, 136, 69));
  String fontName = "Microsoft YaHei";
  Font f = new Font(fontName, Font.PLAIN, 28);
  g2D.setFont(f);
  g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  // 计算文字长度,计算居中的x点坐标
  FontMetrics fm = g2D.getFontMetrics(f);
  int textWidth = fm.stringWidth(textToWrite);
  int widthX = (baseImage.getWidth() - textWidth) / 2;
  // 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
  g2D.drawString(textToWrite, widthX, y);
  // 释放对象
  g2D.dispose();
}

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

fontColor = _defaultColor;
_graphics.setColor(fontColor);
fontTransform.rotate(-ta.getOrientationAngle() * Math.PI / 180);
font = font.deriveFont(fontTransform);
_graphics.setFont(font);
Rectangle2D bounds = _graphics.getFontMetrics().getStringBounds(str, _graphics);
final double yy = bounds.getHeight() + bounds.getY();
double y = 0;
if (ta.getOrientationAngle() == 0) {
  x = -0.5 * ta.getHorizontalAnchor().getFactor() * bounds.getWidth();
  y = 0.5 * ta.getVerticalAnchor().getFactor() * bounds.getHeight() - yy;
  x = pos.x + x;
  y = pos.y + y;
} else {
  x = 0.5 * ta.getVerticalAnchor().getFactor() * bounds.getHeight();
  y = 0.5 * ta.getHorizontalAnchor().getFactor() * bounds.getWidth();
_graphics.drawString(str, (float) x, (float) y);
_graphics.setColor(currentColor);

代码示例来源:origin: knowm/XChart

Rectangle2D annotationRectangle = textLayout.getBounds();
double x = dataPoint.x + dataPoint.w / 2 - annotationRectangle.getWidth() / 2 - MARGIN;
double y = dataPoint.y - 3 * MARGIN - annotationRectangle.getHeight();
double w = annotationRectangle.getWidth() + 2 * MARGIN;
double h = annotationRectangle.getHeight() + 2 * MARGIN;
double halfHeight = h / 2;
 g.setColor(styler.getToolTipHighlightColor());
 g.fill(dataPoint.shape);
g.setColor(styler.getToolTipBackgroundColor());
g.fill(rectangle);
g.setColor(styler.getToolTipBorderColor());
g.draw(rectangle);
g.setColor(styler.getChartFontColor());
g.setFont(styler.getToolTipFont());
AffineTransform orig = g.getTransform();
AffineTransform at = new AffineTransform();

代码示例来源:origin: i2p/i2p.i2p

void drawString(String text, int x, int y, Font font, Paint paint) {
  gd.setFont(font);
  gd.setPaint(paint);
  gd.drawString(text, x, y);
}

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

fontSmall = new Font( "FreeSans", Font.PLAIN, 8 );
gc.setFont( fontGraph );
gc.setColor( background );
gc.fillRect( 0, 0, area.x, area.y );

代码示例来源:origin: kevin-wayne/algs4

/**
 * Write the given text string in the current font, left-aligned at (<em>x</em>, <em>y</em>).
 * @param  x the <em>x</em>-coordinate of the text
 * @param  y the <em>y</em>-coordinate of the text
 * @param  text the text
 */
public static void textLeft(double x, double y, String text) {
  if (text == null) throw new IllegalArgumentException();
  offscreen.setFont(font);
  FontMetrics metrics = offscreen.getFontMetrics();
  double xs = scaleX(x);
  double ys = scaleY(y);
  int hs = metrics.getDescent();
  offscreen.drawString(text, (float) xs, (float) (ys + hs));
  draw();
}

代码示例来源:origin: jenkinsci/jenkins

g2.setFont(getTickLabelFont(tick.getCategory()));
g2.setPaint(getTickLabelPaint(tick.getCategory()));
  r.add(r.getMaxX() + r.getWidth()/2, r.getCenterY());
  r.add(r.getMinX() - r.getWidth()/2, r.getCenterY());
  r.add(r.getCenterX(), r.getMinY() - r.getHeight()/2);
  r.add(r.getCenterX(), r.getMaxX() + r.getHeight()/2);

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

final Color extended = mapper.getMappedColor(fontConfiguration.getExtendedColor());
  if (extended != null) {
    g2d.setColor(extended);
    g2d.setBackground(extended);
    g2d.fill(new Rectangle2D.Double(x, y - dimBack.getHeight() + 1.5, dimBack.getWidth(), dimBack
visible.ensureVisible(x + dimBack.getWidth(), y + 1.5);
g2d.setFont(font.getFont());
g2d.setColor(mapper.getMappedColor(fontConfiguration.getColor()));
final TextLayout t = new TextLayout(shape.getText(), font.getFont(), fontRenderContext);
g2d.translate(x, y);
  final HtmlColor extended = fontConfiguration.getExtendedColor();
  if (extended != null) {
    g2d.setColor(mapper.getMappedColor(extended));
  final FontMetrics fm = g2d.getFontMetrics(font.getFont());
  final int ypos = (int) (y - fm.getDescent() - 0.5);
  final HtmlColor extended = fontConfiguration.getExtendedColor();

代码示例来源:origin: com.github.insubstantial/flamingo

protected void paintBackground(Graphics g) {
  Color main = FlamingoUtilities.getColor(Color.gray,
      "Label.disabledForeground").brighter();
  Graphics2D g2d = (Graphics2D) g.create();
  g2d.setPaint(new GradientPaint(0, 0, FlamingoUtilities.getLighterColor(
      main, 0.9), 0, this.richTooltipPanel.getHeight(),
      FlamingoUtilities.getLighterColor(main, 0.4)));
  g2d.fillRect(0, 0, this.richTooltipPanel.getWidth(),
      this.richTooltipPanel.getHeight());
  g2d.setFont(FlamingoUtilities.getFont(this.richTooltipPanel,
      "Ribbon.font", "Button.font", "Panel.font"));
  g2d.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: robo-code/robocode

public void restore(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setPaint(paint);
    g2.setFont(font);
    g2.setStroke(stroke);
    g2.setTransform(transform);
    g2.setComposite(composite);
    g2.setClip(clip);
    g2.setRenderingHints(renderingHints);
    g2.setColor(color);
    g2.setBackground(background);
  }
}

代码示例来源:origin: martin-grofcik/activiti-crystalball

public SVGProcessDiagramCanvas(int width, int height, int minX, int minY) {
  super(width, height, minX, minY);
  DOMImplementation domImpl =
      GenericDOMImplementation.getDOMImplementation();
  // Create an instance of org.w3c.dom.Document.
  String svgNS = "http://www.w3.org/2000/svg";
  Document document = domImpl.createDocument(svgNS, "svg", null);
  this.g = new SVGGraphics2D( document );
  ((SVGGraphics2D) this.g).setSVGCanvasSize( new Dimension( canvasWidth, canvasHeight));
  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g.setPaint(Color.black);
  Font font = new Font("Arial", Font.BOLD, FONT_SIZE);
  g.setFont(font);
}

代码示例来源:origin: signalapp/BitHub

public static byte[] createFor(String price) throws IOException {
 byte[]        badgeBackground = Resources.toByteArray(Resources.getResource("assets/badge.png"));
 BufferedImage bufferedImage   = ImageIO.read(new ByteArrayInputStream(badgeBackground));
 Graphics2D    graphics        = bufferedImage.createGraphics();
 graphics.setFont(new Font("OpenSans", Font.PLAIN, 34));
 graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
              RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
 graphics.drawString(price + " USD", 86, 45);
 graphics.dispose();
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 ImageIO.write(bufferedImage, "png", baos);
 return baos.toByteArray();
}

代码示例来源:origin: CognitiveJ/cognitivej

private void setupFontHeight(Graphics2D graphics2D, int textContainerWidth, int maxHeightOfText) {
  float fontSize = requestedFont.getSize();
  FontRenderContext fontRenderContext = graphics2D.getFontRenderContext();
  Rectangle2D bounds = requestedFont.getStringBounds(maxSingleLineStringWidth(text), fontRenderContext);
  while (bounds.getWidth() > textContainerWidth || bounds.getHeight() > maxHeightOfText) {
    requestedFont = requestedFont.deriveFont((fontSize -= 2));
    bounds = requestedFont.getStringBounds(maxSingleLineStringWidth(text), fontRenderContext);
  }
  graphics2D.setFont(requestedFont);
}

代码示例来源:origin: magefree/mage

private int calculateOffset(Graphics2D g2d) {
  if (textOffsetX == -1) { // calculate once
    FontRenderContext frc = g2d.getFontRenderContext();
    int textWidth = (int) textFont.getStringBounds(text, frc).getWidth();
    if (textWidth > buttonSize.width) {
      g2d.setFont(textFontMini);
      useMiniFont = true;
      frc = g2d.getFontRenderContext();
      textWidth = (int) textFontMini.getStringBounds(text, frc).getWidth();
    }
    if (alignTextLeft) {
      textOffsetX = 0;
    } else {
      textOffsetX = (imageSize.width - textWidth) / 2;
    }
  }
  return textOffsetX;
}

代码示例来源:origin: opentripplanner/OpenTripPlanner

private void shadowWrite(BufferedImage image, String... strings) {
  Graphics2D g2d = image.createGraphics();
  g2d.setFont(new Font("Sans", Font.PLAIN, 25));
  FontMetrics fm = g2d.getFontMetrics();
  int dy = fm.getHeight();
  int xsize = 0;
  for (String s : strings) {
    int w = fm.stringWidth(s);
    if (w > xsize)
      xsize = w;
  }
  int y = 5;
  int x = 5;
  //g2d.fillRect(x, y, xsize, dy * strings.length + fm.getDescent());
  y += dy;
  for (String s : strings) {
    g2d.setPaint(Color.black);
    g2d.drawString(s, x+1, y+1);
    g2d.setPaint(Color.white);
    g2d.drawString(s, x, y);
    y += dy;
  }
  g2d.dispose();
}

代码示例来源:origin: org.boofcv/visualize

public static void drawLabel( Point2D_F64 locationPixel , String label, Graphics2D g2)
{
  // Draw the ID number approximately in the center
  FontMetrics metrics = g2.getFontMetrics(font);
  Rectangle2D r = metrics.getStringBounds(label,null);
  g2.setColor(Color.ORANGE);
  g2.setFont(font);
  g2.drawString(label,(float)(locationPixel.x-r.getWidth()/2),(float)(locationPixel.y+r.getHeight()/2));
}

代码示例来源:origin: knowm/XChart

pieBounds.getX() + pieBounds.getWidth() / 2 - annotationRectangle.getWidth() / 2;
double yCenter =
  pieBounds.getY() + pieBounds.getHeight() / 2 + annotationRectangle.getHeight() / 2;
double angle = (arcAngle + startAngle) - arcAngle / 2;
double xOffset =
  xCenter
    + Math.cos(Math.toRadians(angle))
      * (pieBounds.getWidth() / 2 * pieStyler.getAnnotationDistance());
double yOffset =
  yCenter
    - Math.sin(Math.toRadians(angle))
      * (pieBounds.getHeight() / 2 * pieStyler.getAnnotationDistance());
 g.setColor(pieStyler.getChartFontColor());
 g.setFont(pieStyler.getAnnotationsFont());
 AffineTransform orig = g.getTransform();
 AffineTransform at = new AffineTransform();

相关文章

Graphics2D类方法