java.awt.Graphics2D#getFont ( )源码实例Demo

下面列出了java.awt.Graphics2D#getFont ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Override
public Image getResolutionVariant(double destImageWidth, double destImageHeight) {

    int w = (int) destImageWidth;
    int h = (int) destImageHeight;

    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = img.createGraphics();
    g.scale(scaleX, scaleY);
    int red = (int) (255 / scaleX);
    int green = (int) (250 / scaleX);
    int blue = (int) (20 / scaleX);
    g.setColor(new Color(red, green, blue));
    g.fillRect(0, 0, width, height);

    g.setColor(Color.decode("#87CEFA"));
    Font f = g.getFont();
    g.setFont(new Font(f.getName(), Font.BOLD, 24));
    g.drawString(String.format("scales: [%1.2fx, %1.2fx]", scaleX, scaleY),
            width / 6, height / 2);

    g.dispose();
    return img;
}
 
源代码2 项目: ET_Redux   文件: XAxisOverlayViewLabel.java
/**
 * 
 * @param g2d
 */
@Override
public void paint ( Graphics2D g2d ) {
    paintInit( g2d );

    String label = "Elapsed Seconds:";
    TextLayout mLayout = //
            new TextLayout(
            label, g2d.getFont(), g2d.getFontRenderContext() );

    Rectangle2D bounds = mLayout.getBounds();

    g2d.drawString( label,//
            getWidth() - (float) (bounds.getWidth()) - 2f,//
            (float) mapY( getRangeY_Display() / 2.0 ) + (float)(bounds.getHeight() / 2f) );

}
 
@Override
public void paint(Graphics2D g2d) {
    paintInit(g2d);

    setBackground(new Color(250, 240, 230));

    String label = "for ALL Ratios";
    TextLayout mLayout = //
            new TextLayout(
                    label, g2d.getFont(), g2d.getFontRenderContext());

    Rectangle2D bounds = mLayout.getBounds();

    g2d.drawString(label,//
            10,// (getWidth() - (float) (bounds.getWidth())) / 2f,//
            10);

}
 
源代码4 项目: phoebus   文件: TitlePart.java
public void paint(final Graphics2D gc, final Font font)
{
    final String text = getName();
    if (text.isEmpty())
        return;

    final Font orig_font = gc.getFont();
    gc.setFont(font);
    super.paint(gc);
    
    final Color old_fg = gc.getColor();
    gc.setColor(GraphicsUtils.convert(getColor()));

    final Rectangle bounds = getBounds();
    final Rectangle metrics = GraphicsUtils.measureText(gc, text);
    
    final int tx = bounds.x + (bounds.width - metrics.width) / 2;
    final int ty = bounds.y + metrics.y + (bounds.height - metrics.height) / 2;
    gc.drawString(text, tx, ty);
    gc.setColor(old_fg);
    gc.setFont(orig_font);
}
 
源代码5 项目: lams   文件: CharBox.java
public void draw(Graphics2D g2, float x, float y) {
    drawDebug(g2, x, y);
    AffineTransform at = g2.getTransform();
    g2.translate(x, y);
    Font font = FontInfo.getFont(cf.fontId);

    if (Math.abs(size - TeXFormula.FONT_SCALE_FACTOR) > TeXFormula.PREC) {
        g2.scale(size / TeXFormula.FONT_SCALE_FACTOR,
                 size / TeXFormula.FONT_SCALE_FACTOR);
    }

    if (g2.getFont() != font) {
        g2.setFont(font);
    }

    arr[0] = cf.c;
    g2.drawChars(arr, 0, 1, 0, 0);
    g2.setTransform(at);
}
 
源代码6 项目: mars-sim   文件: LabelMapLayer.java
/**
 * Draws a label to the right of an X, Y location.
 * @param g2d the graphics 2D context.
 * @param label the label string.
 * @param xLoc the X location from center of settlement (meters).
 * @param yLoc the y Location from center of settlement (meters).
 * @param labelColor the color of the label.
 * @param labelOutlineColor the color of the outline of the label.
 * @param xOffset the X pixel offset from the center point.
 * @param yOffset the Y pixel offset from the center point.
 */
