List of usage examples for java.awt Graphics getFontMetrics
public FontMetrics getFontMetrics()
From source file:MainClass.java
public void paint(Graphics g) { int fontSize = 20; g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize)); FontMetrics fm = g.getFontMetrics(); String s = "www.java2s.com"; int stringWidth = fm.stringWidth(s); int w = 200;/* w w w .ja v a 2 s .co m*/ int h = 200; int x = (w - stringWidth) / 2; int baseline = fm.getMaxAscent() + (h - (fm.getAscent() + fm.getMaxDecent())) / 2; int ascent = fm.getMaxAscent(); int descent = fm.getMaxDecent(); int fontHeight = fm.getMaxAscent() + fm.getMaxDecent(); g.setColor(Color.white); g.fillRect(x, baseline - ascent, stringWidth, fontHeight); g.setColor(Color.gray); g.drawLine(x, baseline, x + stringWidth, baseline); g.setColor(Color.red); g.drawLine(x, baseline + descent, x + stringWidth, baseline + descent); g.setColor(Color.blue); g.drawLine(x, baseline - ascent, x + stringWidth, baseline - ascent); g.setColor(Color.black); g.drawString(s, x, baseline); }
From source file:TextLayoutLeft.java
public void paint(Graphics g) { d = getSize();/*w w w. j a v a 2 s .c o m*/ g.setFont(f); if (fm == null) { fm = g.getFontMetrics(); ascent = fm.getAscent(); fh = ascent + fm.getDescent(); space = fm.stringWidth(" "); } g.setColor(Color.black); StringTokenizer st = new StringTokenizer( "this is a text. this is a test <BR> this is a text. this is a test"); int x = 0; int nextx; int y = 0; String word, sp; int wordCount = 0; String line = ""; while (st.hasMoreTokens()) { word = st.nextToken(); if (word.equals("<BR>")) { drawString(g, line, wordCount, fm.stringWidth(line), y + ascent); line = ""; wordCount = 0; x = 0; y = y + (fh * 2); } else { int w = fm.stringWidth(word); if ((nextx = (x + space + w)) > d.width) { drawString(g, line, wordCount, fm.stringWidth(line), y + ascent); line = ""; wordCount = 0; x = 0; y = y + fh; } if (x != 0) { sp = " "; } else { sp = ""; } line = line + sp + word; x = x + space + w; wordCount++; } } drawString(g, line, wordCount, fm.stringWidth(line), y + ascent); }
From source file:MyCheckBoxUI.java
public void paint(Graphics g, JComponent c) { AbstractButton b = (AbstractButton) c; ButtonModel model = b.getModel(); Dimension d = b.getSize();//from ww w . j av a 2 s . c o m g.setFont(c.getFont()); FontMetrics fm = g.getFontMetrics(); g.setColor(Color.white); g.drawString("Am I a check box", 10, 10); }
From source file:metrics.java
License:asdf
public void paint(Graphics g) { g.translate(100, 100);/* w w w . ja v a2 s. co m*/ FontMetrics fm = null; int ascent, descent, leading, width1, width2, height; String string1 = "dsdas"; String string2 = "asdf"; int xPos = 25, yPos = 50; fm = g.getFontMetrics(); ascent = fm.getAscent(); descent = fm.getDescent(); leading = fm.getLeading(); height = fm.getHeight(); width1 = fm.stringWidth(string1); width2 = fm.stringWidth(string2); g.drawString(string1, xPos, yPos); g.drawLine(xPos, yPos - ascent - leading, xPos + width1, yPos - ascent - leading); g.drawLine(xPos, yPos - ascent, xPos + width1, yPos - ascent); g.drawLine(xPos, yPos, xPos + width1, yPos); g.drawLine(xPos, yPos + descent, xPos + width1, yPos + descent); g.drawString(string2, xPos, yPos + height); g.drawLine(xPos, yPos - ascent - leading + height, xPos + width2, yPos - ascent - leading + height); g.drawLine(xPos, yPos - ascent + height, xPos + width2, yPos - ascent + height); g.drawLine(xPos, yPos + height, xPos + width2, yPos + height); g.drawLine(xPos, yPos + descent + height, xPos + width2, yPos + descent + height); }
From source file:MyButtonUI.java
public void paint(Graphics g, JComponent c) { AbstractButton b = (AbstractButton) c; Dimension d = b.getSize();/*from www . j a v a2 s .c o m*/ g.setFont(c.getFont()); FontMetrics fm = g.getFontMetrics(); g.setColor(b.getForeground()); String caption = b.getText(); int x = (d.width - fm.stringWidth(caption)) / 2; int y = (d.height + fm.getAscent()) / 2; g.drawString(caption, x, y); }
From source file:Main.java
public void paint(Graphics g) { g.translate(100, 100);// ww w. j av a 2 s. c om FontMetrics fm = null; int ascent, descent, leading, width1, width2, height; String string1 = "www.java2s.com"; String string2 = "java2s.com"; int xPos = 25, yPos = 50; fm = g.getFontMetrics(); ascent = fm.getAscent(); descent = fm.getDescent(); leading = fm.getLeading(); height = fm.getHeight(); width1 = fm.stringWidth(string1); width2 = fm.stringWidth(string2); g.drawString(string1, xPos, yPos); g.drawLine(xPos, yPos - ascent - leading, xPos + width1, yPos - ascent - leading); g.drawLine(xPos, yPos - ascent, xPos + width1, yPos - ascent); g.drawLine(xPos, yPos, xPos + width1, yPos); g.drawLine(xPos, yPos + descent, xPos + width1, yPos + descent); g.drawString(string2, xPos, yPos + height); g.drawLine(xPos, yPos - ascent - leading + height, xPos + width2, yPos - ascent - leading + height); g.drawLine(xPos, yPos - ascent + height, xPos + width2, yPos - ascent + height); g.drawLine(xPos, yPos + height, xPos + width2, yPos + height); g.drawLine(xPos, yPos + descent + height, xPos + width2, yPos + descent + height); }
From source file:Base64.java
/** * Draws a line on the screen at the specified index. Default is green. * <p/>/* ww w . j av a2 s .c o m*/ * Available colours: red, green, cyan, purple, white. * * @param render The Graphics object to be used. * @param row The index where you want the text. * @param text The text you want to render. Colours can be set like [red]. */ public static void drawLine(Graphics render, int row, String text) { FontMetrics metrics = render.getFontMetrics(); int height = metrics.getHeight() + 4; // height + gap int y = row * height + 15 + 19; String[] texts = text.split("\\["); int xIdx = 7; Color cur = Color.GREEN; for (String t : texts) { for (@SuppressWarnings("unused") String element : COLOURS_STR) { // String element = COLOURS_STR[i]; // Don't search for a starting '[' cause it they don't exists. // we split on that. int endIdx = t.indexOf(']'); if (endIdx != -1) { String colorName = t.substring(0, endIdx); if (COLOR_MAP.containsKey(colorName)) { cur = COLOR_MAP.get(colorName); } else { try { Field f = Color.class.getField(colorName); int mods = f.getModifiers(); if (Modifier.isPublic(mods) && Modifier.isStatic(mods) && Modifier.isFinal(mods)) { cur = (Color) f.get(null); COLOR_MAP.put(colorName, cur); } } catch (Exception ignored) { } } t = t.replace(colorName + "]", ""); } } render.setColor(Color.BLACK); render.drawString(t, xIdx, y + 1); render.setColor(cur); render.drawString(t, xIdx, y); xIdx += metrics.stringWidth(t); } }
From source file:Main.java
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;// www. j a va2 s.c om } FontMetrics fm = g.getFontMetrics(); paintViewInsets = c.getInsets(paintViewInsets); paintViewR.x = paintViewInsets.left; paintViewR.y = paintViewInsets.top; // Use inverted height & width paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right); paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom); 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, g, paintIconR.x, paintIconR.y); } if (text != null) { int textX = paintTextR.x; int textY = paintTextR.y + fm.getAscent(); if (label.isEnabled()) { paintEnabledText(label, g, clippedText, textX, textY); } else { paintDisabledText(label, g, clippedText, textX, textY); } } g2.setTransform(tr); }
From source file:haven.Utils.java
static Coord textsz(Graphics g, String text) { java.awt.FontMetrics m = g.getFontMetrics(); java.awt.geom.Rectangle2D ts = m.getStringBounds(text, g); return (new Coord((int) ts.getWidth(), (int) ts.getHeight())); }
From source file:uk.co.modularaudio.mads.base.oscilloscope.ui.OscilloscopeDisplayUiJComponent.java
private void drawScale(final Graphics g, final int height, final float maxMag, final int xOffset) { final FontMetrics fm = g.getFontMetrics(); // Do zero, and plus and minus maximum final int middle = height / 2; final int halfFontHeight = fm.getAscent() / 2; g.drawString("0.0", xOffset, middle + halfFontHeight); final int top = 0 + fm.getHeight(); // Only re-create the string when we need to if (previousPositiveMagStr == null || previousPositiveMag != maxMag) { previousPositiveMagStr = MathFormatter.fastFloatPrint(maxMag, 2, true); previousPositiveMag = maxMag;/*from www . j a va 2s.c om*/ } g.drawString(previousPositiveMagStr, xOffset, top); if (previousNegativeMagStr == null || previousNegativeMag != -maxMag) { previousNegativeMagStr = MathFormatter.fastFloatPrint(-maxMag, 2, true); previousNegativeMag = -maxMag; } final int bottom = height - halfFontHeight; g.drawString(previousNegativeMagStr, xOffset, bottom); }