Java Code Examples for java.awt.Graphics.getFont()

The following are Jave code examples for showing how to use getFont() of the java.awt.Graphics 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: Equella   File: RootRenderer.java   View Source Code Vote up 7 votes
private void drawCircledText(Graphics g, String text, int centreX, int topY)
{
	// Make some coordinate calculations
	Rectangle2D textSize = g.getFontMetrics().getStringBounds(text, g);
	int halfDiamondY = OVAL_HEIGHT / 2;

	int textStartX = centreX - (int) (textSize.getWidth() / 2);
	int textStartY = topY + halfDiamondY + (int) (textSize.getHeight() / 2);

	int ovalStartX = textStartX - PADDING_SIZE;
	int ovalWidth = (int) textSize.getWidth() + (PADDING_SIZE * 2);

	// Draw the oval around the text
	g.setColor(Color.BLACK);
	g.fillOval(ovalStartX, topY, ovalWidth, OVAL_HEIGHT);

	// Draw our text
	Font backup = g.getFont();
	g.setFont(new Font(backup.getName(), Font.BOLD, backup.getSize()));
	g.setColor(Color.WHITE);
	g.drawString(text, textStartX - 3, textStartY - 3);
	g.setFont(backup);
}
 
Example 2
Project: myfaces-trinidad   File: TextPainter.java   View Source Code Vote up 6 votes
private static void _resetFont(Graphics g)
{
  Font font = g.getFont();

  // We set the font to be the same font family/style as
  // the current font, but a different size (1pt).
  g.setFont(new Font(font.getName(), font.getStyle(), 1));

  // Then, get the font metrics, this seems to reset the
  // graphics state.
  g.getFontMetrics();

  // Finally, reset the original font.  Now, if we get the
  // font metrics again or paint some text, the right font/
  // font metrics will be used.
  g.setFont(font);
}
 
Example 3
Project: java-course   File: BarChartComponent.java   View Source Code Vote up 6 votes
/**
 * Draws numbers on y-axis.
 * 
 * @param g
 *            The {@link Graphics} object.
 */
private void printNumbersY(Graphics g) {
    FontMetrics fm = g.getFontMetrics();
    g.setColor(Color.black);
    Font currentFont = g.getFont();
    g.setFont(new Font(currentFont.toString(), Font.BOLD, currentFont
            .getSize()));

    for (int i = 0; i < hLines; i++) {
        String text = Integer.toString(chart.getyMin() + i
                * chart.getyStep());
        g.drawString(text, xPoint - fm.stringWidth(text) - SPACE,
                rect.height - yPoint - i * hStep + (textHeight / 4));
    }

    g.setFont(currentFont);
}
 
Example 4
Project: java-course   File: BarChartComponent.java   View Source Code Vote up 6 votes
/**
 * Draws numbers on y-axis.
 * 
 * @param g
 *            The {@link Graphics} object.
 */
private void printNumbersX(Graphics g) {
    g.setColor(Color.black);
    Font currentFont = g.getFont();
    g.setFont(new Font(currentFont.toString(), Font.BOLD, currentFont
            .getSize()));

    for (int i = 0; i < vLines; i++) {
        XYValue value = chart.getValues().get(i);
        String text = Integer.toString(value.getX());
        int x = xPoint + vStep / 2 - getTextWidth(text, g) / 2;
        g.drawString(text, x + i * vStep, rect.height - yPoint + SPACE * 2);
    }

    g.setFont(currentFont);
}
 
Example 5
Project: rapidminer   File: SyntaxUtilities.java   View Source Code Vote up 5 votes
/**
 * Paints the specified line onto the graphics context. Note that this method munges the offset
 * and count values of the segment.
 * 
 * @param line
 *            The line segment
 * @param tokens
 *            The token list for the line
 * @param styles
 *            The syntax style list
 * @param expander
 *            The tab expander used to determine tab stops. May be null
 * @param gfx
 *            The graphics context
 * @param x
 *            The x co-ordinate
 * @param y
 *            The y co-ordinate
 * @return The x co-ordinate, plus the width of the painted string
 */
