void drawString(Graphics g, String text, int x, int y) { for (String line : text.split("\n")) g.drawString(line, x, y += g.getFontMetrics().getHeight()); }
void drawNumbers(Graphics g) { Point p; String num; FontMetrics fm=g.getFontMetrics(); int len=0; synchronized(array) { for(int i=0; i < num_fields; i++) for(int j=0; j < num_fields; j++) { num=String.valueOf(array[i][j]); len=fm.stringWidth(num); p=index2Coord(i, j); g.drawString(num, p.x - (len / 2), p.y); } } }
FontMetrics fm = g.getFontMetrics(); int lineHeight = fm.getHeight(); int wordWidth = fm.stringWidth(word + " "); g.drawString(word, curX, curY);
private void calcStringSizes(Font f, Graphics g) { FontMetrics fm = g.getFontMetrics(f); stringWidth = fm.stringWidth(emptyString); stringHeight = fm.getHeight(); ascent = fm.getMaxAscent(); }
private void draw (Graphics g, Array<TextureRegion> regions, Color color, boolean drawIndex) { int i=0; for(TextureRegion region : regions){ int x = region.getRegionX(), y = region.getRegionY(), h = region.getRegionHeight(); if(drawIndex){ String indexString = ""+i; Rectangle bounds = g.getFontMetrics().getStringBounds(indexString, g).getBounds(); g.setColor(indexBackgroundColor); g.fillRect(x, y+h-bounds.height, bounds.width, bounds.height); g.setColor(indexColor); g.drawString(indexString, x, y+h); ++i; } g.setColor(color); g.drawRect(x, y, region.getRegionWidth(), h); } } }
public synchronized void update(Graphics g) { Dimension d=getSize(); if((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) { offscreen=createImage(d.width, d.height); offscreensize=d; offgraphics=offscreen.getGraphics(); offgraphics.setFont(default_font); } offgraphics.setColor(getBackground()); offgraphics.fillRect(0, 0, d.width, d.height); FontMetrics fm=offgraphics.getFontMetrics(); for(int i=0; i < nodes.size(); i++) { paintNode(offgraphics, nodes.get(i), fm); } g.drawImage(offscreen, 0, 0, null); }
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(); } } }
g.setColor(new Color(0, 255, 0)); this.width = g.getFontMetrics().charWidth(dotChar); if (this.isVisible()) { g.fillRect(r.x, r.y, this.width, r.height);
import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; public int measureText(Graphics g, String text) { g.setFont(new Font("TimesRoman", Font.PLAIN, 12)); FontMetrics metrics = g.getFontMetrics(); return metrics.stringWidth(text); }
graphics.setFont(font); 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());
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 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); } }
public static int getMaxColumnWidth(JTable table, int column) { // Set first to column header width as a minimum Font font = table.getFont(); FontMetrics metrics = table.getGraphics().getFontMetrics(font); String header = table.getColumnName(column) + " "; // Whitespace buffer as a crude way (hack) // to account for the rendering context Test.output("Column name: " + header); // Convert from string length in characters to pixels int widest = metrics.stringWidth(header) + (2 * table.getColumnModel().getColumnMargin()); Test.output("Starting widest value: " + header + " (" + widest + " pixels)"); // Now go through each row to see if there is a longer value in the column int rows = table.getRowCount(); Test.output("Row count: " + rows); for (int index = 0; index < rows; index++) { String cellValue = table.getValueAt(index, column).toString(); int cellWidth = metrics.stringWidth(cellValue) + (2 * table.getColumnModel().getColumnMargin()); if (cellWidth > widest) { widest = cellWidth; Test.output("New widest value: " + widest + " pixels"); } } return widest; }
g.setFont(f); FontMetrics fm = g.getFontMetrics(f); txtY += fm.getMaxAscent();
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); } }
private void calcStringSizes (Font f, Graphics g) { FontMetrics fm = g.getFontMetrics (f); stringWidth = fm.stringWidth(emptyString); stringHeight = fm.getHeight(); ascent = fm.getMaxAscent(); }
private void draw (Graphics g, Array<TextureRegion> regions, Color color, boolean drawIndex) { int i=0; for(TextureRegion region : regions){ int x = region.getRegionX(), y = region.getRegionY(), h = region.getRegionHeight(); if(drawIndex){ String indexString = ""+i; Rectangle bounds = g.getFontMetrics().getStringBounds(indexString, g).getBounds(); g.setColor(indexBackgroundColor); g.fillRect(x, y+h-bounds.height, bounds.width, bounds.height); g.setColor(indexColor); g.drawString(indexString, x, y+h); ++i; } g.setColor(color); g.drawRect(x, y, region.getRegionWidth(), h); } } }
/** * Paint a selected line * @param g Graphics instance. * @param x int. x axis value. * @param y int. y axis value. */ private void paintInSelected(Graphics g, int x, int y) { g.setFont(HexView.BOLD_FONT); char[] content = model.getLineChars(selectedLine); g.drawChars(content, 0, selectedIndexInLine - 0, x, y); g.setColor(HexView.SELECTED_COLOR); x += g.getFontMetrics().charsWidth(content, 0, selectedIndexInLine-0); g.drawChars(content, selectedIndexInLine, 1, x, y); g.setColor(Color.black); x += g.getFontMetrics().charWidth(content[selectedIndexInLine]); g.drawChars(content, selectedIndexInLine+1, (content.length-1)-selectedIndexInLine, x, y); g.setFont(HexView.FONT); }
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); Rectangle bounds = g.getClipBounds(); int index = 0; for (String name : shortLineNames.keySet()) { int nameWidth = g.getFontMetrics().stringWidth(name); int lineX = lineStep * index; index++; if (bounds.getMaxX() < lineX || bounds.getMinX() > lineX + nameWidth) { continue; } g.setClip(lineX + LINE_LABEL_BOUNDARY, 0, lineStep - +LINE_LABEL_BOUNDARY * 2, getHeight()); g.drawString(name, lineX + LINE_LABEL_BOUNDARY, AXIS_SIZE / 3); g.setClip(null); } } }
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(getDisabledTextColor()); g.drawString(placeholder, getInsets().left, pG.getFontMetrics() .getMaxAscent() + getInsets().top);