Java Code Examples for java.awt.Graphics2D.getFontMetrics()

The following are Jave code examples for showing how to use getFontMetrics() of the java.awt.Graphics2D class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: parabuild-ci   File: Axis.java   View Source Code Vote up 6 votes
/**
 * Returns a rectangle that encloses the axis label.  This is typically 
 * used for layout purposes (it gives the maximum dimensions of the label).
 *
 * @param g2  the graphics device.
 * @param edge  the edge of the plot area along which the axis is measuring.
 *
 * @return The enclosing rectangle.
 */
protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {

    Rectangle2D result = new Rectangle2D.Double();
    String axisLabel = getLabel();
    if (axisLabel != null && !axisLabel.equals("")) {
        FontMetrics fm = g2.getFontMetrics(getLabelFont());
        Rectangle2D bounds = TextUtilities.getTextBounds(axisLabel, g2, fm);
        RectangleInsets insets = getLabelInsets();
        bounds = insets.createOutsetRectangle(bounds);
        double angle = getLabelAngle();
        if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
            angle = angle - Math.PI / 2.0;
        }
        double x = bounds.getCenterX();
        double y = bounds.getCenterY();
        AffineTransform transformer 
            = AffineTransform.getRotateInstance(angle, x, y);
        Shape labelBounds = transformer.createTransformedShape(bounds);
        result = labelBounds.getBounds2D();
    }

    return result;

}
 
Example 2
Project: brModelo   File: baseDrawerItem.java   View Source Code Vote up 6 votes
private void medidaH(Graphics2D g, int l, int t) {
    FontMetrics fm = g.getFontMetrics();
    String vl = dono.FormateUnidadeMedida(width);
    int xini = l;
    int pre_y = t;
    int xfim = l + width;
    int yfim = t + height / 2;

    int traco = height;
    int ytraco = pre_y;// - (traco/2);

    g.drawLine(xini, ytraco, xini, ytraco + traco);
    g.drawLine(xfim, ytraco, xfim, ytraco + traco);
    g.drawLine(xini, yfim, xfim, yfim);

    xini = xini + (width - fm.stringWidth(vl)) / 2;
    int yini = invertido ? yfim + (fm.getHeight() - fm.getDescent()) : yfim - fm.getDescent();// yfim + (fm.getHeight()) / 2 - fm.getDescent();
    g.drawString(vl, xini, yini);
}
 
Example 3
Project: JavaPPTX   File: PPWindowImage.java   View Source Code Vote up 6 votes
private void drawTitleBar(Graphics2D g) {
   Graphics2D g2 = (Graphics2D) g.create();
   RoundRectangle2D titleBar = createTitleBarShape();
   double h = SF * (TITLE_BAR_HEIGHT + CORNER_RADIUS) + 1;
   g2.setPaint(new GradientPaint(0, 0, grayColor(GRAY_TOP),
                                 0, (float) h, grayColor(GRAY_BOTTOM)));
   g2.fill(titleBar);
   g2.setColor(Color.DARK_GRAY);
   g2.draw(titleBar);
   g2.setColor(Color.BLACK);
   int scaledSize = (int) Math.round(SF * TITLE_FONT_SIZE);
   g2.setFont(Font.decode(TITLE_FONT_FAMILY + "-" + scaledSize));
   FontMetrics fm = g2.getFontMetrics();
   int x = (int) Math.round((sw - fm.stringWidth(title)) / 2);
   int y = (int) Math.round(SF * (TITLE_BAR_HEIGHT / 2 + TITLE_DY));
   g2.drawString(title, x, y);
   drawBall(g2, RED_BALL, 0);
   drawBall(g2, AMBER_BALL, 1);
   drawBall(g2, GREEN_BALL, 2);
   g2.dispose();
}
 
