List of usage examples for java.awt FontMetrics charsWidth
public int charsWidth(char[] data, int off, int len)
From source file:Main.java
public static void main(String args[]) { JFrame f = new JFrame("JColorChooser Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(300, 200);/* www . j av a 2 s .co m*/ f.setVisible(true); FontMetrics metrics = f.getFontMetrics(f.getFont()); int widthX = metrics.charsWidth(new char[] { 'a', 'b' }, 1, 2); System.out.println(widthX); }
From source file:com.intel.stl.ui.common.view.ComponentFactory.java
/** * /*from www .j a va2s . c om*/ * <i>Description:</i> simple method that creates single/multi line label * with specified label width based on a source label. For more advanced * label attributes, it's the developer's responsibility to set them back. * * @param source * @param wrap * @param maxWidth * @return */ public static JLabel deriveLabel(JLabel source, final boolean wrap, final int maxWidth) { JXLabel label = new JXLabel(source.getText(), source.getIcon(), source.getHorizontalAlignment()) { private static final long serialVersionUID = -4816144910055350011L; private Font cachedFont; private String chahedRawText, chahedText; /* * (non-Javadoc) * * @see javax.swing.JLabel#getText() */ @Override public String getText() { String text = super.getText(); if (wrap || maxWidth <= 0 || text == null || text.isEmpty()) { return text; } if (getFont().equals(cachedFont) && text.equals(chahedRawText)) { return chahedText; } chahedRawText = text; cachedFont = getFont(); FontMetrics fm = getFontMetrics(cachedFont); char[] chars = text.toCharArray(); int width = fm.charsWidth(chars, 0, chars.length); if (width < maxWidth) { chahedText = text; } else { width += fm.charWidth('.') * 3; int pos = chars.length - 1; for (; pos >= 0 && width > maxWidth; pos--) { width -= fm.charWidth(chars[pos]); } chahedText = new String(chars, 0, pos) + "..."; if (getToolTipText() == null) { setToolTipText(text); } } return chahedText; } }; if (wrap) { label.setLineWrap(true); } if (maxWidth > 0) { label.setMaxLineSpan(maxWidth); } label.setEnabled(source.isEnabled()); label.setForeground(source.getForeground()); label.setOpaque(source.isOpaque()); label.setBackground(source.getBackground()); label.setFont(source.getFont()); label.setBorder(source.getBorder()); label.setToolTipText(source.getToolTipText()); return label; }
From source file:krasa.cpu.CpuUsagePanel.java
@Override public void paintComponent(final Graphics g) { final boolean pressed = getModel().isPressed(); final boolean stateChanged = myWasPressed != pressed; myWasPressed = pressed;//w ww . ja v a2 s .com Image bufferedImage = myBufferedImage; if (bufferedImage == null || stateChanged) { final Dimension size = getSize(); final Insets insets = getInsets(); bufferedImage = UIUtil.createImage(g, size.width, size.height, BufferedImage.TYPE_INT_ARGB); final Graphics2D g2 = (Graphics2D) bufferedImage.getGraphics().create(); final int max = 100; int system = CpuUsageManager.system; int process = CpuUsageManager.process; final int otherProcesses = system - process; final int totalBarLength = size.width - insets.left - insets.right - 3; final int processUsageBarLength = totalBarLength * process / max; final int otherProcessesUsageBarLength = totalBarLength * otherProcesses / max; final int barHeight = Math.max(size.height, getFont().getSize() + 2); final int yOffset = (size.height - barHeight) / 2; final int xOffset = insets.left; // background g2.setColor(UIUtil.getPanelBackground()); g2.fillRect(0, 0, size.width, size.height); // gauge (ide) g2.setColor(ideColor); g2.fillRect(xOffset + 1, yOffset, processUsageBarLength + 1, barHeight); // gauge (system) g2.setColor(systemColor); g2.fillRect(xOffset + processUsageBarLength + 1, yOffset, otherProcessesUsageBarLength + 1, barHeight); // label g2.setFont(getFont()); // final String info = CpuUsageBundle.message("cpu.usage.panel.message.text", CpuUsageManager.process, // CpuUsageManager.system); final String info = fixedLengthString(String.valueOf(process), 3) + "% / " + fixedLengthString(String.valueOf(system), 3) + "%"; final FontMetrics fontMetrics = g.getFontMetrics(); final int infoWidth = fontMetrics.charsWidth(info.toCharArray(), 0, info.length()); final int infoHeight = fontMetrics.getAscent(); UISettings.setupAntialiasing(g2); final Color fg = pressed ? UIUtil.getLabelDisabledForeground() : JBColor.foreground(); g2.setColor(fg); g2.drawString(info, xOffset + (totalBarLength - infoWidth) / 2, yOffset + infoHeight + (barHeight - infoHeight) / 2 - 1); // border g2.setStroke(new BasicStroke(1)); g2.setColor(JBColor.GRAY); g2.drawRect(0, 0, size.width - 2, size.height - 1); g2.dispose(); myBufferedImage = bufferedImage; } draw(g, bufferedImage); }
From source file:util.ui.UiUtilities.java
/** * Gets the width of the specified char array. * * @param chars// w w w .java 2 s . c o m * The char array to get the width for. * @param offset * The offset where to start. * @param length * The length of the measure. * @param font * The font being the base of the measure. * @return the width of the specified char array. */ public static int getCharsWidth(Font font, char[] chars, int offset, int length) { if (chars == null) { return 0; } FontMetrics metrics = HELPER_LABEL.getFontMetrics(font); return metrics.charsWidth(chars, offset, length); }