private void drawLabelRight(
	Graphics2D g2d, String label, double xLoc, double yLoc,
	Color labelColor, Color labelOutlineColor, int xOffset, int yOffset
) {

	// Save original graphics transforms.
	AffineTransform saveTransform = g2d.getTransform();
	Font saveFont = g2d.getFont();

	// Get the label image.
	Font font = g2d.getFont().deriveFont(Font.BOLD, 12F);
	g2d.setFont(font);
	BufferedImage labelImage = getLabelImage(
		label, font, g2d.getFontRenderContext(),
		labelColor, labelOutlineColor
	);

	// Determine transform information.
	double centerX = labelImage.getWidth() / 2D;
	double centerY = labelImage.getHeight() / 2D;
	double translationX = (-1D * xLoc * mapPanel.getScale()) - centerX;
	double translationY = (-1D * yLoc * mapPanel.getScale()) - centerY;

	// Apply graphic transforms for label.
	AffineTransform newTransform = new AffineTransform(saveTransform);
	newTransform.translate(translationX, translationY);
	newTransform.rotate(mapPanel.getRotation() * -1D, centerX, centerY);
	g2d.setTransform(newTransform);

	// Draw image label.
	int totalRightOffset = (labelImage.getWidth() / 2) + xOffset;
	g2d.drawImage(labelImage, totalRightOffset, yOffset, mapPanel);

	// Restore original graphic transforms.
	g2d.setTransform(saveTransform);
	g2d.setFont(saveFont);
}
 
源代码7 项目: ET_Redux   文件: AbstractDataView.java
protected double calculateLengthOfStringPlot(Graphics2D g2d, String label) {
    TextLayout mLayout
            = new TextLayout(
                    label, g2d.getFont(), g2d.getFontRenderContext());

    Rectangle2D bounds = mLayout.getBounds();
    return bounds.getWidth();
}
 
源代码8 项目: openjdk-8   文件: RenderToCustomBufferTest.java
private static void renderTo(BufferedImage dst) {
    System.out.println("The buffer: " + dst);
    Graphics2D g = dst.createGraphics();

    final int w = dst.getWidth();
    final int h = dst.getHeight();

    g.setColor(Color.blue);
    g.fillRect(0, 0, w, h);

    g.setColor(Color.red);
    Font f = g.getFont();
    g.setFont(f.deriveFont(48f));

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
           RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    // NB: this clip ctriggers the problem
    g.setClip(50, 50, 200, 100);

    g.drawString("AA Text", 52, 90);

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
           RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

    // NB: this clip ctriggers the problem
    g.setClip(50, 100, 100, 100);
    g.drawString("Text", 52, 148);

    g.dispose();
}
 
private static void renderTo(BufferedImage dst) {
    System.out.println("The buffer: " + dst);
    Graphics2D g = dst.createGraphics();

    final int w = dst.getWidth();
    final int h = dst.getHeight();

    g.setColor(Color.blue);
    g.fillRect(0, 0, w, h);

    g.setColor(Color.red);
    Font f = g.getFont();
    g.setFont(f.deriveFont(48f));

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
           RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    // NB: this clip ctriggers the problem
    g.setClip(50, 50, 200, 100);

    g.drawString("AA Text", 52, 90);

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
           RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

    // NB: this clip ctriggers the problem
    g.setClip(50, 100, 100, 100);
    g.drawString("Text", 52, 148);

    g.dispose();
}
 
源代码10 项目: jdk8u60   文件: RenderToCustomBufferTest.java
private static void renderTo(BufferedImage dst) {
    System.out.println("The buffer: " + dst);
    Graphics2D g = dst.createGraphics();

    final int w = dst.getWidth();
    final int h = dst.getHeight();

    g.setColor(Color.blue);
    g.fillRect(0, 0, w, h);

    g.setColor(Color.red);
    Font f = g.getFont();
    g.setFont(f.deriveFont(48f));

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
           RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    // NB: this clip ctriggers the problem
    g.setClip(50, 50, 200, 100);

    g.drawString("AA Text", 52, 90);

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
           RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

    // NB: this clip ctriggers the problem
    g.setClip(50, 100, 100, 100);
    g.drawString("Text", 52, 148);

    g.dispose();
}
 