public static int paintSyntaxLine(Segment line, Token tokens, SyntaxStyle[] styles, TabExpander expander, Graphics gfx,
		int x, int y) {
	Font defaultFont = gfx.getFont();
	Color defaultColor = gfx.getColor();

	int offset = 0;
	for (;;) {
		byte id = tokens.id;
		if (id == Token.END) {
			break;
		}

		int length = tokens.length;
		if (id == Token.NULL) {
			if (!defaultColor.equals(gfx.getColor())) {
				gfx.setColor(defaultColor);
			}
			if (!defaultFont.equals(gfx.getFont())) {
				gfx.setFont(defaultFont);
			}
		} else {
			styles[id].setGraphicsFlags(gfx, defaultFont);
		}

		line.count = length;
		x = Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
		line.offset += length;
		offset += length;

		tokens = tokens.next;
	}

	return x;
}
 
Example 6
Project: OpenDA   File: PlotBox.java   View Source Code Vote up 5 votes
private int _drawLegend(Graphics graphics, int urx, int ury) {
    // Ignore if there is no graphics object to draw on.
    if (graphics == null) return 0;

    // FIXME: consolidate all these for efficiency
    Font previousFont = graphics.getFont();
    graphics.setFont(_labelFont);
    int spacing = _labelFontMetrics.getHeight();

    Enumeration v = _legendStrings.elements();
    Enumeration i = _legendDatasets.elements();
    int ypos = ury + spacing;
    int maxwidth = 0;
    while (v.hasMoreElements()) {
        String legend = (String) v.nextElement();
        // NOTE: relies on _legendDatasets having the same num. of entries.
        int dataset = ((Integer) i.nextElement()).intValue();
        if (dataset >= 0) {
            if (_usecolor) {
                // Points are only distinguished up to the number of colors
                int color = dataset % _colors.length;
                graphics.setColor(_colors[color]);
            }
            _drawPoint(graphics, dataset, urx-3, ypos-3, false);

            graphics.setColor(_foreground);
            int width = _labelFontMetrics.stringWidth(legend);
            if (width > maxwidth) maxwidth = width;
            graphics.drawString(legend, urx - 15 - width, ypos);
            ypos += spacing;
        }
    }
    graphics.setFont(previousFont);
    return 22 + maxwidth;  // NOTE: subjective spacing parameter.
}
 
Example 7
Project: geomapapp   File: GeneralUtils.java   View Source Code Vote up 5 votes
/**
 * Place a string in the center of a bounding rectangle (eg a plot), with an optional white box 
 * behind the text
 * @param g
 * @param text
 * @param rect
 * @param font
 * @param xScale
 * @param yScale
 * @param whiteBox
 */
public static void drawCenteredString(Graphics g, String text, Rectangle2D rect, Font font, double xScale, 
		double yScale, boolean whiteBox) {
	// Get the original font
	Font oldFont = g.getFont();
    // Get the FontMetrics
    FontMetrics metrics = g.getFontMetrics(font);
    // Determine the X coordinate for the text
    int x = (int) ((rect.getWidth()* xScale  - metrics.stringWidth(text))  / 2);
    // Determine the Y coordinate for the text (note we add the ascent, as in java 2d 0 is top of the screen)
    int y = (int) ((rect.getHeight() * yScale - metrics.getHeight()) / 2) + metrics.getAscent();
    // Set the font
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(font);
    
    if (whiteBox) {
    	//draw a white box behind the text
	    int textWidth = (int) font.getStringBounds(text, g2.getFontRenderContext()).getWidth() + 2;
	    int textHeight = metrics.getHeight() + 2;
	    Color oldColor = g2.getColor();
	    g2.setColor(Color.white);
	    g2.fillRect(x-1, y-textHeight+4, textWidth, textHeight);
	    g2.setColor(oldColor);
    }
    
    // Draw the String
    g.drawString(text, x , y);
    // Reset font
    g.setFont(oldFont);
}
 