Example 4
Project: geomapapp   File: PoleMapServer.java   View Source Code Vote up 6 votes
private static BufferedImage drawTileName(BufferedImage image, String name) {
	BufferedImage i2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
	Graphics2D g2 = i2.createGraphics();
	g2.drawImage(image, null, 0, 0);
	g2.setColor(Color.red);
	g2.setStroke(new BasicStroke(4));
	g2.drawRect(8, 8, image.getWidth() - 8, image.getHeight() - 8);

	g2.setColor(Color.RED);
	g2.setFont(g2.getFont().deriveFont(Font.BOLD).deriveFont(13f));
	FontMetrics fm = g2.getFontMetrics();
	String[] parts = name.split("/");
	for (int i = 0; i < parts.length; i++) {
		String s = i == parts.length - 1 ? parts[i] : parts[i] + "/"; 
		int x = 40 + i * 5;
		int y = 40 + i * (fm.getHeight() + 5);
		Rectangle2D rect = fm.getStringBounds(s, g2);
		g2.setColor(Color.white);
		g2.fillRect(x, y - fm.getAscent(), (int)rect.getWidth(), fm.getHeight());
		g2.setColor(Color.red);
		g2.drawString(s, x, y);
	}
	return i2;
}
 
Example 5
Project: org.alloytools.alloy   File: Artist.java   View Source Code Vote up 5 votes
/**
 * Allocates the nonbold and bold fonts, then calculates the max ascent and
 * descent.
 */
private static void calc() {
	if (cachedMaxDescent >= 0)
		return; // already done
	BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
	cachedGraphics = (Graphics2D) (image.getGraphics());
	cachedPlainMetrics = cachedGraphics.getFontMetrics(cachedPlainFont = new Font(fontName, Font.PLAIN, fontSize));
	cachedBoldMetrics = cachedGraphics.getFontMetrics(cachedBoldFont = new Font(fontName, Font.BOLD, fontSize));
	cachedGraphics.setFont(cachedPlainFont);
	cachedMaxAscent = cachedPlainMetrics.getMaxAscent();
	cachedMaxDescent = cachedPlainMetrics.getMaxDescent();
}
 
Example 6
Project: powertext   File: SyntaxScheme.java   View Source Code Vote up 5 votes
void refreshFontMetrics(Graphics2D g2d) {
	// It is assumed that any rendering hints are already applied to g2d.
	for (int i=0; i<styles.length; i++) {
		Style s = styles[i];
		if (s!=null) {
			s.fontMetrics = s.font==null ? null :
							g2d.getFontMetrics(s.font);
		}
	}
}
 
Example 7
Project: litiengine   File: GuiComponent.java   View Source Code Vote up 5 votes
public String getTextToRender(final Graphics2D g) {
  if (this.getText() == null) {
    return "";
  }
  final FontMetrics fm = g.getFontMetrics();
  String newText = this.getText();

  while (newText.length() > 1 && fm.stringWidth(newText) >= this.getWidth() - this.getTextXMargin()) {
    newText = newText.substring(1, newText.length());
  }
  return newText;

}
 
Example 8
Project: litiengine   File: GuiComponent.java   View Source Code Vote up 5 votes
private void renderText(Graphics2D g) {
  if (this.getText() == null || this.getText().isEmpty()) {
    return;
  }

  final FontMetrics fm = g.getFontMetrics();

  double defaultTextX;
  double defaultTextY = fm.getAscent() + (this.getHeight() - (fm.getAscent() + fm.getDescent())) / 2;
  switch (this.getTextAlign()) {
  case LEFT:
    defaultTextX = this.getTextXMargin();
    break;
  case RIGHT:
    defaultTextX = this.getWidth() - this.getTextXMargin() - fm.stringWidth(this.getTextToRender(g));
    break;
  case CENTER:
  default:
    defaultTextX = this.getWidth() / 2 - fm.stringWidth(this.getTextToRender(g)) / 2.0;
    break;
  }
  if (this.getTextY() == 0) {
    this.setTextY(defaultTextY);
  }

  if (this.getTextX() == 0) {
    this.setTextX(defaultTextX);
  }
  if (this.getTextAngle() == 0) {
    if (this.drawTextShadow()) {
      RenderEngine.drawTextWithShadow(g, this.getTextToRender(g), this.getX() + this.getTextX(), this.getY() + this.getTextY(), this.getTextShadowColor());
    } else {
      RenderEngine.drawText(g, this.getTextToRender(g), this.getX() + this.getTextX(), this.getY() + this.getTextY());
    }
  } else if (this.getTextAngle() == 90) {
    RenderEngine.drawRotatedText(g, this.getX() + this.getTextX(), this.getY() + this.getTextY() - fm.stringWidth(this.getTextToRender(g)), this.getTextAngle(), this.getTextToRender(g));
  } else {
    RenderEngine.drawRotatedText(g, this.getX() + this.getTextX(), this.getY() + this.getTextY(), this.getTextAngle(), this.getTextToRender(g));
  }
}
 
