public void drawText( String text, int x, int y ) { int height = gc.getFontMetrics().getHeight(); String[] lines = text.split( "\n" ); for ( String line : lines ) { gc.drawString( line, x + xOffset, y + height + yOffset ); y += height; } }
public void paintNode(Graphics g, Node n, FontMetrics fm) { String addr=n.addr != null? n.addr.toString() : null; int x=(int)n.x; int y=(int)n.y; g.setColor((n == pick)? selectColor : (n.fixed? fixedColor : nodeColor)); int w=fm.stringWidth(n.lbl) + 10; if(addr != null) w=Math.max(w, fm.stringWidth(addr) + 10); if(addr == null) addr="<no address>"; int h=(fm.getHeight() + 4) * 2; n.width=w; n.height=h; n.xloc=x - w / 2; n.yloc=y - h / 2; g.fillRect(x - w / 2, y - h / 2, w, h); g.setColor(Color.black); g.drawRect(x - w / 2, y - h / 2, w - 1, h - 1); g.drawString(n.lbl, x - (w - 10) / 2, (y - (h - 4) / 2) + fm.getAscent()); g.drawString(addr, x - (w - 10) / 2, (y - (h - 4) / 2) + 2 * fm.getAscent() + 4); }
public void setFont(Font font) { super.setFont(font); Graphics g = this.getGraphics(); if (g != null) { FontMetrics fm = g.getFontMetrics(font); int height = fm.getHeight(); _rowHeight = height + height / 3; setRowHeight(_rowHeight); } }
graphics.drawString(textWithoutCol, x + 1, position.y + 1); graphics.drawString(textWithoutCol, x, position.y); x += fontMetrics.stringWidth(textWithoutCol); graphics.drawString(text, position.x + 1, position.y + 1); return new Dimension(fontMetrics.stringWidth(text), fontMetrics.getHeight());
@Override public Dimension render(Graphics2D graphics) { graphics.translate(preferredLocation.x, preferredLocation.y); final FontMetrics metrics = graphics.getFontMetrics(); final TextComponent titleComponent = new TextComponent(); titleComponent.setText(text); titleComponent.setColor(color); titleComponent.setPosition(new Point((preferredSize.width - metrics.stringWidth(text)) / 2, metrics.getHeight())); final Dimension dimension = titleComponent.render(graphics); graphics.translate(-preferredLocation.x, -preferredLocation.y); return new Dimension(preferredSize.width, dimension.height); } }
return new Dimension(200, 200); String text = "This is a test xyx"; g.setColor(Color.RED); g.drawLine(0, height / 2, getWidth(), height / 2); FontMetrics fm = g.getFontMetrics(); int totalWidth = (fm.stringWidth(text) * 2) + 4; int y = (getHeight() - fm.getHeight()) / 2; g.setColor(Color.BLACK); g.drawString(text, x, y + ((fm.getDescent() + fm.getAscent()) / 2)); x += fm.stringWidth(text) + 2; y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent(); g.drawString(text, x, y);
@Override public Dimension getPreferredSize() { if (preferredSize != null) { return preferredSize; } if (tree == null) { return super.getSize(); } FontMetrics fM = getFontMetrics(new Font(fontName, style, fontSize)); double nodeHeight = fM.getHeight(); double layerMultiplier = (1.0 + belowLineSkip + aboveLineSkip + parentSkip); double layerHeight = nodeHeight * layerMultiplier; double layerBuffer = (diffConstituents.size() + 1)*layerHeight; layerBuffer += 20.0; preferredSize = new Dimension((int)width(tree,fM), (int)(height(tree,fM) + layerBuffer)); return preferredSize; }
private void shadowWrite(BufferedImage image, String... strings) { Graphics2D g2d = image.createGraphics(); g2d.setFont(new Font("Sans", Font.PLAIN, 25)); FontMetrics fm = g2d.getFontMetrics(); int dy = fm.getHeight(); int xsize = 0; for (String s : strings) { int w = fm.stringWidth(s); if (w > xsize) xsize = w; } int y = 5; int x = 5; //g2d.fillRect(x, y, xsize, dy * strings.length + fm.getDescent()); y += dy; for (String s : strings) { g2d.setPaint(Color.black); g2d.drawString(s, x+1, y+1); g2d.setPaint(Color.white); g2d.drawString(s, x, y); y += dy; } g2d.dispose(); }
private void paintSelectedMinute(Graphics g) { Dimension d = this.getSize(); FontMetrics fm = this.getFontMetrics(this.getFont()); int posX = this.getInsets().left + fm.stringWidth(" 00 : "); //$NON-NLS-1$ int posY = d.height - this.getInsets().bottom - fm.getHeight(); g.setColor(this.getSelectionColor()); g.fillRect(posX, posY, fm.stringWidth("00"), fm.getHeight()); //$NON-NLS-1$ }
/** * Draw a String centered in the middle of a Rectangle. * * @param g The Graphics instance. * @param text The String to draw. * @param rect The Rectangle to center the text in. * @param font */ public void drawCenteredStringWOutline(Graphics2D g, String text, Rectangle rect, Font font) { // Get the FontMetrics FontMetrics metrics = g.getFontMetrics(font); // Determine the X coordinate for the text int x = rect.x + (rect.width - 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 = rect.y + ((rect.height - metrics.getHeight()) / 2) + metrics.getAscent(); // Set the font g.setFont(font); GlyphVector gv = font.createGlyphVector(g.getFontRenderContext(), text); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); g.drawGlyphVector(gv, x, y); g.translate(x - 1, y - 1); for (int i = 0; i < text.length(); i++) { g.setColor(Color.BLACK); g.draw(gv.getGlyphOutline(i)); } g.translate(-x + 1, -y + 1); }
public void paint(Graphics g) { if (isVisible()) { try { JTextComponent component = getComponent(); Rectangle r = component.getUI().modelToView(component, getDot()); Color c = g.getColor(); g.setColor(component.getBackground()); g.setXORMode(component.getCaretColor()); r.setBounds(r.x, r.y, g.getFontMetrics().charWidth('w'), g.getFontMetrics().getHeight()); g.fillRect(r.x, r.y, r.width, r.height); g.setPaintMode(); g.setColor(c); } catch (BadLocationException e) { e.printStackTrace(); } } }
import java.awt.*; public class TestComponent extends JPanel { private void drawString(Graphics g, String text, int x, int y) { for (String line : text.split("\n")) g.drawString(line, x, y += g.getFontMetrics().getHeight()); } public void paintComponent(Graphics g) { super.paintComponent(g); drawString(g, "hello\nworld", 20, 20); g.setFont(g.getFont().deriveFont(20f)); drawString(g, "part1\npart2", 120, 120); } public static void main(String s[]) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new TestComponent()); f.setSize(220, 220); f.setVisible(true); } }
FontMetrics fm = graphics.getFontMetrics(font); int width = SwingUtilities.computeStringWidth(fm, header); ((Graphics2D) graphics).drawString(header, (float) (paper.getImageableWidth() / 2 - width / 2), (float) paper.getImageableY() / 2 + fm.getHeight()); ((Graphics2D) graphics).translate(0, paper.getImageableY() - fm.getHeight()); double height = paper.getImageableHeight() + paper.getImageableY() / 2; width = SwingUtilities.computeStringWidth(fm, name); ((Graphics2D) graphics).drawString(name, (float) (paper.getImageableWidth() / 2 - width / 2), (float) height - fm.getHeight() / 2); ((Graphics2D) graphics).translate(0, fm.getHeight()); width = SwingUtilities.computeStringWidth(fm, pageStr); ((Graphics2D) graphics).drawString(pageStr, (float) (paper.getImageableWidth() / 2 - width / 2), (float) height - fm.getHeight() / 2);
private void drawProgressLabel(Graphics2D graphics, XpGlobe globe, int startXp, int goalXp, int x, int y) { if (goalXp <= globe.getCurrentXp()) { return; } // Convert to int just to limit the decimal cases String progress = (int) (getSkillProgress(startXp, globe.getCurrentXp(), goalXp)) + "%"; final FontMetrics metrics = graphics.getFontMetrics(); int drawX = x + (config.xpOrbSize() / 2) - (metrics.stringWidth(progress) / 2); int drawY = y + (config.xpOrbSize() / 2) + (metrics.getHeight() / 2); OverlayUtil.renderTextLocation(graphics, new Point(drawX, drawY), progress, Color.WHITE); }
private void calcStringSizes(Font f, Graphics g) { FontMetrics fm = g.getFontMetrics(f); stringWidth = fm.stringWidth(emptyString); stringHeight = fm.getHeight(); ascent = fm.getMaxAscent(); }
Font font = new Font("Arial", Font.PLAIN, 48); g2d.setFont(font); FontMetrics fm = g2d.getFontMetrics(); int width = fm.stringWidth(text); int height = fm.getHeight(); g2d.dispose(); g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); g2d.setFont(font); fm = g2d.getFontMetrics(); g2d.setColor(Color.BLACK); g2d.drawString(text, 0, fm.getAscent()); g2d.dispose(); try {
FontMetrics fm = g.getFontMetrics(); int lineHeight = fm.getHeight(); int wordWidth = fm.stringWidth(word + " "); g.drawString(word, curX, curY);
final int progressTextX = barX + (width - metrics.stringWidth(textToWrite)) / 2; final int progressTextY = barY + ((height - metrics.getHeight()) / 2) + metrics.getHeight(); final int progressFill = (int) (width * pc); return new Dimension(width, height);
int y = metrics.getHeight(); final int leftFullWidth = getLineWidth(left, metrics); final int rightFullWidth = getLineWidth(right, metrics); rightLineComponent.setColor(rightColor); rightLineComponent.render(graphics); y += metrics.getHeight(); return new Dimension(preferredSize.width, y - metrics.getHeight()); rightLineComponent.setColor(rightColor); rightLineComponent.render(graphics); y += metrics.getHeight(); return new Dimension(preferredSize.width, y - metrics.getHeight());