Example 8
Project: geomapapp   File: GeneralUtils.java   View Source Code Vote up 5 votes
/**
 * Place a string in the lower left of a bounding rectangle (eg a plot)
 * @param g
 * @param text
 * @param rect
 * @param font
 * @param xScale
 * @param yScale
 */
public static void drawLowerLeftString(Graphics g, String text, Rectangle2D rect, Font font, double xScale, double yScale, boolean whiteBox) {
	// Get the original font
	Font oldFont = g.getFont();
    // Get the FontMetrics
    FontMetrics metrics = g.getFontMetrics(font);
    // Determine the X coordinate for the text
    int x = 10;
    // Determine the Y coordinate for the text (note we add the ascent, as in java 2d 0 is top of the screen)
    int y = (int) ((rect.getHeight() * yScale - metrics.getHeight()) ) + metrics.getAscent() - 5;
    // Set the font
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(font);
    
    if (whiteBox) {
    	//draw a white box behind the text
	    int textWidth = (int) font.getStringBounds(text, g2.getFontRenderContext()).getWidth() + 2;
	    int textHeight = metrics.getHeight() + 2;
	    Color oldColor = g2.getColor();
	    g2.setColor(Color.white);
	    g2.fillRect(x-1, y-textHeight+4, textWidth, textHeight);
	    g2.setColor(oldColor);
    }
    // Draw the String
    g2.drawString(text, x , y);
    // Reset font
    g2.setFont(oldFont);
}
 
Example 9
Project: Logisim   File: OddParityGate.java   View Source Code Vote up 5 votes
@Override
public void paintIconRectangular(InstancePainter painter) {
	Graphics g = painter.getGraphics();
	g.setColor(Color.black);
	g.drawRect(1, 2, 16, 16);
	Font old = g.getFont();
	g.setFont(old.deriveFont(9.0f));
	GraphicsUtil.drawCenteredText(g, "2k", 9, 6);
	GraphicsUtil.drawCenteredText(g, "+1", 9, 13);
	g.setFont(old);
}
 
Example 10
Project: Logisim   File: GraphicsUtil.java   View Source Code Vote up 5 votes
static public void drawText(Graphics g, Font font, String text, int x, int y, int halign, int valign) {
	Font oldfont = g.getFont();
	if (font != null)
		g.setFont(font);
	drawText(g, text, x, y, halign, valign);
	if (font != null)
		g.setFont(oldfont);
}
 
Example 11
Project: Logisim   File: GraphicsUtil.java   View Source Code Vote up 5 votes
static public Rectangle getTextBounds(Graphics g, Font font, String text, int x, int y, int halign, int valign) {
	if (g == null)
		return new Rectangle(x, y, 0, 0);
	Font oldfont = g.getFont();
	if (font != null)
		g.setFont(font);
	Rectangle ret = getTextBounds(g, text, x, y, halign, valign);
	if (font != null)
		g.setFont(oldfont);
	return ret;
}
 
Example 12
Project: Logisim   File: Canvas.java   View Source Code Vote up 5 votes
private void paintString(Graphics g, String msg) {
	Font old = g.getFont();
	g.setFont(old.deriveFont(Font.BOLD).deriveFont(18.0f));
	FontMetrics fm = g.getFontMetrics();
	int x = (getWidth() - fm.stringWidth(msg)) / 2;
	if (x < 0)
		x = 0;
	g.drawString(msg, x, getHeight() - 23);
	g.setFont(old);
	return;
}
 
