Package java.awt

Examples of java.awt.FontMetrics


    }

    private void calculateBaseUnits() {
        // This calculation comes from:
        // http://support.microsoft.com/default.aspx?scid=kb;EN-US;125681
        FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(
                                         UIManager.getFont("Button.font"));
        baseUnitX = metrics.stringWidth(
                      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
        baseUnitX = (baseUnitX / 26 + 1) / 2;
        // The -1 comes from experimentation.
        baseUnitY = metrics.getAscent() + metrics.getDescent() - 1;
    }
View Full Code Here


            textClr = Color.darkGray; // make the text dark gray
        }

        protected void measure() {
            // get the global values we use in relation to our current font
            FontMetrics fm = this.getFontMetrics(this.getFont());
            if (fm == null) {
                return;
            }
            width = fm.stringWidth("X") * cols; //$NON-NLS-1$
            height = fm.getHeight() + 4;
        }
View Full Code Here

            }
        }

        private void centerText(String s, Graphics g, int w, int h) {
            // from the centerText method in "Java Examples in a Nutshell"
            FontMetrics fm = this.getFontMetrics(this.getFont());
            if (fm == null) {
                return;
            }
            int sWidth = fm.stringWidth(s);
            int sx = (w - sWidth) / 2;
            int sy = (h - fm.getHeight()) / 2 + fm.getAscent();
            g.drawString(s, sx, sy);
        }