Example 9
Project: parabuild-ci   File: ValueAxis.java   View Source Code Vote up 5 votes
/**
 * A utility method for determining the width of the widest tick label.
 *
 * @param ticks  the ticks.
 * @param g2  the graphics device.
 * @param drawArea  the area within which the plot and axes should be drawn.
 * @param vertical  a flag that indicates whether or not the tick labels 
 *                  are 'vertical'.
 *
 * @return The width of the tallest tick label.
 */
protected double findMaximumTickLabelWidth(List ticks, 
                                           Graphics2D g2, 
                                           Rectangle2D drawArea, 
                                           boolean vertical) {
                                               
    RectangleInsets insets = getTickLabelInsets();
    Font font = getTickLabelFont();
    double maxWidth = 0.0;
    if (!vertical) {
        FontMetrics fm = g2.getFontMetrics(font);
        Iterator iterator = ticks.iterator();
        while (iterator.hasNext()) {
            Tick tick = (Tick) iterator.next();
            Rectangle2D labelBounds = TextUtilities.getTextBounds(
                    tick.getText(), g2, fm);
            if (labelBounds.getWidth() + insets.getLeft() 
                    + insets.getRight() > maxWidth) {
                maxWidth = labelBounds.getWidth() 
                           + insets.getLeft() + insets.getRight();
            }
        }
    }
    else {
        LineMetrics metrics = font.getLineMetrics("ABCxyz", 
                g2.getFontRenderContext());
        maxWidth = metrics.getHeight() 
                   + insets.getTop() + insets.getBottom();
    }
    return maxWidth;
    
}
 
Example 10
Project: rapidminer   File: ProcessBackgroundImage.java   View Source Code Vote up 5 votes
/**
 * Creates an {@link Image} which displays the given {@link String}.
 *
 * @param text
 *            this text will be displayed in the image
 * @return the image, never {@code null}
 */
private Image createImageFromString(String text) {
	// to know bounds of desired text we need Graphics context so create fake one
	Graphics2D g2 = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics();
	Font font = new Font("Arial", Font.PLAIN, 24);
	g2.setFont(font);
	FontMetrics fm = g2.getFontMetrics();
	// set intermediate width and height so we don't lose original height of background image
	// while loading and/or in error case
	loadingW = fm.stringWidth(text);
	loadingH = fm.getHeight();
	errorW = loadingW;
	errorH = loadingH;
	g2.dispose();

	// create actual image now that text bounds are known
	BufferedImage img = new BufferedImage(loadingW, loadingH, BufferedImage.TYPE_INT_ARGB);
	g2 = img.createGraphics();
	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
	g2.setFont(font);
	fm = g2.getFontMetrics();
	g2.setColor(Colors.TEXT_FOREGROUND);
	g2.drawString(text, 0, fm.getAscent());
	g2.dispose();

	return img;
}
 
Example 11
Project: FreeCol   File: ImageLibrary.java   View Source Code Vote up 5 votes
/**
 * Create a "chip" with the given text and colors.
 *
 * @param g Graphics2D for getting the FontMetrics.
 * @param text The text to display.
 * @param border The border {@code Color}.
 * @param background The background {@code Color}.
 * @param amount How much to fill the chip with the fill color
 * @param fill The fill {@code Color}.
 * @param foreground The foreground {@code Color}.
 * @param filled Whether the chip is filled or not
 * @return A chip.
 */