Example 13
Project: cuttlefish   File: BasicRenderer.java   View Source Code Vote up 4 votes
public void paintVertex(Graphics g, Vertex v, int x, int y) {

		String label = null;
		if (getLabel() != null) {
//			label = (String) v.getUserDatum(getLabel());
			label = StringLabeller.getLabeller((Graph) v.getGraph()).getLabel(v);
		}

		if (label == null) {
			label = v.toString();
		}

		if (label.length() > 15) {
			label = label.substring(0, 14);
		}

		int nodeSize = mDefaultNodeSize;
		if (mSizeKey != null) {
			Number decoratedNodeSize = (Number) v.getUserDatum(mSizeKey);
			nodeSize =
				(int) Math.ceil(
					decoratedNodeSize.doubleValue() * mNodeSizeScale);
		}

		if (isPicked(v))
			g.setColor(Color.ORANGE);
		else
			g.setColor(Color.RED);

		int labelSize = g.getFontMetrics().stringWidth(label);
		nodeSize = Math.max(nodeSize, 10);
		nodeSize = Math.min(nodeSize, 150);

		g.fillOval(x - nodeSize / 2, y - nodeSize / 2, nodeSize, nodeSize);
		g.setColor(Color.GRAY);
		g.drawOval(x - nodeSize / 2, y - nodeSize / 2, nodeSize, nodeSize);
		g.setColor(Color.BLACK);
		Font font = new Font("Arial", Font.PLAIN, 12);
		Font f = g.getFont();
		g.setFont(font);
		if (nodeSize > labelSize) {
			g.drawString(label, x - labelSize / 2, y + 4);
		} else {
			g.drawString(label, x - labelSize / 2 + 20, y + 15);

		}
		g.setFont(f);
	}
 
Example 14
Project: OpenJSharp   File: MotifGraphicsUtils.java   View Source Code Vote up 4 votes
static void drawStringInRect(JComponent c, Graphics g, String aString,
                                 int x, int y, int width, int height,
                                 int justification) {
        FontMetrics  fontMetrics;
        int          drawWidth, startX, startY, delta;

        if (g.getFont() == null) {
//            throw new InconsistencyException("No font set");
            return;
        }
        fontMetrics = SwingUtilities2.getFontMetrics(c, g);
        if (fontMetrics == null) {
//            throw new InconsistencyException("No metrics for Font " + font());
            return;
        }

        if (justification == CENTER) {
            drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
            if (drawWidth > width) {
                drawWidth = width;
            }
            startX = x + (width - drawWidth) / 2;
        } else if (justification == RIGHT) {
            drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
            if (drawWidth > width) {
                drawWidth = width;
            }
            startX = x + width - drawWidth;
        } else {
            startX = x;
        }

        delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
        if (delta < 0) {
            delta = 0;
        }

        startY = y + height - delta - fontMetrics.getDescent();

        SwingUtilities2.drawString(c, g, aString, startX, startY);
    }
 
Example 15
Project: jdk8u-jdk   File: MotifBorders.java   View Source Code Vote up 4 votes
/**
 * Paints the border for the specified component with the
 * specified position and size.
 * @param c the component for which this border is being painted
 * @param g the paint graphics
 * @param x the x position of the painted border
 * @param y the y position of the painted border
 * @param width the width of the painted border
 * @param height the height of the painted border
 */
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    if (!(c instanceof JPopupMenu)) {
        return;
    }

    Font origFont = g.getFont();
    Color origColor = g.getColor();
    JPopupMenu popup = (JPopupMenu)c;

    String title = popup.getLabel();
    if (title == null) {
        return;
    }

    g.setFont(font);

    FontMetrics fm = SwingUtilities2.getFontMetrics(popup, g, font);
    int         fontHeight = fm.getHeight();
    int         descent = fm.getDescent();
    int         ascent = fm.getAscent();
    Point       textLoc = new Point();
    int         stringWidth = SwingUtilities2.stringWidth(popup, fm,
                                                          title);

    textLoc.y = y + ascent + TEXT_SPACING;
    textLoc.x = x + ((width - stringWidth) / 2);

    g.setColor(background);
    g.fillRect(textLoc.x - TEXT_SPACING, textLoc.y - (fontHeight-descent),
               stringWidth + (2 * TEXT_SPACING), fontHeight - descent);
    g.setColor(foreground);
    SwingUtilities2.drawString(popup, g, title, textLoc.x, textLoc.y);

    MotifGraphicsUtils.drawGroove(g, x, textLoc.y + TEXT_SPACING,
                                  width, GROOVE_HEIGHT,
                                  shadowColor, highlightColor);

    g.setFont(origFont);
    g.setColor(origColor);
}
 
