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

The following examples show how to use java.awt.Graphics#getFont() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CallGraphFrame.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
public CallGraphFrame(Pattern urlPattern) {
    // visibility needs to be temporarily set, so we can get the font metrics
    this.setVisible(true);
    Graphics graphics = this.getGraphics();
    Font font = graphics.getFont();
    this.fontmetrics = graphics.getFontMetrics(font);
    this.setVisible(false);

    // now retrieve the call Graph data
    try {
        setupGraph(urlPattern);
        setupFrame();
    } catch (SQLException e) {
        log.error("Failed to setup the graph", e);
    }
}
 
Example 2
Source File: UniTools.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
public static int drawTabbedText(Segment segment, int x, int y, Graphics g, TabExpander e, int startOffset){
    
    List<Segment> segments=new ArrayList<Segment>();
    List<Boolean> unis=new ArrayList<Boolean>();
    getSegments(g.getFont(), segment, segments, unis);
    Font origFont=g.getFont();
    Font uniFont = defaultUniFont.deriveFont(origFont.getStyle(),origFont.getSize2D());
    int ret=x;
    int pos=0;
    for(int i=0;i<segments.size();i++){
        Segment seg=segments.get(i);
        if(unis.get(i)){
            g.setFont(uniFont);
        }else{
            g.setFont(origFont);
        }
        ret = Utilities.drawTabbedText(seg, ret, y, g, e, startOffset+pos);   
        pos += seg.length();
    }
    g.setFont(origFont);
    return ret;         
}
 
Example 3
Source File: GraphicsUtil.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
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 4
Source File: StringTool.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * return the bounds of a Rectangle around a String
 * 
 * @deprecated this ist a dependency to Swing
 */
@Deprecated
public static Rectangle2D getStringBounds(final String s, final Graphics g){
	if (isNothing(s)) {
		return new Rectangle(0, 0);
	}
	FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
	Font fnt = g.getFont();
	Rectangle2D r = fnt.getStringBounds(s, frc);
	return r;
}
 
Example 5
Source File: GraphicsUtil.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
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 6
Source File: PerspectiveFilter.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
public void draw(DrawingPanel panel, Graphics g) {
 	if (!PerspectiveFilter.super.isEnabled()) return;
 	VideoPanel vidPanel = (VideoPanel)panel;
 	Corner[] corners = PerspectiveFilter.this.isEnabled()? outCorners: inCorners;
	for (int i=0; i<4; i++) {
		screenPts[i] = corners[i].getScreenPosition(vidPanel);
     transform.setToTranslation(screenPts[i].getX(), screenPts[i].getY());
     Shape s = corners[i]==selectedCorner? selectionShape: cornerShape;
     Stroke sk = corners[i]==selectedCorner? stroke: cornerStroke;
     hitShapes[i] = transform.createTransformedShape(s);
     drawShapes[i] = sk.createStrokedShape(hitShapes[i]);
	}
path.reset();
path.moveTo((float)screenPts[0].getX(), (float)screenPts[0].getY());
path.lineTo((float)screenPts[1].getX(), (float)screenPts[1].getY());
path.lineTo((float)screenPts[2].getX(), (float)screenPts[2].getY());
path.lineTo((float)screenPts[3].getX(), (float)screenPts[3].getY());
path.closePath();
drawShapes[4] = stroke.createStrokedShape(path);
Graphics2D g2 = (Graphics2D)g;
   Color gcolor = g2.getColor();
   g2.setColor(color);
   Font gfont = g.getFont();
   g2.setFont(font);
   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                      RenderingHints.VALUE_ANTIALIAS_ON);
   for (int i=0; i< drawShapes.length; i++) {
   	g2.fill(drawShapes[i]);
   }
   for (int i=0; i<textLayouts.length; i++) {
     p.setLocation(screenPts[i].getX()-4-font.getSize(), screenPts[i].getY()-6);
   	textLayouts[i].draw(g2, p.x, p.y);
   }
   g2.setFont(gfont);
   g2.setColor(gcolor);
}
 
Example 7
Source File: SyntaxUtilities.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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();

	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;

		tokens = tokens.next;
	}

	return x;
}
 
Example 8
Source File: OCR.java    From SikuliX1 with MIT License 5 votes vote down vote up
private static float getDefaultTextHeight() {
  Graphics g = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB).getGraphics();
  try {
    Font font = g.getFont();
    FontMetrics fm = g.getFontMetrics(font);
    return fm.getLineMetrics("X", g).getHeight();
  } finally {
    g.dispose();
  }
}
 
