Java Code Examples for java.awt.Graphics#getFontMetrics()
The following examples show how to use
java.awt.Graphics#getFontMetrics() .
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: Figure.java From hottub with GNU General Public License v2.0 | 6 votes |
public int getWidth() { if (widthCash == -1) { int max = 0; BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.setFont(diagram.getFont()); FontMetrics metrics = g.getFontMetrics(); for (String s : lines) { int cur = metrics.stringWidth(s); if (cur > max) { max = cur; } } widthCash = max + INSET; } return widthCash; }
Example 2
Source File: MultilayerPerceptron.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * This will draw the node id to the graphics context. * @param g The graphics context. * @param w The width of the drawing area. * @param h The height of the drawing area. */ public void drawNode(Graphics g, int w, int h) { if ((m_type & PURE_INPUT) == PURE_INPUT) { g.setColor(Color.green); } else { g.setColor(Color.orange); } FontMetrics fm = g.getFontMetrics(); int l = (int)(m_x * w) - fm.stringWidth(m_id) / 2; int t = (int)(m_y * h) - fm.getHeight() / 2; g.fill3DRect(l, t, fm.stringWidth(m_id) + 4 , fm.getHeight() + fm.getDescent() + 4 , true); g.setColor(Color.black); g.drawString(m_id, l + 2, t + fm.getHeight() + 2); }
Example 3
Source File: CompositionArea.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
private Rectangle getCaretRectangle(TextHitInfo caret) { int caretLocation = 0; TextLayout layout = composedTextLayout; if (layout != null) { caretLocation = Math.round(layout.getCaretInfo(caret)[0]); } Graphics g = getGraphics(); FontMetrics metrics = null; try { metrics = g.getFontMetrics(); } finally { g.dispose(); } return new Rectangle(TEXT_ORIGIN_X + caretLocation, TEXT_ORIGIN_Y - metrics.getAscent(), 0, metrics.getAscent() + metrics.getDescent()); }
Example 4
Source File: SyntaxStyle.java From visualvm with GNU General Public License v2.0 | 6 votes |
/** * Draw text. This can directly call the Utilities.drawTabbedText. * Sub-classes can override this method to provide any other decorations. * @param segment - the source of the text * @param x - the X origin >= 0 * @param y - the Y origin >= 0 * @param graphics - the graphics context * @param e - how to expand the tabs. If this value is null, tabs will be * expanded as a space character. * @param startOffset - starting offset of the text in the document >= 0 * @return */ public int drawText(Segment segment, int x, int y, Graphics graphics, TabExpander e, int startOffset) { graphics.setFont(graphics.getFont().deriveFont(getFontStyle())); FontMetrics fontMetrics = graphics.getFontMetrics(); int a = fontMetrics.getAscent(); int h = a + fontMetrics.getDescent(); int w = Utilities.getTabbedTextWidth(segment, fontMetrics, 0, e, startOffset); int rX = x - 1; int rY = y - a; int rW = w + 2; int rH = h; if ((getFontStyle() & 0x10) != 0) { graphics.setColor(Color.decode("#EEEEEE")); graphics.fillRect(rX, rY, rW, rH); } graphics.setColor(getColor()); x = Utilities.drawTabbedText(segment, x, y, graphics, e, startOffset); if ((getFontStyle() & 0x8) != 0) { graphics.setColor(Color.RED); graphics.drawRect(rX, rY, rW, rH); } return x; }
Example 5
Source File: SessionBean.java From tn5250j with GNU General Public License v2.0 | 6 votes |
private static Dimension deriveOptimalSize(JComponent comp, Font f , Border brdr, int nrChars , int nrLines) { if (comp == null) return null; FontMetrics fm = null; Graphics g = comp.getGraphics(); if (g != null) fm = g.getFontMetrics(f); else fm = comp.getFontMetrics(f); Insets insets = (brdr == null) ? new Insets(0, 0, 0, 0) : brdr.getBorderInsets(comp); int height = (fm.getHeight() * nrLines) + insets.top+ insets.bottom; int width = (nrChars * fm.charWidth('M')) + insets.left + insets.right; return new Dimension(width + 2, height); }
Example 6
Source File: CallGraphFrame.java From zap-extensions with Apache License 2.0 | 6 votes |
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 7
Source File: TextTip.java From JAVA-MVC-Swing-Monopoly with Apache License 2.0 | 6 votes |
/** * * �������� * */ private void drawSting(Graphics g) { FontMetrics fm = g.getFontMetrics(); String str = this.textTip.getTipString(); int maxSize = 13; int posY = 32; int front = 0; int rear = maxSize; while (front < str.length() - 1) { if (rear >= str.length()) { rear = str.length() - 1; } char[] temp = new char[maxSize]; str.getChars(front, rear, temp, 0); // Char[] ת����string String s = new String(temp); g.drawString(s, pointWindow.x + 20, pointWindow.y + posY); front = rear; rear += maxSize; posY += 20; } }
Example 8
Source File: MappingTable.java From netbeans with Apache License 2.0 | 6 votes |
/** * When paint is first invoked, we set the rowheight based on the * size of the font. */ @Override public void paint(Graphics g) { if (fontChanged) { fontChanged = false; int height = 0; FontMetrics fm = g.getFontMetrics(getFont()); height = fm.getHeight() + margin; if (height > rowHeight) { rowHeight = height; } //triggers paint, just return afterwards this.setRowHeight(rowHeight); return; } super.paint(g); }
Example 9
Source File: Blink.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
@Override public void paint(Graphics g) { int fontSize = g.getFont().getSize(); int x = 0, y = fontSize, space; int red = (int) (50 * Math.random()); int green = (int) (50 * Math.random()); int blue = (int) (256 * Math.random()); Dimension d = getSize(); g.setColor(Color.black); FontMetrics fm = g.getFontMetrics(); space = fm.stringWidth(" "); for (StringTokenizer t = new StringTokenizer(labelString); t.hasMoreTokens();) { String word = t.nextToken(); int w = fm.stringWidth(word) + space; if (x + w > d.width) { x = 0; y += fontSize; //move word to next line if it doesn't fit } if (Math.random() < 0.5) { g.setColor(new java.awt.Color((red + y * 30) % 256, (green + x / 3) % 256, blue)); } else { g.setColor(getBackground()); } g.drawString(word, x, y); x += w; //shift to the right to draw the next word } }
Example 10
Source File: DitherTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Override public void paint(Graphics g) { int w = getSize().width; int h = getSize().height; if (img == null) { super.paint(g); g.setColor(Color.black); FontMetrics fm = g.getFontMetrics(); int x = (w - fm.stringWidth(calcString)) / 2; int y = h / 2; g.drawString(calcString, x, y); } else { g.drawImage(img, 0, 0, w, h, this); } }
Example 11
Source File: About.java From Logisim with GNU General Public License v3.0 | 5 votes |
private void drawText(Graphics g, int x, int y) { FontMetrics fm; String str; g.setColor(headerColor); g.setFont(headerFont); g.drawString("Logisim", x, y + 45); fm = g.getFontMetrics(); g.setFont(versionFont); g.drawString(Main.VERSION_NAME, fm.stringWidth("Logisim") + x + 10, y + 45); g.setFont(copyrightFont); fm = g.getFontMetrics(); str = "\u00a9 " + Main.COPYRIGHT_YEAR; g.drawString(str, IMAGE_WIDTH - fm.stringWidth(str) - x, y + 6); }
Example 12
Source File: Blink.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override public void paint(Graphics g) { int fontSize = g.getFont().getSize(); int x = 0, y = fontSize, space; int red = (int) (50 * Math.random()); int green = (int) (50 * Math.random()); int blue = (int) (256 * Math.random()); Dimension d = getSize(); g.setColor(Color.black); FontMetrics fm = g.getFontMetrics(); space = fm.stringWidth(" "); for (StringTokenizer t = new StringTokenizer(labelString); t.hasMoreTokens();) { String word = t.nextToken(); int w = fm.stringWidth(word) + space; if (x + w > d.width) { x = 0; y += fontSize; //move word to next line if it doesn't fit } if (Math.random() < 0.5) { g.setColor(new java.awt.Color((red + y * 30) % 256, (green + x / 3) % 256, blue)); } else { g.setColor(getBackground()); } g.drawString(word, x, y); x += w; //shift to the right to draw the next word } }
Example 13
Source File: SwitcherTable.java From netbeans with Apache License 2.0 | 5 votes |
/** * Calculate the height of rows based on the current font. This is done * when the first paint occurs, to ensure that a valid Graphics object is * available. * * @since 1.25 */ private void calcRowHeight(Graphics g) { Font f = getFont(); FontMetrics fm = g.getFontMetrics(f); // As icons are displayed use maximum from font and icon height int rowHeight = Math.max(fm.getHeight(), 16) + 4; needCalcRowHeight = false; setRowHeight(rowHeight); }
Example 14
Source File: GtkEditorTabDisplayerUI.java From netbeans with Apache License 2.0 | 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 + 12; } return new Dimension(displayer.getWidth(), prefHeight); }
Example 15
Source File: AWTFontCalibration.java From phoebus with Eclipse Public License 1.0 | 4 votes |
@Override public void run() { Logger.getLogger("").setLevel(Level.CONFIG); for (Handler handler : Logger.getLogger("").getHandlers()) handler.setLevel(Level.CONFIG); final double factor = getCalibrationFactor(); final Frame frame = new Frame("Java AWT: Calibration factor " + factor); frame.setSize(text_width, text_height); // Would like to use TextField or Label, but: // "Peered AWT components, such as Label and TextField, // can only use logical fonts." (Javadoc for 'Font') // Sure enough at least on Windows the font family is // ignored, only the style and size are honored // by Label.setFont() or TextField.setFont() // --> Use canvas and draw the text with font. final Canvas text = new Canvas() { private static final long serialVersionUID = 1L; @Override public void paint(final Graphics gc) { super.paint(gc); gc.setFont(font); final FontMetrics metrics = gc.getFontMetrics(); // drawString x/y is 'baseline' of text final int y = metrics.getLeading() + metrics.getAscent(); gc.drawString(FontCalibration.TEXT, 0, y); // Show baseline and 'leading' gc.setColor(Color.RED); gc.drawLine(0, y, text_width, y); gc.setColor(Color.GREEN); gc.drawLine(0, metrics.getLeading(), text_width, metrics.getLeading()); } }; text.setSize(text_width, text_height); frame.add(text); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent windowEvent) { System.exit(0); } }); frame.pack(); frame.setVisible(true); if (Math.abs(factor - 1.0) > 0.01) System.err.println("Calibration is not 1.0 but " + factor); }
Example 16
Source File: PCheckBox.java From PolyGlot with MIT License | 4 votes |
@Override public void paint(Graphics g) { boolean enabled = this.isEnabled(); Color selected = VisualStyleManager.getCheckBoxSelected(enabled, nightMode); Color backGround = VisualStyleManager.getCheckBoxBG(enabled, nightMode); Color outline = VisualStyleManager.getCheckBoxOutline(enabled, nightMode); Color hover = VisualStyleManager.getCheckBoxHover(enabled, nightMode); Color click = VisualStyleManager.getCheckBoxClicked(enabled, nightMode); Color fieldBack = VisualStyleManager.getCheckBoxFieldBack(enabled, nightMode); int rounding = PGTUtil.CHECKBOX_ROUNDING; int thisHeight = this.getHeight(); if (this.hasFocus()) { g.setColor(selected); g.drawRect(0, 0, this.getWidth() - 1, thisHeight - 1); } ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (enabled) { if (mouseOver) { g.setColor(hover); g.drawRoundRect(3, 3, thisHeight - 6, thisHeight - 6, rounding, rounding); } if (clicked) { backGround = click; } } g.setColor(outline); g.drawRoundRect(4, 4, thisHeight - 8, thisHeight - 8, rounding, rounding); g.setColor(backGround); g.drawRoundRect(5, 5, thisHeight - 10, thisHeight - 10, rounding, rounding); g.setColor(fieldBack); g.fillRect(6, 6, thisHeight - 11, thisHeight - 11); g.setColor(selected); if (this.isSelected()) { g.fillRect(7, 7, thisHeight - 14, thisHeight - 14); } g.setColor(outline); char[] text = getText().toCharArray(); FontMetrics fm = g.getFontMetrics(getFont()); Rectangle2D rec = fm.getStringBounds(getText(), g); int stringH = (int) Math.round(rec.getHeight()); g.drawChars(text, 0, text.length, thisHeight, thisHeight/2 + stringH/3); }
Example 17
Source File: AbstractFormatterFactoryEditor.java From netbeans with Apache License 2.0 | 4 votes |
@Override public void paintValue(Graphics g, Rectangle rectangle) { String msg = NbBundle.getMessage(AbstractFormatterFactoryEditor.class, "MSG_AbstractFormatterFactory"); // NOI18N FontMetrics fm = g.getFontMetrics(); g.drawString(msg, rectangle.x, rectangle.y + (rectangle.height - fm.getHeight())/2 + fm.getAscent()); }
Example 18
Source File: VerticalLabelUI.java From pdfxtk with Apache License 2.0 | 4 votes |
public void paint(Graphics g, JComponent c) { JLabel label = (JLabel)c; String text = label.getText(); Icon icon = label.isEnabled() ? label.getIcon() : label.getDisabledIcon(); if(icon == null && text == null) return; FontMetrics fm = g.getFontMetrics(); paintViewInsets = c.getInsets(paintViewInsets); paintViewR.x = paintViewInsets.top; paintViewR.y = paintViewInsets.left; paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom); paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right); paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0; paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0; String clippedText = layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR); Graphics2D g2 = (Graphics2D)g; AffineTransform tr = g2.getTransform(); if(clockwise) { g2.rotate(Math.PI / 2); g2.translate(0, - c.getWidth()); } else { g2.rotate(-Math.PI / 2); g2.translate(-c.getHeight(), 0); } if (icon != null) { icon.paintIcon(c, g2, paintIconR.x, paintIconR.y); } if (text != null) { View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { v.paint(g2, paintTextR); } else { int textX = paintTextR.x; int textY = paintTextR.y + fm.getAscent(); if (label.isEnabled()) paintEnabledText(label, g2, clippedText, textX, textY); else paintDisabledText(label, g2, clippedText, textX, textY); } } }
Example 19
Source File: TextFieldCaret.java From Logisim with GNU General Public License v3.0 | 4 votes |
@Override public void draw(Graphics g) { if (field.getFont() != null) g.setFont(field.getFont()); // draw boundary Bounds bds = getBounds(g); g.setColor(new Color(255, 255, 255, 128)); g.fillRect(bds.getX(), bds.getY(), bds.getWidth(), bds.getHeight()); g.setColor(Color.black); g.drawRect(bds.getX(), bds.getY(), bds.getWidth(), bds.getHeight()); if (field.getColor() != null) g.setColor(field.getColor()); // draw text int x = field.getX(); int y = field.getY(); FontMetrics fm = g.getFontMetrics(); int width = fm.stringWidth(curText); int ascent = fm.getAscent(); int descent = fm.getDescent(); switch (field.getHAlign()) { case TextField.H_CENTER: x -= width / 2; break; case TextField.H_RIGHT: x -= width; break; default: break; } switch (field.getVAlign()) { 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(curText, x, y); g.setColor(Color.BLACK); // draw cursor if (pos > 0) x += fm.stringWidth(curText.substring(0, pos)); g.drawLine(x, y + descent, x, y - ascent); }
Example 20
Source File: ListJavaFonts.java From SWET with MIT License | 4 votes |
public static void main(String[] args) { List<String> monospaceFontFamilyNames = new ArrayList<>(); GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment .getLocalGraphicsEnvironment(); String[] fontFamilyNames = graphicsEnvironment .getAvailableFontFamilyNames(); BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); Graphics graphics = bufferedImage.createGraphics(); for (String fontFamilyName : fontFamilyNames) { boolean isMonospaced = true; // int fontStyle = Font.PLAIN; int fontSize = 12; Font font = new Font(fontFamilyName, fontStyle, fontSize); @SuppressWarnings("serial") List<Integer> codePoints = new ArrayList<Integer>() { { add(108); /* l */ add(109); /* m */ add(119); /* w */ add(49); /* 1 */ add(52); /* 4 */ } }; FontMetrics fontMetrics = graphics.getFontMetrics(font); int firstCharacterWidth = 0; boolean hasFirstCharacterWidth = false; for (int codePoint : codePoints) { if (Character.isValidCodePoint(codePoint) && (Character.isLetter(codePoint) || Character.isDigit(codePoint))) { char character = (char) codePoint; int characterWidth = fontMetrics.charWidth(character); if (hasFirstCharacterWidth) { if (characterWidth != firstCharacterWidth) { isMonospaced = false; break; } } else { firstCharacterWidth = characterWidth; hasFirstCharacterWidth = true; } } } if (isMonospaced) { monospaceFontFamilyNames.add(fontFamilyName); } } graphics.dispose(); for (String fontFamily : monospaceFontFamilyNames) { System.out.println(fontFamily); } }