Java Code Examples for java.awt.FontMetrics.getHeight()

The following are Jave code examples for showing how to use getHeight() of the java.awt.FontMetrics 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: pdfjumbler   File: PdfList.java   View Source Code Vote up 6 votes
private void drawCenteredText(Graphics g, String message, boolean alignCenter) {
	String[] lines = message.split("\n");
	
	FontMetrics fm = g.getFontMetrics();
	int height = lines.length * fm.getHeight();
	int width = 0;
	if (!alignCenter) {
		for (String line : lines) {
			if (fm.stringWidth(line) > width) {
				width = fm.stringWidth(line);
			}
		}
	}
	
	for (int i = 0; i < lines.length; i++) {
		g.drawString(
			lines[i],
			(getWidth() - (alignCenter ? fm.stringWidth(lines[i]) : width)) / 2,
			(getHeight() - height) / 2 + i*fm.getHeight()
		);
	}
}
 
Example 2
Project: incubator-netbeans   File: LinkButton.java   View Source Code Vote up 6 votes
@Override
public void paint(Graphics g) {
    super.paint(g);
    if( underline && isEnabled() ) {
        Font f = getFont();
        FontMetrics fm = getFontMetrics(f);
        int iconWidth = 0;
        if( null != getIcon() ) {
            iconWidth = getIcon().getIconWidth()+getIconTextGap();
        }
        int x1 = iconWidth;
        int y1 = fm.getHeight();
        int x2 = fm.stringWidth(getText()) + iconWidth;
        if( getText().length() > 0 )
            g.drawLine(x1, y1, x2, y1);
    }
}
 
Example 3
Project: whackpad   File: SwingGui.java   View Source Code Vote up 6 votes
/**
 * Called when a mouse button is released.
 */