private BufferedImage createChip(Graphics2D g, String text,
                                 Color border, Color background,
                                 double amount, Color fill,
                                 Color foreground,
                                 Boolean filled) {
    Font font = FontLibrary.createFont(FontLibrary.FontType.SIMPLE,
        FontLibrary.FontSize.TINY, Font.BOLD, scaleFactor);
    FontMetrics fm = g.getFontMetrics(font);
    int padding = (int)(6 * scaleFactor);
    BufferedImage bi = new BufferedImage(fm.stringWidth(text) + padding,
        fm.getMaxAscent() + fm.getMaxDescent() + padding,
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bi.createGraphics();
    g2.setFont(font);
    int width = bi.getWidth();
    int height = bi.getHeight();
    g2.setColor(border);
    g2.fillRect(0, 0, width, height);
    g2.setColor(background);
    g2.fillRect(1, 1, width - 2, height - 2);
    if (filled.equals(false)) {
        if (amount > 0.0 && amount <= 1.0) {
            g2.setColor(fill);
            g2.fillRect(1, 1, width - 2, (int)((height - 2) * amount));
        }
    }
    g2.setColor(foreground);
    g2.drawString(text, padding/2, fm.getMaxAscent() + padding/2);
    g2.dispose();
    return bi;
}
 
Example 12
Project: parabuild-ci   File: ValueAxis.java   View Source Code Vote up 5 votes
/**
 * A utility method for determining the width of the widest tick label.
 *
 * @param ticks  the ticks.
 * @param g2  the graphics device.
 * @param drawArea  the area within which the plot and axes should be drawn.
 * @param vertical  a flag that indicates whether or not the tick labels are 'vertical'.
 *
 * @return the width of the tallest tick label.
 */
protected double findMaximumTickLabelWidth(List ticks, 
                                           Graphics2D g2, 
                                           Rectangle2D drawArea, 
                                           boolean vertical) {
                                               
    Insets insets = getTickLabelInsets();
    Font font = getTickLabelFont();
    double maxWidth = 0.0;
    if (!vertical) {
        FontMetrics fm = g2.getFontMetrics(font);
        Iterator iterator = ticks.iterator();
        while (iterator.hasNext()) {
            Tick tick = (Tick) iterator.next();
            Rectangle2D labelBounds = TextUtilities.getTextBounds(tick.getText(), g2, fm);
            if (labelBounds.getWidth() + insets.left + insets.right > maxWidth) {
                maxWidth = labelBounds.getWidth() + insets.left + insets.right;
            }
        }
    }
    else {
        LineMetrics metrics = font.getLineMetrics("ABCxyz", g2.getFontRenderContext());
        maxWidth = metrics.getHeight() + insets.top + insets.bottom;
    }
    return maxWidth;
    
}
 
Example 13
Project: freecol   File: ImageLibrary.java   View Source Code Vote up 5 votes
/**
 * Create a "chip" with the given text and colors.
 *
 * @param g Graphics2D for getting the FontMetrics.
 * @param text The text to display.
 * @param border The border {@code Color}.
 * @param background The background {@code Color}.
 * @param amount How much to fill the chip with the fill color
 * @param fill The fill {@code Color}.
 * @param foreground The foreground {@code Color}.
 * @param filled Whether the chip is filled or not
 * @return A chip.
 */
private BufferedImage createChip(Graphics2D g, String text,
                                 Color border, Color background,
                                 double amount, Color fill,
                                 Color foreground,
                                 Boolean filled) {
    Font font = FontLibrary.createFont(FontLibrary.FontType.SIMPLE,
        FontLibrary.FontSize.TINY, Font.BOLD, scaleFactor);
    FontMetrics fm = g.getFontMetrics(font);
    int padding = (int)(6 * scaleFactor);
    BufferedImage bi = new BufferedImage(fm.stringWidth(text) + padding,
        fm.getMaxAscent() + fm.getMaxDescent() + padding,
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bi.createGraphics();
    g2.setFont(font);
    int width = bi.getWidth();
    int height = bi.getHeight();
    g2.setColor(border);
    g2.fillRect(0, 0, width, height);
    g2.setColor(background);
    g2.fillRect(1, 1, width - 2, height - 2);
    if (filled.equals(false)) {
        if (amount > 0.0 && amount <= 1.0) {
            g2.setColor(fill);
            g2.fillRect(1, 1, width - 2, (int)((height - 2) * amount));
        }
    }
    g2.setColor(foreground);
    g2.drawString(text, padding/2, fm.getMaxAscent() + padding/2);
    g2.dispose();
    return bi;
}
 
Example 14
Project: parabuild-ci   File: Axis.java   View Source Code Vote up 5 votes
/**
 * Returns a rectangle that encloses the axis label.  This is typically used for layout
 * purposes (it gives the maximum dimensions of the label).
 *
 * @param g2  the graphics device.
 * @param edge  the edge of the plot area along which the axis is measuring.
 *
 * @return The enclosing rectangle.
 */
protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {

    // calculate the width of the axis label...
    Rectangle2D result = new Rectangle2D.Double();
    String axisLabel = getLabel();
    if (axisLabel != null) {
        FontMetrics fm = g2.getFontMetrics(getLabelFont());
        Rectangle2D bounds = TextUtilities.getTextBounds(axisLabel, g2, fm);
        Insets insets = getLabelInsets();
        bounds.setRect(bounds.getX(), bounds.getY(),
                       bounds.getWidth() + insets.left + insets.right,
                       bounds.getHeight() + insets.top + insets.bottom);
        double angle = getLabelAngle();
        if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
            angle = angle - Math.PI / 2.0;
        }
        double x = bounds.getCenterX();
        double y = bounds.getCenterY();
        AffineTransform transformer = AffineTransform.getRotateInstance(angle, x, y);
        Shape labelBounds = transformer.createTransformedShape(bounds);
        result = labelBounds.getBounds2D();
    }

    return result;

}
 
Example 15
Project: parabuild-ci   File: CyclicNumberAxis.java   View Source Code Vote up 5 votes
/**
 * Reserve some space on each axis side because we draw a centered label at each extremity. 
 * 
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param plotArea  the plot area.
 * @param edge  the edge.
 * @param space  the space already reserved.
 * 
 * @return the reserved space.
 */
public AxisSpace reserveSpace(Graphics2D g2, 
                              Plot plot, 
                              Rectangle2D plotArea, 
                              RectangleEdge edge, 
                              AxisSpace space) {
    
    this.internalMarkerCycleBoundTick = null;
    AxisSpace ret = super.reserveSpace(g2, plot, plotArea, edge, space);
    if (this.internalMarkerCycleBoundTick == null) {
        return ret;
    }

    FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
    Rectangle2D r = TextUtilities.getTextBounds(
        this.internalMarkerCycleBoundTick.getText(), g2, fm
    );

    if (RectangleEdge.isTopOrBottom(edge)) {
        if (isVerticalTickLabels()) {
            space.add(r.getHeight() / 2, RectangleEdge.RIGHT);
        }
        else {
            space.add(r.getWidth() / 2, RectangleEdge.RIGHT);
        }
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        if (isVerticalTickLabels()) {
            space.add(r.getWidth() / 2, RectangleEdge.TOP);
        }
        else {
            space.add(r.getHeight() / 2, RectangleEdge.TOP);
        }
    }
    
    return ret;
    
}
 
Example 16
Project: parabuild-ci   File: ValueAxis.java   View Source Code Vote up 5 votes
/**
 * A utility method for determining the height of the tallest tick label.
 *
 * @param ticks  the ticks.
 * @param g2  the graphics device.
 * @param drawArea  the area within which the plot and axes should be drawn.
 * @param vertical  a flag that indicates whether or not the tick labels 
 *                  are 'vertical'.
 *
 * @return The height of the tallest tick label.
 */
protected double findMaximumTickLabelHeight(List ticks,
                                            Graphics2D g2, 
                                            Rectangle2D drawArea, 
                                            boolean vertical) {
                                                
    RectangleInsets insets = getTickLabelInsets();
    Font font = getTickLabelFont();
    double maxHeight = 0.0;
    if (vertical) {
        FontMetrics fm = g2.getFontMetrics(font);
        Iterator iterator = ticks.iterator();
        while (iterator.hasNext()) {
            Tick tick = (Tick) iterator.next();
            Rectangle2D labelBounds = TextUtilities.getTextBounds(
                    tick.getText(), g2, fm);
            if (labelBounds.getWidth() + insets.getTop() 
                    + insets.getBottom() > maxHeight) {
                maxHeight = labelBounds.getWidth() 
                            + insets.getTop() + insets.getBottom();
            }
        }
    }
    else {
        LineMetrics metrics = font.getLineMetrics("ABCxyz", 
                g2.getFontRenderContext());
        maxHeight = metrics.getHeight() 
                    + insets.getTop() + insets.getBottom();
    }
    return maxHeight;
    
}
 
Example 17
Project: Wurst-MC-1.12   File: WurstFont.java   View Source Code Vote up 5 votes
private BufferedImage getFontImage(final char ch, final boolean antiAlias)
{
	final BufferedImage tempfontImage =
		new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
	final Graphics2D g = (Graphics2D)tempfontImage.getGraphics();
	if(antiAlias)
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
			RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	else
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
			RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
	g.setFont(font);
	final FontMetrics fontMetrics = g.getFontMetrics();
	int charwidth = fontMetrics.charWidth(ch) + 8;
	
	if(charwidth <= 0)
		charwidth = 7;
	int charheight = fontMetrics.getHeight() + 3;
	if(charheight <= 0)
		charheight = font.getSize();
	final BufferedImage fontImage = new BufferedImage(charwidth, charheight,
		BufferedImage.TYPE_INT_ARGB);
	final Graphics2D gt = (Graphics2D)fontImage.getGraphics();
	if(antiAlias)
		gt.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
			RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	else
		gt.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
			RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
	gt.setFont(font);
	gt.setColor(Color.WHITE);
	final int charx = 3;
	final int chary = 1;
	gt.drawString(String.valueOf(ch), charx,
		chary + fontMetrics.getAscent());
	
	return fontImage;
	
}
 
Example 18
Project: Pogamut3   File: PoshWidget.java   View Source Code Vote up 4 votes
@Override
protected void paintWidget() {
    Graphics2D g = getGraphics();

    // draw rectanlge
    g.setColor(getType().getColor());
    g.fillRect(0, 0, width, height);


    int collapseWidth = 0;
    // draw collapse block
    if (!getChildNodes().isEmpty()) {
        collapseWidth = arrowWidth;
        g.setColor(getType().getColor().darker());

        if (isCollapsed()) {
            g.fillPolygon(
                    new int[]{width - arrowWidth, width, width - arrowWidth},
                    new int[]{0, height / 2, height},
                    3);
        } else {
            g.fillPolygon(
                    new int[]{width, width - arrowWidth, width},
                    new int[]{0, height / 2, height},
                    3);
        }
    }
    g.setColor(Color.BLACK);
    int xofs = 4;

    int maxTextWidth = width - xofs - collapseWidth;
    // draw headline text
    FontMetrics boldMetrics = g.getFontMetrics(boldFont);
    String renderedHeadlineString = getFittingString(getHeadlineText(), boldMetrics, maxTextWidth);

    g.setFont(boldFont);
    g.drawString(renderedHeadlineString, xofs, boldMetrics.getHeight());

    // draw comment string
    FontMetrics italicMetrics = g.getFontMetrics(italicFont);
    String renderCommentString = getFittingString(getCommentText(), italicMetrics, maxTextWidth);

    g.setFont(italicFont);
    g.drawString(renderCommentString, xofs, boldMetrics.getHeight() + italicMetrics.getHeight());
}
 
Example 19
Project: brModelo   File: baseDrawer.java   View Source Code Vote up 4 votes
public void bordaTopDown(Graphics2D g, boolean isTop) {
        FontMetrics fm = g.getFontMetrics();
        String vl = FormateUnidadeMedida(W);
        int xini = getLeft() + margem;
        int pre_y = (isTop ? getTop() : getTopHeight());
        int xfim = getLeftWidth() - margem;
        int yfim = pre_y + (isTop ? 2 : -2);

        int traco = largTraco < margem ? largTraco : margem;
        traco = (isTop ? traco : -traco);
        int ytraco = yfim;
//        int xini = getLeft() + margem;
//        int pre_y = (isTop ? getTop() : getTopHeight() - margem);
//        int xfim = getLeftWidth() - margem;
//        int yfim = pre_y + margem / 2;
//
//        int traco = largTraco < margem ? largTraco : margem;
//        int ytraco = pre_y + (margem - traco) / 2;

        g.setColor(getCorRegua());
        g.drawLine(xini, ytraco, xini, ytraco + 2 * traco);
        g.drawLine(xfim, ytraco, xfim, ytraco + 2 * traco);
        g.drawLine(xini, yfim, xfim, yfim);

        int blc = calculeSubEspaco(W);
        int sr = xini;
        int dv = modInteiro(blc);
        int subblc = 0;
        if (dv > 0) {
            subblc = blc / dv;
        }
        while (sr < xfim) {
            if (dv > 0) {
                int a = subblc;
                while (a < blc) {
                    if (sr + a < xfim) {
                        g.drawLine(sr + a, ytraco, sr + a, ytraco + traco / 2);
                    }
                    a += subblc;
                }
            }
            sr += blc;
            if (sr < xfim) {
                g.drawLine(sr, ytraco, sr, ytraco + traco);
            }
        }

        if (isMostrarTextoRegua()) {
            g.setColor(getForeColor());
            yfim = pre_y + (isTop ? margem / 2 : -margem / 2);
            xini = xini + (W - fm.stringWidth(vl)) / 2;
            int yini = yfim + (fm.getHeight()) / 2 - fm.getDescent();
            g.drawString(vl, xini, yini);
        }
    }
 
Example 20
Project: parabuild-ci   File: MeterPlot.java   View Source Code Vote up 4 votes
/**
 * Draws a tick on the dial.
 *
 * @param g2  the graphics device.
 * @param meterArea  the meter area.
 * @param value  the tick value.
 * @param label  a flag that controls whether or not a value label is drawn.
 */
protected void drawTick(Graphics2D g2, Rectangle2D meterArea,
                        double value, boolean label) {

    double valueAngle = valueToAngle(value);

    double meterMiddleX = meterArea.getCenterX();
    double meterMiddleY = meterArea.getCenterY();

    g2.setPaint(this.tickPaint);
    g2.setStroke(new BasicStroke(2.0f));

    double valueP2X = 0;
    double valueP2Y = 0;

    double radius = (meterArea.getWidth() / 2) + DEFAULT_BORDER_SIZE;
    double radius1 = radius - 15;

    double valueP1X = meterMiddleX 
            + (radius * Math.cos(Math.PI * (valueAngle / 180)));
    double valueP1Y = meterMiddleY 
            - (radius * Math.sin(Math.PI * (valueAngle / 180)));

    valueP2X = meterMiddleX 
            + (radius1 * Math.cos(Math.PI * (valueAngle / 180)));
    valueP2Y = meterMiddleY 
            - (radius1 * Math.sin(Math.PI * (valueAngle / 180)));

    Line2D.Double line = new Line2D.Double(valueP1X, valueP1Y, valueP2X, 
            valueP2Y);
    g2.draw(line);

    if (this.tickLabelsVisible && label) {

        String tickLabel =  this.tickLabelFormat.format(value);
        g2.setFont(this.tickLabelFont);
        g2.setPaint(this.tickLabelPaint);

        FontMetrics fm = g2.getFontMetrics();
        Rectangle2D tickLabelBounds 
            = TextUtilities.getTextBounds(tickLabel, g2, fm);

        double x = valueP2X;
        double y = valueP2Y;
        if (valueAngle == 90 || valueAngle == 270) {
            x = x - tickLabelBounds.getWidth() / 2;
        }
        else if (valueAngle < 90 || valueAngle > 270) {
            x = x - tickLabelBounds.getWidth();
        }
        if ((valueAngle > 135 && valueAngle < 225) 
                || valueAngle > 315 || valueAngle < 45) {
            y = y - tickLabelBounds.getHeight() / 2;
        }
        else {
            y = y + tickLabelBounds.getHeight() / 2;
        }
        g2.drawString(tickLabel, (float) x, (float) y);
    }
}