Example 9
Source File: Text.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void paintGhost(InstancePainter painter) {
	TextAttributes attrs = (TextAttributes) painter.getAttributeSet();
	String text = attrs.getText();
	if (text == null || text.equals(""))
		return;

	int halign = attrs.getHorizontalAlign();
	int valign = attrs.getVerticalAlign();
	Graphics g = painter.getGraphics();
	Font oldFont = g.getFont();
	Color oldColor = g.getColor();
	g.setFont(attrs.getFont());
	g.setColor(painter.getAttributeValue(ATTR_COLOR));
	GraphicsUtil.drawText(g, text, 0, 0, halign, valign);

	String textTrim = text.endsWith(" ") ? text.substring(0, text.length() - 1) : text;
	Bounds newBds;
	if (textTrim.equals("")) {
		newBds = Bounds.EMPTY_BOUNDS;
	} else {
		Rectangle bdsOut = GraphicsUtil.getTextBounds(g, textTrim, 0, 0, halign, valign);
		newBds = Bounds.create(bdsOut).expand(4);
	}
	if (attrs.setOffsetBounds(newBds)) {
		Instance instance = painter.getInstance();
		if (instance != null)
			instance.recomputeBounds();
	}
	g.setFont(oldFont);
	g.setColor(oldColor);
}
 
Example 10
Source File: TextField.java    From Logisim with GNU General Public License v3.0 4 votes vote down vote up
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 11
Source File: MotifBorders.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 12
Source File: PlotBox.java    From opt4j with MIT License 4 votes vote down vote up
/** @serial Indicator that size has been set. */

	// protected boolean _sizeHasBeenSet = false;

	// /////////////////////////////////////////////////////////////////
	// // private methods ////

	/*
	 * Draw the legend in the upper right corner and return the width (in
	 * pixels) used up. The arguments give the upper right corner of the region
	 * where the legend should be placed.
	 */
	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<String> v = _legendStrings.elements();
		Enumeration<Integer> i = _legendDatasets.elements();
		int ypos = ury + spacing;
		int maxwidth = 0;

		while (v.hasMoreElements()) {
			String legend = v.nextElement();

			// NOTE: relies on _legendDatasets having the same num. of entries.
			int dataset = 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 13
Source File: SyntaxView.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected int drawUnselectedText(Graphics graphics, int x, int y, int p0,
        int p1) {
    Graphics2D graphics2D = (Graphics2D) graphics;
    graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            textAAHint);
    Font saveFont = graphics.getFont();
    Color saveColor = graphics.getColor();
    SyntaxDocument doc = (SyntaxDocument) getDocument();
    Segment segment = getLineBuffer();
    // Draw the right margin first, if needed.  This way the text overalys
    // the margin
    if (rightMarginColumn > 0) {
        int m_x = rightMarginColumn * graphics.getFontMetrics().charWidth('m');
        int h = graphics.getFontMetrics().getHeight();
        graphics.setColor(rightMarginColor);
        graphics.drawLine(m_x, y, m_x, y - h);
    }
    try {
        // Colour the parts
        Iterator<Token> i = doc.getTokens(p0, p1);
        int start = p0;
        while (i.hasNext()) {
            Token t = i.next();
            // if there is a gap between the next token start and where we
            // should be starting (spaces not returned in tokens), then draw
            // it in the default type
            if (start < t.start) {
                doc.getText(start, t.start - start, segment);
                x = DEFAULT_STYLE.drawText(segment, x, y, graphics, this, start);
            }
            // t and s are the actual start and length of what we should
            // put on the screen.  assume these are the whole token....
            int l = t.length;
            int s = t.start;
            // ... unless the token starts before p0:
            if (s < p0) {
                // token is before what is requested. adgust the length and s
                l -= (p0 - s);
                s = p0;
            }
            // if token end (s + l is still the token end pos) is greater 
            // than p1, then just put up to p1
            if (s + l > p1) {
                l = p1 - s;
            }
            doc.getText(s, l, segment);
            x = SyntaxStyles.getInstance().drawText(segment, x, y, graphics, this, t);
            start = t.end();
        }
        // now for any remaining text not tokenized:
        if (start < p1) {
            doc.getText(start, p1 - start, segment);
            x = DEFAULT_STYLE.drawText(segment, x, y, graphics, this, start);
        }
    } catch (BadLocationException ex) {
        System.err.println("Requested: " + ex.offsetRequested());
        log.log(Level.SEVERE, null, ex);
    } finally {
        graphics.setFont(saveFont);
        graphics.setColor(saveColor);
    }
    return x;
}
 
Example 14
Source File: MotifGraphicsUtils.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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
Source File: MotifBorders.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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
Source File: MotifGraphicsUtils.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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
Source File: MotifBorders.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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
Source File: MotifGraphicsUtils.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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
Source File: MotifBorders.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 20
Source File: MotifGraphicsUtils.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
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);
    }