List of usage examples for java.awt Graphics getFontMetrics
public FontMetrics getFontMetrics()
From source file:Main.java
public static void main(String[] args) { String s = "The quick brown fox jumps over the lazy dog!"; BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); FontMetrics fm = g.getFontMetrics(); Rectangle2D b = fm.getStringBounds(s, g); System.out.println(b);/*from w w w . j a va2s . c o m*/ bi = new BufferedImage((int) b.getWidth(), (int) (b.getHeight() + fm.getDescent()), BufferedImage.TYPE_INT_RGB); g = bi.getGraphics(); g.drawString(s, 0, (int) b.getHeight()); JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi))); // Technique 3 - JLabel JLabel l = new JLabel(s); l.setSize(l.getPreferredSize()); bi = new BufferedImage(l.getWidth(), l.getHeight(), BufferedImage.TYPE_INT_RGB); g = bi.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, 400, 100); l.paint(g); JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi))); }
From source file:SplashScreenDemo.java
public static void main(String[] args) { SplashScreen splashScreen = SplashScreen.getSplashScreen(); Dimension size = splashScreen.getSize(); int borderDim = (int) (size.height * 0.05); Graphics g = splashScreen.createGraphics(); g.setColor(Color.blue);//from w ww. j a va2s.c o m for (int i = 0; i < borderDim; i++) g.drawRect(i, i, size.width - 1 - i * 2, size.height - 1 - i * 2); FontMetrics fm = g.getFontMetrics(); int sWidth = fm.stringWidth("Initializing..."); int sHeight = fm.getHeight(); if (sWidth < size.width && 2 * sHeight < size.height) { g.setColor(Color.blue); g.drawString("Initializing...", (size.width - sWidth) / 2, size.height - 2 * sHeight); } splashScreen.update(); try { Thread.sleep(5000); } catch (InterruptedException e) { } }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel() { @Override// ww w. j a v a 2 s. c om protected void paintComponent(Graphics g) { super.paintComponent(g); int y = 0; for (int size = 4; size <= 24; size += 2) { g.setFont(new Font("Arial", Font.BOLD, size)); g.drawString("Name", 0, y); int heightOfFont = g.getFontMetrics().getHeight(); y += heightOfFont; } } @Override public Dimension getPreferredSize() { return new Dimension(300, 300); } }; frame.add(panel); frame.setLocationByPlatform(true); frame.setVisible(true); frame.pack(); }
From source file:Main.java
public static void main(String args[]) { JFrame f = new JFrame(); f.add(new JComponent() { public void paintComponent(Graphics g) { // Some parameters. String text = "Some Label"; int centerX = 150, centerY = 100; int ovalWidth = 200, ovalHeight = 100; // Draw oval g.setColor(Color.BLUE); g.fillOval(centerX - ovalWidth / 2, centerY - ovalHeight / 2, ovalWidth, ovalHeight); // Draw centered text FontMetrics fm = g.getFontMetrics(); double textWidth = fm.getStringBounds(text, g).getWidth(); g.setColor(Color.WHITE); g.drawString(text, (int) (centerX - textWidth / 2), (int) (centerY + fm.getMaxAscent() / 2)); }/*from ww w.jav a 2 s . c om*/ }); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(300, 300); f.setVisible(true); }
From source file:Main.java
public static void drawCenteredString(String string, int width, int height, Graphics graphics) { FontMetrics fontMetrics = graphics.getFontMetrics(); int x = (width - fontMetrics.stringWidth(string)) / 2; int y = (fontMetrics.getAscent() + (height - (fontMetrics.getAscent() + fontMetrics.getDescent())) / 2); graphics.drawString(string, x, y);//from www. j ava 2 s .co m }
From source file:Main.java
/** * Get the string height in the context of Graphics. * /*w w w .ja v a2 s .c o m*/ * @param g * The <code>Graphics</code>. * @return The height <code>int</code>. */ public static int getStringHeight(final Graphics g) { final FontMetrics fontMetrics = g.getFontMetrics(); return fontMetrics.getMaxAscent() + fontMetrics.getMaxDescent(); }
From source file:Main.java
/** Draw 'str' centered at 'p'. */ public static void drawCenteredText(Graphics g, Point p, String str) { FontMetrics fm = g.getFontMetrics(); LineMetrics lm = fm.getLineMetrics(str, g); // Go to 'p', then add a/2 to get to the baseline. // I ignore the descent because it looks better to center without // regard to descenders. int baseY = p.y + (int) (lm.getAscent() / 2); int baseX = p.x - fm.stringWidth(str) / 2; g.drawString(str, baseX, baseY);/*from w w w .j a v a 2 s .c om*/ }
From source file:Main.java
/** Draw 'str' at the given location, but process newlines by moving * to a new line. *//* www .ja v a 2s.co m*/ public static void drawTextWithNewlines(Graphics g, String str, int x, int y) { String lines[] = str.split("\n"); int lineHeight = g.getFontMetrics().getHeight(); for (String s : lines) { g.drawString(s, x, y); y += lineHeight; } }
From source file:Main.java
public static void setLabelAboveOut(JLabel label, String str, int x, int y, int width) { Graphics g = label.getGraphics(); label.setText(str);/*w ww . ja va 2 s . c o m*/ int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str); int lX = x + width / 2 - strWidth / 2; int lY = y - g.getFont().getSize(); label.setBounds(lX, lY, strWidth, g.getFont().getSize()); }
From source file:Main.java
public static void setLabelTop(JLabel label, String str, int width, int height) { Graphics g = label.getGraphics(); label.setText(str);//from w w w .ja v a 2s .c om int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str); int lX = width / 2 - strWidth / 2; int lY = g.getFont().getSize() / 2; //System.out.printf("\"label\"+%s X:%d,Y:%d\n",str,lX,lY); label.setBounds(lX, lY, strWidth, g.getFont().getSize()); }