@Override
public void paint ( Graphics2D g2d ) {
    paintInit( g2d );

    g2d.drawRect( 0, 0, getWidth() - 1, getHeight() - 1 );

    g2d.setFont( new Font(
            "SansSerif",
            Font.BOLD,
            9 ) );
    
    if ( tics != null ) {
        g2d.setStroke( new BasicStroke( 0.3f ) );
        g2d.setPaint( Color.BLACK );
        for (int i = 0; i < tics.length; i ++) {
            try {
                Shape ticMark = new Line2D.Double( //
                        15, mapY( getTics()[i].doubleValue() ), getWidth(), mapY( getTics()[i].doubleValue() ) );
                g2d.draw( ticMark );

                TextLayout mLayout = //
                        new TextLayout(
                        getTics()[i].toPlainString(), g2d.getFont(), g2d.getFontRenderContext() );

                Rectangle2D bounds = mLayout.getBounds();

                g2d.drawString( getTics()[i].toPlainString(),//
                        (float) 13 - (float) bounds.getWidth(),//
                        (float) mapY( getTics()[i].doubleValue() ) + (float) bounds.getHeight() / 2.0f );
            } catch (Exception e) {
            }
        }
    }
}
 
源代码12 项目: osp   文件: XYAxis.java
protected void drawMultiplier(int xpix, int ypix, int exponent, Graphics2D g2) {
  Font oldFont = g2.getFont();
  g2.drawString("10", xpix, ypix);             //$NON-NLS-1$
  g2.setFont(g2.getFont().deriveFont(Font.PLAIN, 9.0f));
  g2.drawString(""+exponent, xpix+16, ypix-6); //$NON-NLS-1$
  g2.setFont(oldFont);
}
 
源代码13 项目: hottub   文件: RenderToCustomBufferTest.java
private static void renderTo(BufferedImage dst) {
    System.out.println("The buffer: " + dst);
    Graphics2D g = dst.createGraphics();

    final int w = dst.getWidth();
    final int h = dst.getHeight();

    g.setColor(Color.blue);
    g.fillRect(0, 0, w, h);

    g.setColor(Color.red);
    Font f = g.getFont();
    g.setFont(f.deriveFont(48f));

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
           RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    // NB: this clip ctriggers the problem
    g.setClip(50, 50, 200, 100);

    g.drawString("AA Text", 52, 90);

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
           RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

    // NB: this clip ctriggers the problem
    g.setClip(50, 100, 100, 100);
    g.drawString("Text", 52, 148);

    g.dispose();
}
 