View Full Code Here

        boolean isMonospaced(Font f) {

            int width = -1;

            FontMetrics fm = getFontMetrics(f);

            for (char i = ' '; i <= 'z'; i++) {

                int cw = fm.charWidth(i);

                if (width == -1 || cw == width) {

                    width = cw;
View Full Code Here

                return new Dimension(0, 0);
            }
            int w = 0;
            int h = 0;
            int iw = 0;
            FontMetrics fm = getFontMetrics(getFont());
            for (Enumeration e = menu.children(); e.hasMoreElements();) {
                Action action = (Action) e.nextElement();
                if(action.getName().equals(ActionMenu.SEPARATOR.getName())) {
                    h += separator.getPreferredSize().height;
                }
                else {
                  String imagePath = null;
                  if (iconType.equals(ActionButton.SMALL_ICONS)) {
                      imagePath = (String) action.getValue(Action.SMALL_IMAGE_PATH);
                  } else if (iconType.equals(ActionButton.LARGE_ICONS)) {
                      imagePath = (String) action.getValue(Action.IMAGE_PATH);
                  }
                  String n = (String) action.getValue(Action.SHORT_DESCRIPTION);
                  n = n == null ? action.getName() : n;
                  w = Math.max(w, fm.stringWidth(n));
                  itemHeight = Math.max(itemHeight, fm.getHeight());
                  if (imagePath != null) {
                      Image img = UIUtil.loadImage(action.getClass(), imagePath);
                      if (img != null) {
                          UIUtil.waitFor(img, this);
                          iw = Math.max(iw, img.getWidth(this));
View Full Code Here

                px += insets.left;
                py += insets.top;
            }
            if (menu != null) {
                Action m = null;
                FontMetrics fm = getFontMetrics(getFont());
                //                py += fm.getHeight() - fm.getDescent();
                int i = 0;
                for (Enumeration e = menu.children(); e.hasMoreElements();) {
                    m = (Action) e.nextElement();
                    if(m.getName().equals(ActionMenu.SEPARATOR.getName())) {
                        int h = separator.getPreferredSize().height;
                        int sw = s.width - ( insets == null ? 0 : ( ( insets.left + insets.right ) / 2 ) ) - ( borderWidth * 2 );
                        separator.setBounds(0, 0, sw, h);
                        int sx = (s.width - sw ) / 2;
                        g.translate(sx, py);
                        separator.paint(g);
                        g.translate(-sx, -py);
                        py += h;
                    }
                    else {
                      String n = (String) m.getValue(Action.SHORT_DESCRIPTION);
                      n = n == null ? m.getName() : n;
                      if (m.isEnabled()) {
                          if (i == sel) {
                              g.setColor(selectionBackground);
                              int ty = borderWidth + (insets != null ? insets.top : 0) + (py - borderWidth - insets.top );
                              g.fillRect(borderWidth, ty, s.width - (borderWidth * 2), itemHeight);
                              if (borderWidth != 0) {
                                  g.setColor(borderColor);
                                  g.drawLine(borderWidth, ty, (borderWidth * 2) + s.width - 1, ty);
                                  g.drawLine(borderWidth, ty + itemHeight - 1, (borderWidth * 2) + s.width - 1, ty + itemHeight - 1);
                              }
                              g.setColor(selectionForeground);
                          } else {
                              g.setColor(getForeground());
                          }
                      } else {
                          g.setColor(disabledForeground);
                      }
                      String imagePath = null;
                      if (iconType.equals(ActionButton.SMALL_ICONS)) {
                          imagePath = (String) m.getValue(Action.SMALL_IMAGE_PATH);
                      } else if (iconType.equals(ActionButton.LARGE_ICONS)) {
                          imagePath = (String) m.getValue(Action.IMAGE_PATH);
                      }
                      int toff = 0;
                      if (imagePath != null) {
                          Image img = UIUtil.loadImage(m.getClass(), imagePath);
                          if(!m.isEnabled()) {
                              img = createImage(new FilteredImageSource(img.getSource(),
                              new GrayFilter()));
                              UIUtil.waitFor(img, this);
                          }
                         
                          if (img != null) {
                              g.drawImage(img, px, py + ((itemHeight - img.getHeight(this)) / 2), this);
                              toff = imageTextGap + img.getWidth(this);
                          }
                      }
                      g.drawString(n, px + toff, py + fm.getHeight() - fm.getDescent() + ((itemHeight - fm.getHeight()) / 2));
                      py += itemHeight;
                    }
                    i++;
                }
            }
View Full Code Here

            if (menu != null && getGraphics() != null) {
                int y = borderWidth;
                if (insets != null) {
                    y += insets.top;
                }
                FontMetrics fm = getFontMetrics(getFont());
                Action m = null;
                for (Enumeration e = menu.children(); e.hasMoreElements();) {
                    idx++;
                    m = (Action) e.nextElement();                  
                    if(!m.getName().equals(ActionMenu.SEPARATOR.getName())) {
View Full Code Here

    float scale = (float) val / ((float) max - (float) min);
    g.fillRect(1, 1, (int) ((float) (bounds.width - 2) * scale),
        bounds.height - 2);
    String s = progressString != null ? progressString : (int) (scale * 100F)
        + "%"; //$NON-NLS-1$
    FontMetrics fm = getFontMetrics(getFont());
    int swidth = fm.stringWidth(s);
    g.setColor(getForeground());
    g.drawString(s, (bounds.width - swidth) / 2, bounds.height / 2
        + ( fm.getMaxAscent() / 2));
  }
View Full Code Here

            textClr = Color.darkGray; // make the text dark gray
        }

        protected void measure() {
            // get the global values we use in relation to our current font
            FontMetrics fm = this.getFontMetrics(this.getFont());
            if (fm == null) {
                return;
            }
            width = fm.stringWidth("X") * cols; //$NON-NLS-1$
            height = fm.getHeight() + 4;
        }
View Full Code Here

            }
        }

        private void centerText(String s, Graphics g, int w, int h) {
            // from the centerText method in "Java Examples in a Nutshell"
            FontMetrics fm = this.getFontMetrics(this.getFont());
            if (fm == null) {
                return;
            }
            int sWidth = fm.stringWidth(s);
            int sx = (w - sWidth) / 2;
            int sy = (h - fm.getHeight()) / 2 + fm.getAscent();
            g.drawString(s, sx, sy);
        }
View Full Code Here

TOP

Related Classes of java.awt.FontMetrics

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.