public static void main(String[] args) throws Exception { final BufferedImage image = ImageIO.read(new URL( "http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png")); Graphics g = image.getGraphics(); g.setFont(g.getFont().deriveFont(30f)); g.drawString("Hello World!", 100, 100); g.dispose(); ImageIO.write(image, "png", new File("test.png")); }
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); } }
public Font scaleFont(String text, Rectangle rect, Graphics g, Font pFont) { float fontSize = 20.0f; Font font = pFont; font = g.getFont().deriveFont(fontSize); int width = g.getFontMetrics(font).stringWidth(text); fontSize = (rect.width / width ) * fontSize; return g.getFont().deriveFont(fontSize); }
private Font getFont(Graphics g, int size) { Font font = new Font(DEFAULT_FONT_NAME, Font.PLAIN, size); if (font != null) return font; return g.getFont(); }
private Font getFont(Graphics g, int size) { Font font = new Font(DEFAULT_FONT_NAME, Font.BOLD, size); if (font != null) return font; return g.getFont(); }
public Point getToolTipLocation(MouseEvent e) { Graphics g = getGraphics(); FontMetrics metrics = g.getFontMetrics(g.getFont()); String prefix = itemnameHistory.size() <= 9 ? "8: " : "88: "; int prefixWidth = metrics.stringWidth(prefix); int x = JButton.TRAILING + JButton.LEADING - 1 + prefixWidth; return new Point(x, 0); } }
private Font getFont(Graphics g, int size) { Font font = new Font(DEFAULT_FONT_NAME, Font.BOLD, size); if (font != null) return font; return g.getFont(); }
public static void fillWithString(Graphics g, String theText, double sizeFactor) { int theHeight = g.getClipBounds().height; int theWidth = g.getClipBounds().width; Font theFont = g.getFont().deriveFont((float) (theHeight / sizeFactor)).deriveFont(Font.BOLD); g.setFont(theFont); FontMetrics theMetric = g.getFontMetrics(); g.drawString(theText, (theWidth - theMetric.stringWidth(theText)) / 2, theMetric.getAscent() + (theHeight - (theMetric.getDescent() + theMetric.getAscent())) / 2); }
private Font getFont(Graphics g, int size) { Font font = new Font(DEFAULT_FONT_NAME, Font.PLAIN, size); if (font != null) return font; return g.getFont(); }
private void calculateYAxisDimensions(Graphics g) { FontMetrics fm = g.getFontMetrics(g.getFont()); int axisWidth = getYLabelsMaxWidth(fm) + spacing * 3 + fm.getHeight(); yAxisRect.setBounds(chartRect.x, chartRect.y, axisWidth, chartRect.height); if (!isPreview) { chartRect.setBounds(chartRect.x + axisWidth, chartRect.y, chartRect.width - axisWidth, chartRect.height); } else { chartRect.setBounds(chartRect.x + previewInset, chartRect.y, chartRect.width, chartRect.height); } }
private Font getFont(Graphics g, int size) { Font font = new Font(DEFAULT_FONT_NAME, Font.BOLD, size); if (font != null) return font; return g.getFont(); }
private void calculateYAxisDimensions(Graphics g) { FontMetrics fm = g.getFontMetrics(g.getFont()); int axisWidth = getYLabelsMaxWidth(fm) + spacing * 3 + fm.getHeight(); yAxisRect.setBounds(chartRect.x, chartRect.y, axisWidth, chartRect.height); if (!isPreview) { chartRect.setBounds(chartRect.x + axisWidth, chartRect.y, chartRect.width - axisWidth, chartRect.height); } else { chartRect.setBounds(chartRect.x + previewInset, chartRect.y, chartRect.width, chartRect.height); } }
private Font getFont(Graphics g, int size) { Font font = new Font(DEFAULT_FONT_NAME, Font.BOLD, size); if (font != null) return font; return g.getFont(); }
private final Font getFont() { Graphics g = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB).getGraphics(); Font font = new Font(g.getFont().toString(), 0, 12); g.dispose(); return font; }
@Override public void paintIcon(Component c, Graphics g, int x, int y) { if (background != null) background.paintIcon(c, g, x + backgroundXOffset, y + backgroundYOffset); g.setColor(color); Font origFont = g.getFont(); g.setFont(font); g.drawString(text, x + textXOffset, y + textYOffset); g.setFont(origFont); }
@Override public void paintBorder(java.awt.Component c, Graphics g, int x, int y, int width, int height) { g.setColor(COLOR); g.fillRect(x, y, width, 20); g.setFont(g.getFont().deriveFont(BOLD)); g.setColor(WHITE); g.drawString(text, x + 5, y + 15); } }
/** * Returns a <code>String</code> object representing this * <code>DVBGraphics</code> object's value. * @return a string representation of this graphics context. * @since MHP 1.0 */ public String toString() { return getClass().getName() + "[font=" + getFont() + ",color=" + getColor() + "]"; } }
private void criarBuffer() { createBufferStrategy(2); buffer = getBufferStrategy(); if (fonteTexto == null) { Graphics g = buffer.getDrawGraphics(); fonteTexto = g.getFont(); dimensoesFonte = getFontMetrics(fonteTexto); g.dispose(); } }
public void init(DrawContext ctx) { JTextComponent c = ctx.getEditorUI().getComponent(); gColor = graphics.getColor(); gFont = graphics.getFont(); // initialize reference to annotations annos = ctx.getEditorUI().getDocument().getAnnotations(); }