public void mouseReleased(MouseEvent e) {
    if (e.getComponent() == this
            && (e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
        int y = e.getY();
        Font font = fileWindow.textArea.getFont();
        FontMetrics metrics = getFontMetrics(font);
        int h = metrics.getHeight();
        int line = y/h;
        if (line == pressLine) {
            fileWindow.toggleBreakPoint(line + 1);
        } else {
            pressLine = -1;
        }
    }
}
 
Example 4
Project: incubator-netbeans   File: SummaryCellRenderer.java   View Source Code Vote up 6 votes
@SuppressWarnings("empty-statement")
private int resizePane (String text, JList list, int lastWidth) {
    if (text == null) {
        text = ""; //NOI18N
    }
    int width = summaryView.getMaster().getComponent().getWidth();
    if (width > 0 && width != lastWidth) {
        String[] rows = text.split("\n"); //NOI18N
        FontMetrics fm = list.getFontMetrics(list.getFont());
        int lines = 0;
        for (String row : rows) {
            Rectangle2D rect = fm.getStringBounds(row, revisionCell.getGraphics());
            lines += (int) (rect.getWidth() / (width - 80) + 1);
        }
        int ph = fm.getHeight() * (lines + 1) + 4;
        revisionCell.setPreferredSize(new Dimension(width - 50 - ICON_COLLAPSED.getIconWidth(), ph));
        setPreferredSize(revisionCell.getPreferredSize());
    }
    return width;
}
 
Example 5
Project: Caritemere   File: GameRenderer.java   View Source Code Vote up 6 votes
public static int getLargestHeight(Graphics g, String text, int font, int size, int wrapWidth) {
	FontMetrics fm = null;
	BitmapFont bmfont = null;
	if (font == Theme.FONT_SMALL) {
		bmfont = BitmapFont.SMALL;
	} else if (font == Theme.FONT_TEXT) {
		bmfont = BitmapFont.MAIN;
	} else  {
		 fm = g.getFontMetrics(MainGui.gui.getTheme().getFont(font));
	}
	String[] lines = text.split(TextFormatting.NEW_LINE);
	int h = 0;
	for (String line : lines) {
		if (bmfont == null) {
			if (fm.stringWidth(line) > wrapWidth)
				h += fm.getHeight();
			h += fm.getHeight();
		} else {
			if(bmfont.getStringWidth(line, size) > wrapWidth)
				h += (bmfont.getCharHeight() + bmfont.getNewLineSpacing()) * size;
			h += (bmfont.getCharHeight() + bmfont.getNewLineSpacing()) * size;
		}
	}

	return h;
}
 
Example 6
Project: marathonv5   File: TransparentFrame.java   View Source Code Vote up 5 votes
protected void paintTransparentFrame() {
    Dimension size = component.getComponent().getSize();
    if (component.getComponent() instanceof JComponent) {
        JComponent jc = (JComponent) component.getComponent();
        jc.paintImmediately(0, 0, size.width, size.height);
    }
    if (disposed) {
        return;
    }
    size = component.getSize();
    Point location = component.getLocation();
    graphics.setColor(BG);
    graphics.fillRect(0 + location.x, 0 + location.y, size.width, size.height);
    String name = component.getRComponentName();
    graphics.setColor(Color.WHITE);
    Font font = new Font(graphics.getFont().getName(), Font.ITALIC | Font.BOLD, 12);
    FontMetrics metrics = graphics.getFontMetrics(font);
    int stringWidth = metrics.stringWidth(name);
    int stringHeight = metrics.getHeight() / 2;
    if (stringWidth < size.width && stringHeight < size.height) {
        graphics.setFont(font);
        graphics.drawString(name, (size.width - stringWidth) / 2 + location.x, (size.height + stringHeight) / 2 + location.y);
    } else if (stringWidth >= size.width || stringHeight >= size.height) {
        graphics.setFont(new Font(graphics.getFont().getName(), Font.ITALIC, 9));
        graphics.drawString(name, 0 + location.x, (size.height + stringHeight) / 2 + location.y);
    } else {
        logger.warning("Not drawing");
    }
}
 
Example 7
Project: incubator-netbeans   File: ListCompletionView.java   View Source Code Vote up 5 votes
private static void renderHtml(ClipboardHistoryElement f, Graphics g, Font defaultFont, Color defaultColor,
int width, int height, boolean selected) {
    int textEnd = width - AFTER_ICON_GAP - AFTER_TEXT_GAP;
    FontMetrics fm = g.getFontMetrics(defaultFont);
    int textY = (height - fm.getHeight())/2 + fm.getHeight() - fm.getDescent();

    HtmlRenderer.renderHTML(f.getNumber() + " " + StringEscapeUtils.escapeHtml(f.getShortenText()), g, 1, textY, textEnd, textY, //NOI18N
            defaultFont, defaultColor, HtmlRenderer.STYLE_TRUNCATE, true);//, selected);
}
 
Example 8
Project: whackpad   File: SwingGui.java   View Source Code Vote up 5 votes
/**
 * Called when a mouse button is pressed.
 */
public void mousePressed(MouseEvent e) {
    Font font = fileWindow.textArea.getFont();
    FontMetrics metrics = getFontMetrics(font);
    int h = metrics.getHeight();
    pressLine = e.getY() / h;
}
 
Example 9
Project: rapidminer   File: ProgressBarUI.java   View Source Code Vote up 5 votes
private void drawString(Graphics2D g2, int w, int h, boolean compressed) {
	if (progressBar.isStringPainted()) {
		// need to reduce font size to fit available space.
		// DO NOT CALL THIS EVERY TIME AS IT'S TREMENDOUSLY EXPENSIVE!!!
		if (compressed && progressBar.getFont().getSize() != 11) {
			progressBar.setFont(progressBar.getFont().deriveFont(11f));
		}
		FontMetrics fontSizer = progressBar.getFontMetrics(progressBar.getFont());

		String displayString = progressBar.getString();
		if (displayString == null || displayString.trim().isEmpty()) {
			return;
		}

		int stringHeight = fontSizer.getHeight();
		int stringWidth = fontSizer.stringWidth(displayString);

		// if string is too wide, cut beginning off until it fits
		while (stringWidth > w * 2) {
			displayString = displayString.substring(0, (int) (displayString.length() * 0.9));
			stringWidth = fontSizer.stringWidth(displayString);
		}

		g2.setColor(Colors.TEXT_FOREGROUND);
		if (compressed) {
			g2.drawString(displayString, (int) Math.max(0, w / 0.33 - w - stringWidth - 5), h - (h - stringHeight) / 2
					- 1);
		} else {
			g2.drawString(displayString, w - stringWidth, h + stringHeight - 1);
		}
	}
}
 
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: incubator-netbeans   File: WinClassicEditorTabDisplayerUI.java   View Source Code Vote up 5 votes
public Dimension getPreferredSize(JComponent c) {
    int prefHeight = 28;
    Graphics g = BasicScrollingTabDisplayerUI.getOffscreenGraphics();
    if (g != null) {
        FontMetrics fm = g.getFontMetrics(displayer.getFont());
        Insets ins = getTabAreaInsets();
        prefHeight = fm.getHeight() + ins.top + ins.bottom + (isGenericUI ? 5 : 6);
    }
    return new Dimension(displayer.getWidth(), prefHeight);
}
 
Example 12
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 13
Project: OpenJSharp   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 14
Project: incubator-netbeans   File: GraphicsTestCase.java   View Source Code Vote up 4 votes
/** Determine if the graphics environment is 24 bit and has standard fonts,
 * so pixel position and color tests will not erroneously fail.  This method is
 * thread safe.
 */
public static boolean canSafelyRunPixelTests() {
    if (graphicsTestsSafe != null) {
        return graphicsTestsSafe.booleanValue();
    }
    
    try {
        boolean result = false;
        if (GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless()) {
            System.err.println("Cannot run test in a headless environment");
            graphicsTestsSafe = Boolean.FALSE;
            return false;
        }
        
        DisplayMode dm =
                GraphicsEnvironment.getLocalGraphicsEnvironment().
                getDefaultScreenDevice().getDisplayMode();
        
        int i = dm.getBitDepth();
        if (i == dm.BIT_DEPTH_MULTI || i >= 16) {
            result = true;
            
            Font f = UIManager.getFont("controlFont");
            if (f == null) {
                f = new JTable().getFont();
            }
            Graphics g = GraphicsEnvironment.getLocalGraphicsEnvironment().
                    getDefaultScreenDevice().getDefaultConfiguration().
                    createCompatibleImage(10,10).getGraphics();
            
            FontMetrics fm = g.getFontMetrics(f);
            if (fm.getHeight() != 16) {
                System.err.println("Cannot run this test - default font size is not " + 16 + " pixels in height - could lead to false fails");
                System.err.println("Basic font size is " + fm.getHeight());
                //Some environments, such as Mandrake linux or Windows with
                //large fonts will supply fonts bigger than the error icon,
                //causing the pixel tests to fail due to icon positioning
                //differences
                result = false;
            }
        }
        if (result) {
            result = tryPrototypePixelTest();
        }
        return result;
    } catch (Exception e) {
        graphicsTestsSafe = Boolean.FALSE;
        e.printStackTrace();
        return false;
    }
}
 
Example 15
Project: incubator-netbeans   File: QuietEditorPane.java   View Source Code Vote up 4 votes
private void setFontHeightWidth(Font font) {
    FontMetrics metrics=getFontMetrics(font);
    fontHeight=metrics.getHeight();
    charWidth=metrics.charWidth('m');
}
 
Example 16
Project: parabuild-ci   File: CyclicNumberAxis.java   View Source Code Vote up 4 votes
/**
 * Draws the tick marks and labels.
 * 
 * @param g2  the graphics device.
 * @param cursor  the cursor.
 * @param plotArea  the plot area.
 * @param dataArea  the area inside the axes.
 * @param edge  the side on which the axis is displayed.
 * 
 * @return The axis state.
 */
protected AxisState drawTickMarksAndLabels(Graphics2D g2, double cursor, 
                                           Rectangle2D plotArea, 
                                           Rectangle2D dataArea, 
                                           RectangleEdge edge) {
    this.internalMarkerWhenTicksOverlap = false;
    AxisState ret = super.drawTickMarksAndLabels(
        g2, cursor, plotArea, dataArea, edge
    );
    
    // continue and separate the labels only if necessary
    if (!this.internalMarkerWhenTicksOverlap) {
        return ret;
    }
    
    double ol = getTickMarkOutsideLength();
    FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
    
    if (isVerticalTickLabels()) {
        ol = fm.getMaxAdvance(); 
    }
    else {
        ol = fm.getHeight();
    }
    
    double il = 0;
    if (isTickMarksVisible()) {
        float xx = (float) valueToJava2D(
            getRange().getUpperBound(), dataArea, edge
        );
        Line2D mark = null;
        g2.setStroke(getTickMarkStroke());
        g2.setPaint(getTickMarkPaint());
        if (edge == RectangleEdge.LEFT) {
            mark = new Line2D.Double(cursor - ol, xx, cursor + il, xx);
        }
        else if (edge == RectangleEdge.RIGHT) {
            mark = new Line2D.Double(cursor + ol, xx, cursor - il, xx);
        }
        else if (edge == RectangleEdge.TOP) {
            mark = new Line2D.Double(xx, cursor - ol, xx, cursor + il);
        }
        else if (edge == RectangleEdge.BOTTOM) {
            mark = new Line2D.Double(xx, cursor + ol, xx, cursor - il);
        }
        g2.draw(mark);
    }
    return ret;
}
 
Example 17
Project: parabuild-ci   File: CyclicNumberAxis.java   View Source Code Vote up 4 votes
protected AxisState drawTickMarksAndLabels(Graphics2D g2, double cursor, Rectangle2D plotArea, 
        Rectangle2D dataArea, RectangleEdge edge) {
    this.internalMarkerWhenTicksOverlap = false;
    AxisState ret = super.drawTickMarksAndLabels(g2, cursor, plotArea, dataArea, edge);
    
    // continue and separate the labels only if necessary
    if (!this.internalMarkerWhenTicksOverlap) {
        return ret;
    }
    
    double ol = getTickMarkOutsideLength();
    FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
    
    if (this.isVerticalTickLabels()) {
        ol = fm.getMaxAdvance(); 
    }
    else {
        ol = fm.getHeight();
    }
    
    double il = 0;
    if (isTickMarksVisible()) {
        float xx = (float) valueToJava2D(getRange().getUpperBound(), dataArea, edge);
        Line2D mark = null;
        g2.setStroke(getTickMarkStroke());
        g2.setPaint(getTickMarkPaint());
        if (edge == RectangleEdge.LEFT) {
            mark = new Line2D.Double(cursor - ol, xx, cursor + il, xx);
        }
        else if (edge == RectangleEdge.RIGHT) {
            mark = new Line2D.Double(cursor + ol, xx, cursor - il, xx);
        }
        else if (edge == RectangleEdge.TOP) {
            mark = new Line2D.Double(xx, cursor - ol, xx, cursor + il);
        }
        else if (edge == RectangleEdge.BOTTOM) {
            mark = new Line2D.Double(xx, cursor + ol, xx, cursor - il);
        }
        g2.draw(mark);
    }
    return ret;
}
 
Example 18
Project: OpenDiabetes   File: Tree.java   View Source Code Vote up 2 votes
/**
 * Method declaration
 *
 *
 * @param f
 */
private static int getMaxHeight(FontMetrics f) {
    return f.getHeight() + 2;
}
 
Example 19
Project: parabuild-ci   File: Tree.java   View Source Code Vote up 2 votes
/**
 * Method declaration
 *
 *
 * @param f
 *
 * @return
 */
private static int getMaxHeight(FontMetrics f) {
    return f.getHeight() + 2;
}
 
Example 20
Project: OpenDiabetes   File: Grid.java   View Source Code Vote up 2 votes
/**
 * Method declaration
 *
 *
 * @param f
 */
private static int getMaxHeight(FontMetrics f) {
    return f.getHeight() + 4;
}