源代码14 项目: openstock   文件: TextUtils.java
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param text  the text.
 * @param anchor  the anchor point.
 * @param textBounds  the text bounds (if not <code>null</code>, this
 *                    object will be updated by this method to match the
 *                    string bounds).
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor, Rectangle2D textBounds) {

    float[] result = new float[3];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = getTextBounds(text, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    result[2] = -ascent;
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isHorizontalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    if (textBounds != null) {
        textBounds.setRect(bounds);
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;
}
 
源代码15 项目: mars-sim   文件: DisplayRectangular.java
@Override
protected void paintComponent(Graphics g) {
    if (!isInitialized()) {
        return;
    }

    final Graphics2D G2 = (Graphics2D) g.create();

    G2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    G2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    //G2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    //G2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    //G2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    G2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    //G2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    G2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    G2.translate(getInnerBounds().x, getInnerBounds().y);

    // Draw the frame
    if (isFrameVisible()) {
        G2.drawImage(frameImage, 0, 0, null);
    }

    // Draw the background
    if (isBackgroundVisible()) {
        G2.drawImage(backgroundImage, 0, 0, null);
    }

    // Draw LCD display
    if (isLcdVisible()) {
        int offset = 0;
        if (isFrameVisible()) {
            offset = 18;
        }

        G2.drawImage(lcdImage, offset, offset, null);

        if (getLcdColor() == LcdColor.CUSTOM) {
            G2.setColor(getCustomLcdForeground());
        } else {
            G2.setColor(getLcdColor().TEXT_COLOR);
        }
        G2.setFont(getLcdUnitFont());
        final double UNIT_STRING_WIDTH;
        if (isLcdUnitStringVisible()) {
            unitLayout = new TextLayout(getLcdUnitString(), G2.getFont(), RENDER_CONTEXT);
            UNIT_BOUNDARY.setFrame(unitLayout.getBounds());
            G2.drawString(getLcdUnitString(), (int) ((lcdImage.getWidth() - UNIT_BOUNDARY.getWidth()) - lcdImage.getWidth() * 0.03f) + offset, (int) (lcdImage.getHeight() * 0.76f) + offset);
            UNIT_STRING_WIDTH = UNIT_BOUNDARY.getWidth();
        } else {
            UNIT_STRING_WIDTH = 0;
        }
        G2.setFont(getLcdValueFont());
        valueLayout = new TextLayout(formatLcdValue(getLcdValue()), G2.getFont(), RENDER_CONTEXT);
        VALUE_BOUNDARY.setFrame(valueLayout.getBounds());
        G2.drawString(formatLcdValue(getLcdValue()), (int) ((lcdImage.getWidth() - UNIT_STRING_WIDTH - VALUE_BOUNDARY.getWidth()) - lcdImage.getWidth() * 0.09) + offset, (int) (lcdImage.getHeight() * 0.76f) + offset);
    }

    // Draw the foreground
    if (isForegroundVisible()) {
        G2.drawImage(foregroundImage, 0, 0, null);
    }

    if (!isEnabled()) {
        G2.drawImage(disabledImage, 0, 0, null);
    }

    G2.translate(-getInnerBounds().x, -getInnerBounds().y);

    G2.dispose();
}
 
源代码16 项目: buffer_bci   文件: TextUtils.java
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param text  the text.
 * @param anchor  the anchor point.
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor) {

    float[] result = new float[2];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = getTextBounds(text, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isVerticalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;

}
 
源代码17 项目: osp   文件: TextBox.java
public void draw(DrawingPanel panel, Graphics g) {
  String tempText = text; // local reference for thread safety
  if(tempText==null) {
    return;
  }
  Graphics2D g2 = (Graphics2D) g;
  g2.setColor(color);
  Font oldFont = g2.getFont();
  g2.setFont(font);
  FontMetrics fm = g.getFontMetrics();
  int sh = fm.getAscent()+2;           // current string height
  int sw = fm.stringWidth(tempText)+6; // current string width
  boxHeight = Math.max(boxHeight, sh);
  boxWidth = Math.max(boxWidth, sw);
  switch(placement_mode) {
     case PIXEL_PLACEMENT :
       xpix = (int) x;
       ypix = (int) y;
       break;
     case RELATIVE_PLACEMENT :
       xpix = (int) (x*panel.getWidth());
       ypix = (int) ((1-y)*panel.getHeight());
       break;
     case TOP_LEFT_PLACEMENT :
       xpix = 0;
       ypix = 0;
       break;
     case TOP_LEFT_GUTTER_PLACEMENT :
       xpix = panel.getLeftGutter();
       ypix = panel.getTopGutter();
       break;
     case BOTTOM_LEFT_PLACEMENT :
       xpix = 0;
       ypix = panel.getHeight()-boxHeight-yoffset-1;
       break;
     case BOTTOM_LEFT_GUTTER_PLACEMENT :
       xpix = panel.getLeftGutter();
       ypix = panel.getHeight()-boxHeight-yoffset-1-panel.getBottomGutter();
       break;
     case TOP_RIGHT_PLACEMENT :
       xpix = panel.getWidth()-boxWidth-1;
       ypix = 0;
       break;
     case TOP_RIGHT_GUTTER_PLACEMENT :
       xpix = panel.getWidth()-boxWidth-1-panel.getRightGutter();
       ypix = panel.getTopGutter();
       break;
     case BOTTOM_RIGHT_PLACEMENT :
       xpix = panel.getWidth()-boxWidth-1;
       ypix = panel.getHeight()-boxHeight-yoffset-1;
       break;
     case BOTTOM_RIGHT_GUTTER_PLACEMENT :
       xpix = panel.getWidth()-boxWidth-1-panel.getRightGutter();
       ypix = panel.getHeight()-boxHeight-yoffset-1-panel.getBottomGutter();
       break;
     default :
       xpix = panel.xToPix(x);
       ypix = panel.yToPix(y);
       break;
  }
  int xoffset = this.xoffset, yoffset = this.yoffset;
  if(this.alignment_mode==TOP_CENTER_ALIGNMENT) {
    xoffset -= boxWidth/2;
  }
  Shape clipShape = g2.getClip();
  g2.setClip(0, 0, panel.getWidth(), panel.getHeight());
  g2.setColor(Color.yellow);
  g2.fillRect(xpix+xoffset, ypix+yoffset, boxWidth, boxHeight);
  g2.setColor(Color.black);
  g2.drawRect(xpix+xoffset, ypix+yoffset, boxWidth, boxHeight);
  g2.drawString(tempText, xpix+3+xoffset, ypix+boxHeight-2+yoffset);
  g2.setFont(oldFont);
  g2.setClip(clipShape);
}
 
源代码18 项目: phoebus   文件: RTMeter.java
/** Draw needle and label for current value */
private void drawValue(final Graphics2D gc)
{
    gc.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    gc.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    gc.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    gc.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    // Needle
    final double angle = Math.toRadians(scale.getAngle(value));
    final Stroke orig_stroke = gc.getStroke();
    gc.setStroke(AxisPart.TICK_STROKE);

    final int[] nx = new int[]
    {
        (int) (scale.getCenterX() + scale.getRadiusX() * Math.cos(angle) + 0.5),
        (int) (scale.getCenterX() + NEEDLE_BASE * Math.cos(angle + Math.PI/2) + 0.5),
        (int) (scale.getCenterX() + NEEDLE_BASE * Math.cos(angle - Math.PI/2) + 0.5),
    };
    final int[] ny = new int[]
    {
        (int) (scale.getCenterY() - scale.getRadiusY() * Math.sin(angle) + 0.5),
        (int) (scale.getCenterY() - NEEDLE_BASE * Math.sin(angle + Math.PI/2) + 0.5),
        (int) (scale.getCenterY() - NEEDLE_BASE * Math.sin(angle - Math.PI/2) + 0.5),
    };
    gc.setColor(needle);
    gc.fillPolygon(nx, ny, 3);

    gc.setColor(knob);
    gc.fillOval(scale.getCenterX()-NEEDLE_BASE, scale.getCenterY()-NEEDLE_BASE, 2*NEEDLE_BASE, 2*NEEDLE_BASE);

    gc.setStroke(orig_stroke);

    // Label
    gc.setColor(foreground);
    final Font orig_font = gc.getFont();
    gc.setFont(font);
    final Rectangle metrics = GraphicsUtils.measureText(gc, label);
    final Rectangle area_copy = area;
    final int tx = (area_copy.width - metrics.width)/2;
    final int ty = 2*(area_copy.height + metrics.height)/3;
    gc.drawString(label, tx, ty);
    gc.setFont(orig_font);
}
 
源代码19 项目: dsworkbench   文件: FreeForm.java
@Override
public void renderForm(Graphics2D g2d) {
    if (points.size() < 1) {
        return;
    }
    //store properties
    Stroke sBefore = g2d.getStroke();
    Color cBefore = g2d.getColor();
    Composite coBefore = g2d.getComposite();
    Font fBefore = g2d.getFont();
    //draw
    g2d.setStroke(getStroke());
    checkShowMode(g2d, drawColor);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, drawAlpha));

    Point2D.Double pp = MapPanel.getSingleton().virtualPosToSceenPosDouble(points.get(0).getX(), points.get(0).getY());
    GeneralPath p = new GeneralPath();
    p.moveTo(pp.x, pp.y);
    for (int i = 1; i < points.size(); i++) {
        pp = MapPanel.getSingleton().virtualPosToSceenPosDouble(points.get(i).getX(), points.get(i).getY());
        p.lineTo(pp.x, pp.y);
    }
    java.awt.Rectangle mapBounds = MapPanel.getSingleton().getBounds();
    if (mapBounds.intersects(p.getBounds())) {
        setVisibleOnMap(true);
    } else {
        setVisibleOnMap(false);
        return;
    }
    if (filled) {
        g2d.fill(p);
    } else {
        g2d.draw(p);
    }

    if (drawName) {
        g2d.setColor(getTextColor());
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTextAlpha()));
        g2d.setFont(fBefore.deriveFont((float) getTextSize()));
        Rectangle2D textBounds = g2d.getFontMetrics().getStringBounds(getFormName(), g2d);
        java.awt.Rectangle bounds = p.getBounds();
        g2d.drawString(getFormName(), (int) Math.rint(bounds.getX() + bounds.getWidth() / 2 - textBounds.getWidth() / 2), (int) Math.rint(bounds.getY() + bounds.getHeight() / 2 + textBounds.getHeight() / 2));
    }

    //restore properties
    g2d.setStroke(sBefore);
    g2d.setColor(cBefore);
    g2d.setComposite(coBefore);
    g2d.setFont(fBefore);
}
 
源代码20 项目: buffer_bci   文件: TextUtils.java
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param text  the text.
 * @param anchor  the anchor point.
 * @param textBounds  the text bounds (if not <code>null</code>, this
 *                    object will be updated by this method to match the
 *                    string bounds).
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor, Rectangle2D textBounds) {

    float[] result = new float[3];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = getTextBounds(text, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    result[2] = -ascent;
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isHorizontalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    if (textBounds != null) {
        textBounds.setRect(bounds);
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;
}