Example 16
Project: jdk8u-jdk   File: MotifGraphicsUtils.java   View Source Code Vote up 4 votes
static void drawStringInRect(JComponent c, Graphics g, String aString,
                                 int x, int y, int width, int height,
                                 int justification) {
        FontMetrics  fontMetrics;
        int          drawWidth, startX, startY, delta;

        if (g.getFont() == null) {
//            throw new InconsistencyException("No font set");
            return;
        }
        fontMetrics = SwingUtilities2.getFontMetrics(c, g);
        if (fontMetrics == null) {
//            throw new InconsistencyException("No metrics for Font " + font());
            return;
        }

        if (justification == CENTER) {
            drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
            if (drawWidth > width) {
                drawWidth = width;
            }
            startX = x + (width - drawWidth) / 2;
        } else if (justification == RIGHT) {
            drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
            if (drawWidth > width) {
                drawWidth = width;
            }
            startX = x + width - drawWidth;
        } else {
            startX = x;
        }

        delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
        if (delta < 0) {
            delta = 0;
        }

        startY = y + height - delta - fontMetrics.getDescent();

        SwingUtilities2.drawString(c, g, aString, startX, startY);
    }
 
Example 17
Project: openjdk-jdk10   File: MotifBorders.java   View Source Code Vote up 4 votes
/**
 * Paints the border for the specified component with the
 * specified position and size.
 * @param c the component for which this border is being painted
 * @param g the paint graphics
 * @param x the x position of the painted border
 * @param y the y position of the painted border
 * @param width the width of the painted border
 * @param height the height of the painted border
 */
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    if (!(c instanceof JPopupMenu)) {
        return;
    }

    Font origFont = g.getFont();
    Color origColor = g.getColor();
    JPopupMenu popup = (JPopupMenu)c;

    String title = popup.getLabel();
    if (title == null) {
        return;
    }

    g.setFont(font);

    FontMetrics fm = SwingUtilities2.getFontMetrics(popup, g, font);
    int         fontHeight = fm.getHeight();
    int         descent = fm.getDescent();
    int         ascent = fm.getAscent();
    Point       textLoc = new Point();
    int         stringWidth = SwingUtilities2.stringWidth(popup, fm,
                                                          title);

    textLoc.y = y + ascent + TEXT_SPACING;
    textLoc.x = x + ((width - stringWidth) / 2);

    g.setColor(background);
    g.fillRect(textLoc.x - TEXT_SPACING, textLoc.y - (fontHeight-descent),
               stringWidth + (2 * TEXT_SPACING), fontHeight - descent);
    g.setColor(foreground);
    SwingUtilities2.drawString(popup, g, title, textLoc.x, textLoc.y);

    MotifGraphicsUtils.drawGroove(g, x, textLoc.y + TEXT_SPACING,
                                  width, GROOVE_HEIGHT,
                                  shadowColor, highlightColor);

    g.setFont(origFont);
    g.setColor(origColor);
}
 
Example 18
Project: openjdk-jdk10   File: MotifGraphicsUtils.java   View Source Code Vote up 4 votes
static void drawStringInRect(JComponent c, Graphics g, String aString,
                                 int x, int y, int width, int height,
                                 int justification) {
        FontMetrics  fontMetrics;
        int          drawWidth, startX, startY, delta;

        if (g.getFont() == null) {
//            throw new InconsistencyException("No font set");
            return;
        }
        fontMetrics = SwingUtilities2.getFontMetrics(c, g);
        if (fontMetrics == null) {
//            throw new InconsistencyException("No metrics for Font " + font());
            return;
        }

        if (justification == CENTER) {
            drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
            if (drawWidth > width) {
                drawWidth = width;
            }
            startX = x + (width - drawWidth) / 2;
        } else if (justification == RIGHT) {
            drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
            if (drawWidth > width) {
                drawWidth = width;
            }
            startX = x + width - drawWidth;
        } else {
            startX = x;
        }

        delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
        if (delta < 0) {
            delta = 0;
        }

        startY = y + height - delta - fontMetrics.getDescent();

        SwingUtilities2.drawString(c, g, aString, startX, startY);
    }
 
