public static FontMetrics getFontMetrics(Font font){ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsConfiguration config = gd.getDefaultConfiguration(); Canvas c = new Canvas(config); return c.getFontMetrics(font); }
@Override public FontMetrics getFontMetrics(Font f) { // works also in headless mode return new Canvas().getFontMetrics(f); }
public TextIcon(String text, Color foreground, Color background, boolean square, Font font) { this.text = text; this.font = font; FontMetrics metrics = new Canvas().getFontMetrics(font); this.textWidth = metrics.stringWidth(text); this.iconWidth = textWidth; this.iconHeight = font.getSize() + 2; if (square) { this.iconWidth = Math.max(this.iconWidth, this.iconHeight); this.iconHeight = this.iconWidth; } this.ascent = metrics.getAscent(); this.foreground = foreground; this.background = background; }
private float getPreferredAlignmentY() { Font font = getFont(); // deprecated: FontMetrics fm = getToolkit().getFontMetrics(font); FontMetrics fm = new Canvas().getFontMetrics(font); float h = fm.getHeight(); float d = fm.getDescent(); return (h - d) / h; }
public TextOverlayIcon(Icon background, String text, Color color, Font font) { this.background = background; this.text = text; this.color = color; this.font = font; this.metrics = new Canvas().getFontMetrics(font); int textWidth = metrics.stringWidth(text); int iconWidth = background.getIconWidth(); setIconWidth(Math.max(textWidth, iconWidth)); int textHeight = font.getSize(); int iconHeight = background.getIconHeight(); setIconHeight(Math.max(textHeight, iconHeight)); }
public int measureText(String text) throws IOException { if (baseUrl != null) { URL fontURL = new URL(baseUrl, "fonts/verdana.ttf"); InputStream fontStream = fontURL.openStream(); Font defaultFont = null; try { defaultFont = Font.createFont(Font.TRUETYPE_FONT, fontStream); } catch (FontFormatException e) { throw new IOException(e.getMessage()); } defaultFont = defaultFont.deriveFont(11f); Canvas canvas = new Canvas(); FontMetrics fontMetrics = canvas.getFontMetrics(defaultFont); return fontMetrics.stringWidth(text); } return 0; }
public int measureText( String text ) throws FontFormatException, IOException { URL fontURL = new URL( Jenkins.getInstance().pluginManager.getPlugin( "status-badges" ).baseResourceURL, "fonts/verdana.ttf" ); InputStream fontStream = fontURL.openStream(); Font defaultFont = Font.createFont( Font.TRUETYPE_FONT, fontStream ); defaultFont = defaultFont.deriveFont( 11f ); Canvas canvas = new Canvas(); FontMetrics fontMetrics = canvas.getFontMetrics( defaultFont ); return fontMetrics.stringWidth( text ); }
private ImageIcon getScaledIcon(ImageIcon original) { Canvas c = new Canvas(); FontMetrics fm = c.getFontMetrics(getFont()); int height = (int) (fm.getHeight() * 2f); int width = original.getIconWidth() / original.getIconHeight() * height; BufferedImage scaledImage = Scalr.resize(ImageCache.createImage(original.getImage()), Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_ANTIALIAS); return new ImageIcon(scaledImage); }
private ImageIcon getScaledIcon(ImageIcon original) { Canvas c = new Canvas(); FontMetrics fm = c.getFontMetrics(getFont()); int height = (int) (fm.getHeight() * 2f); int width = original.getIconWidth() / original.getIconHeight() * height; BufferedImage scaledImage = Scalr.resize(ImageCache.createImage(original.getImage()), Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_ANTIALIAS); return new ImageIcon(scaledImage); }
private ImageIcon getScaledIcon(ImageIcon original) { Canvas c = new Canvas(); FontMetrics fm = c.getFontMetrics(getFont()); int height = (int) (fm.getHeight() * 2f); int width = original.getIconWidth() / original.getIconHeight() * height; BufferedImage scaledImage = Scalr.resize(ImageCache.createImage(original.getImage()), Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_ANTIALIAS); return new ImageIcon(scaledImage); } }
private ImageIcon getScaledIcon(ImageIcon original) { Canvas c = new Canvas(); FontMetrics fm = c.getFontMetrics(getFont()); int height = (int) (fm.getHeight() * 2f); int width = original.getIconWidth() / original.getIconHeight() * height; BufferedImage scaledImage = Scalr.resize(ImageCache.createImage(original.getImage()), Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_ANTIALIAS); return new ImageIcon(scaledImage); }
private ImageIcon getScaledIcon(ImageIcon original) { Canvas c = new Canvas(); FontMetrics fm = c.getFontMetrics(getFont()); int height = (int) (fm.getHeight() * 2f); int width = original.getIconWidth() / original.getIconHeight() * height; BufferedImage scaledImage = Scalr.resize(ImageCache.createImage(original.getImage()), Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_ANTIALIAS); return new ImageIcon(scaledImage); }
private ImageIcon getScaledIcon(ImageIcon original) { Canvas c = new Canvas(); FontMetrics fm = c.getFontMetrics(getFont()); int height = (int) (fm.getHeight() * 2f); int width = original.getIconWidth() / original.getIconHeight() * height; BufferedImage scaledImage = Scalr.resize(ImageCache.createImage(original.getImage()), Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_ANTIALIAS); return new ImageIcon(scaledImage); }
private ImageIcon getScaledIcon(ImageIcon original) { Canvas c = new Canvas(); FontMetrics fm = c.getFontMetrics(getFont()); int height = (int) (fm.getHeight() * 2f); int width = original.getIconWidth() / original.getIconHeight() * height; BufferedImage scaledImage = Scalr.resize(ImageCache.createImage(original.getImage()), Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_ANTIALIAS); return new ImageIcon(scaledImage); }
FontMetrics metrics = canvas.getFontMetrics(font);
/** * Gets the preferred size. * * @return the preferred size */ public Dimension getPreferredSize() { final Canvas c = new Canvas(); final FontMetrics fm = c.getFontMetrics(this.font); //System.out.println("longest: " + longestString); configureFormatter(); final int minWidth = fm.stringWidth(this.longestString) + 5; Dimension bestSize = null; if (isVertical()) { bestSize = new Dimension(minWidth, getHeight()); } else { bestSize = new Dimension( (int) (ModelChartImpl.this.runningTime * getScaleFactor()), fm.getHeight()); this.minHalfDistance = minWidth; } //System.out.println("pref: " + bestSize); return bestSize; }
private ImageIcon getScaledIcon(ImageIcon original) { Canvas c = new Canvas(); FontMetrics fm = c.getFontMetrics(new JPanel().getFont()); int height = (int) (fm.getHeight() * 2f); int width = original.getIconWidth() / original.getIconHeight() * height; BufferedImage scaledImage; if (!scraper.isEnabled()) { scaledImage = Scalr.resize(ImageCache.createImage(original.getImage()), Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_GRAYSCALE); } else { scaledImage = Scalr.resize(ImageCache.createImage(original.getImage()), Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_ANTIALIAS); } return new ImageIcon(scaledImage); }
private ImageIcon getScaledIcon(ImageIcon original) { Canvas c = new Canvas(); FontMetrics fm = c.getFontMetrics(new JPanel().getFont()); int height = (int) (fm.getHeight() * 2f); int width = original.getIconWidth() / original.getIconHeight() * height; BufferedImage scaledImage; if (!scraper.isEnabled()) { scaledImage = Scalr.resize(ImageCache.createImage(original.getImage()), Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_GRAYSCALE); } else { scaledImage = Scalr.resize(ImageCache.createImage(original.getImage()), Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_ANTIALIAS); } return new ImageIcon(scaledImage); }
fm = c.getFontMetrics(sysFont);
fm = c.getFontMetrics(sysFont);