Example 19
Project: Logisim   File: TextField.java   View Source Code Vote up 4 votes
public void draw(Graphics g) {
	Font oldFont = g.getFont();
	Color oldColor = g.getColor();
	if (font != null)
		g.setFont(font);
	if (color != null)
		g.setColor(color);
	int x = this.x;
	int y = this.y;
	FontMetrics fm = g.getFontMetrics();
	int width = fm.stringWidth(text);
	int ascent = fm.getAscent();
	int descent = fm.getDescent();
	switch (halign) {
	case TextField.H_CENTER:
		x -= width / 2;
		break;
	case TextField.H_RIGHT:
		x -= width;
		break;
	default:
		break;
	}
	switch (valign) {
	case TextField.V_TOP:
		y += ascent;
		break;
	case TextField.V_CENTER:
		y += ascent / 2;
		break;
	case TextField.V_CENTER_OVERALL:
		y += (ascent - descent) / 2;
		break;
	case TextField.V_BOTTOM:
		y -= descent;
		break;
	default:
		break;
	}
	g.drawString(text, x, y);
	g.setFont(oldFont);
	g.setColor(oldColor);
}
 
Example 20
Project: Logisim   File: SplitterPainter.java   View Source Code Vote up 4 votes
static void drawLabels(ComponentDrawContext context, SplitterAttributes attrs, Location origin) {
	// compute labels
	String[] ends = new String[attrs.fanout + 1];
	int curEnd = -1;
	int cur0 = 0;
	for (int i = 0, n = attrs.bit_end.length; i <= n; i++) {
		int bit = i == n ? -1 : attrs.bit_end[i];
		if (bit != curEnd) {
			int cur1 = i - 1;
			String toAdd;
			if (curEnd <= 0) {
				toAdd = null;
			} else if (cur0 == cur1) {
				toAdd = "" + cur0;
			} else {
				toAdd = cur0 + "-" + cur1;
			}
			if (toAdd != null) {
				String old = ends[curEnd];
				if (old == null) {
					ends[curEnd] = toAdd;
				} else {
					ends[curEnd] = old + "," + toAdd;
				}
			}
			curEnd = bit;
			cur0 = i;
		}
	}

	Graphics g = context.getGraphics().create();
	Font font = g.getFont();
	g.setFont(font.deriveFont(7.0f));

	SplitterParameters parms = attrs.getParameters();
	int x = origin.getX() + parms.getEnd0X() + parms.getEndToSpineDeltaX();
	int y = origin.getY() + parms.getEnd0Y() + parms.getEndToSpineDeltaY();
	int dx = parms.getEndToEndDeltaX();
	int dy = parms.getEndToEndDeltaY();
	if (parms.getTextAngle() != 0) {
		((Graphics2D) g).rotate(Math.PI / 2.0);
		int t;
		t = -x;
		x = y;
		y = t;
		t = -dx;
		dx = dy;
		dy = t;
	}
	int halign = parms.getTextHorzAlign();
	int valign = parms.getTextVertAlign();
	x += (halign == GraphicsUtil.H_RIGHT ? -1 : 1) * (SPINE_WIDTH / 2 + 1);
	y += valign == GraphicsUtil.V_TOP ? 0 : -3;
	for (int i = 0, n = attrs.fanout; i < n; i++) {
		String text = ends[i + 1];
		if (text != null) {
			GraphicsUtil.drawText(g, text, x, y, halign, valign);
		}
		x += dx;
		y += dy;
	}

	g.dispose();
}