Package java.awt

Examples of java.awt.FontMetrics


        float yName = yRoomCenter + room.getNameYOffset();
        TextStyle nameStyle = room.getNameStyle();
        if (nameStyle == null) {
          nameStyle = this.preferences.getDefaultTextStyle(room.getClass());
        }         
        FontMetrics nameFontMetrics = getFontMetrics(componentFont, nameStyle);
        Rectangle2D nameBounds = nameFontMetrics.getStringBounds(roomName, g);
        itemBounds.add(xName - nameBounds.getWidth() / 2,
            yName - nameFontMetrics.getAscent());
        itemBounds.add(xName + nameBounds.getWidth() / 2,
            yName + nameFontMetrics.getDescent());
      }
      if (room.isAreaVisible()) {
        float area = room.getArea();
        if (area > 0.01f) {
          float xArea = xRoomCenter + room.getAreaXOffset();
          float yArea = yRoomCenter + room.getAreaYOffset();
          String areaText = this.preferences.getLengthUnit().getAreaFormatWithUnit().format(area);
          TextStyle areaStyle = room.getAreaStyle();
          if (areaStyle == null) {
            areaStyle = this.preferences.getDefaultTextStyle(room.getClass());
          }         
          FontMetrics areaFontMetrics = getFontMetrics(componentFont, areaStyle);
          Rectangle2D areaTextBounds = areaFontMetrics.getStringBounds(areaText, g);
          itemBounds.add(xArea - areaTextBounds.getWidth() / 2,
              yArea - areaFontMetrics.getAscent());
          itemBounds.add(xArea + areaTextBounds.getWidth() / 2,
              yArea + areaFontMetrics.getDescent());
        }
      }
    } else if (item instanceof HomePieceOfFurniture) {
      if (item instanceof HomeDoorOrWindow) {
        HomeDoorOrWindow doorOrWindow = (HomeDoorOrWindow)item;
        // Add to bounds door and window sashes
        for (Sash sash : doorOrWindow.getSashes()) {
          itemBounds.add(getDoorOrWindowSashShape(doorOrWindow, sash).getBounds2D());
        }
      } else if (item instanceof HomeFurnitureGroup) {
        itemBounds.add(getItemsBounds(g, ((HomeFurnitureGroup)item).getFurniture()));
      }
      // Add to bounds the displayed name of the piece of furniture
      HomePieceOfFurniture piece = (HomePieceOfFurniture)item;
      float xPiece = piece.getX();
      float yPiece = piece.getY();
      String pieceName = piece.getName();
      if (piece.isVisible()
          && piece.isNameVisible()
          && pieceName.length() > 0) {
        float xName = xPiece + piece.getNameXOffset();
        float yName = yPiece + piece.getNameYOffset();
        TextStyle nameStyle = piece.getNameStyle();
        if (nameStyle == null) {
          nameStyle = this.preferences.getDefaultTextStyle(piece.getClass());
        }         
        FontMetrics nameFontMetrics = getFontMetrics(componentFont, nameStyle);
        Rectangle2D nameBounds = nameFontMetrics.getStringBounds(pieceName, g);
        itemBounds.add(xName - nameBounds.getWidth() / 2,
            yName - nameFontMetrics.getAscent());
        itemBounds.add(xName + nameBounds.getWidth() / 2,
            yName + nameFontMetrics.getDescent());
      }
    } else if (item instanceof DimensionLine) {
      // Add to bounds the text bounds of the dimension line length
      DimensionLine dimensionLine = (DimensionLine)item;
      float dimensionLineLength = dimensionLine.getLength();
      String lengthText = this.preferences.getLengthUnit().getFormat().format(dimensionLineLength);
      TextStyle lengthStyle = dimensionLine.getLengthStyle();
      if (lengthStyle == null) {
        lengthStyle = this.preferences.getDefaultTextStyle(dimensionLine.getClass());
      }         
      FontMetrics lengthFontMetrics = getFontMetrics(componentFont, lengthStyle);
      Rectangle2D lengthTextBounds = lengthFontMetrics.getStringBounds(lengthText, g);
      // Transform length text bounding rectangle corners to their real location
      double angle = Math.atan2(dimensionLine.getYEnd() - dimensionLine.getYStart(),
          dimensionLine.getXEnd() - dimensionLine.getXStart());
      AffineTransform transform = new AffineTransform();
      transform.translate(dimensionLine.getXStart(), dimensionLine.getYStart());
      transform.rotate(angle);
      transform.translate(0, dimensionLine.getOffset());
      transform.translate((dimensionLineLength - lengthTextBounds.getWidth()) / 2,
          dimensionLine.getOffset() <= 0
              ? -lengthFontMetrics.getDescent() - 1
              : lengthFontMetrics.getAscent() + 1);
      GeneralPath lengthTextBoundsPath = new GeneralPath(lengthTextBounds);
      for (PathIterator it = lengthTextBoundsPath.getPathIterator(transform); !it.isDone(); ) {
        float [] pathPoint = new float[2];
        if (it.currentSegment(pathPoint) != PathIterator.SEG_CLOSE) {
          itemBounds.add(pathPoint [0], pathPoint [1]);
        }
        it.next();
      }
      // Add to bounds the end lines drawn at dimension line start and end 
      transform = new AffineTransform();
      transform.translate(dimensionLine.getXStart(), dimensionLine.getYStart());
      transform.rotate(angle);
      transform.translate(0, dimensionLine.getOffset());
      for (PathIterator it = DIMENSION_LINE_END.getPathIterator(transform); !it.isDone(); ) {
        float [] pathPoint = new float[2];
        if (it.currentSegment(pathPoint) != PathIterator.SEG_CLOSE) {
          itemBounds.add(pathPoint [0], pathPoint [1]);
        }
        it.next();
      }
      transform.translate(dimensionLineLength, 0);
      for (PathIterator it = DIMENSION_LINE_END.getPathIterator(transform); !it.isDone(); ) {
        float [] pathPoint = new float[2];
        if (it.currentSegment(pathPoint) != PathIterator.SEG_CLOSE) {
          itemBounds.add(pathPoint [0], pathPoint [1]);
        }
        it.next();
      }
    } else if (item instanceof Label) {
      // Add to bounds the displayed text of a label
      Label label = (Label)item;
      float xLabel = label.getX();
      float yLabel = label.getY();
      String labelText = label.getText();
      TextStyle labelStyle = label.getStyle();
      if (labelStyle == null) {
        labelStyle = this.preferences.getDefaultTextStyle(label.getClass());
      }         
      FontMetrics labelFontMetrics = getFontMetrics(componentFont, labelStyle);
      Rectangle2D labelBounds = labelFontMetrics.getStringBounds(labelText, g);
      itemBounds.add(xLabel - labelBounds.getWidth() / 2,
          yLabel - labelFontMetrics.getAscent());
      itemBounds.add(xLabel + labelBounds.getWidth() / 2,
          yLabel + labelFontMetrics.getDescent());
    } else if (item instanceof Compass) {
      Compass compass = (Compass)item;
      AffineTransform transform = AffineTransform.getTranslateInstance(compass.getX(), compass.getY());
      transform.scale(compass.getDiameter(), compass.getDiameter());
      transform.rotate(compass.getNorthDirection());
View Full Code Here


   * Returns the coordinates of the bounding rectangle of the <code>text</code> centered at
   * the point (<code>x</code>,<code>y</code>)
   */
  public float [][] getTextBounds(String text, TextStyle style,
                                  float x, float y, float angle) {
    FontMetrics fontMetrics = getFontMetrics(getFont(), style);
    Rectangle2D textBounds = fontMetrics.getStringBounds(text, null);
    float halfTextLength = (float)textBounds.getWidth() / 2;
    float textBaseLine = fontMetrics.getAscent();
    if (angle == 0) {
      return new float [][] {
          {x - halfTextLength, y - textBaseLine},
          {x + halfTextLength, y - textBaseLine},
          {x + halfTextLength, y + (float)textBounds.getHeight() - textBaseLine},
View Full Code Here

   */
  protected FontMetrics getFontMetrics(Font defaultFont, TextStyle textStyle) {
    if (this.fontsMetrics == null) {
      this.fontsMetrics = new WeakHashMap<TextStyle, FontMetrics>();
    }
    FontMetrics fontMetrics = this.fontsMetrics.get(textStyle);
    if (fontMetrics == null) {
      fontMetrics = getFontMetrics(getFont(defaultFont, textStyle));
      this.fontsMetrics.put(textStyle, fontMetrics);
    }
    return fontMetrics;
View Full Code Here

                                                               row, column);

    String s = obj != null ? obj.toString() : "";
    int w = table.getTableHeader().getColumnModel().getColumn(column).getWidth();

    FontMetrics fm = label.getFontMetrics(label.getFont());
   
    int fmW = fm.stringWidth(s);
   
    // s = w + "/" + fmW + ":" + s;

    int nChars = 3;
    int m = s.length() / 2;
    String s2 = s;
    while((fmW+10) > w && s2.length() > nChars && s2.length() > 5) {     
      int n1 = nChars / 2;
      int n2 = nChars - n1;
      String left  = s.substring(0, m - n1);
      String right = s.substring(m + n2);
      s2 = left + "..." + right;     
      fmW = fm.stringWidth(s2);
      nChars++;
    }
   
    label.setText(s2);
   
View Full Code Here

      g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
      final GradientPaint gp = new GradientPaint(0f, 0f, backgroundColor, 0f,
          this.getHeight() / 2.f, textColor, true);
      g2d.setPaint(gp);
      g2d.setFont(font);
      final FontMetrics metrics = g2d.getFontMetrics();
      int i = 0;
      for (String s : text) {
        width = metrics.stringWidth(s);
        g2d.drawString(s, this.getWidth() / 2 - width / 2,
            textPos + ((metrics.getHeight() + lineSpacing) * i));
        ++i;
      }
    }
  }
 
View Full Code Here

    this.prefferedSize = new Dimension();
    final BufferedImage image = new BufferedImage(100, 100,
        BufferedImage.TYPE_INT_RGB);
    final Graphics2D g2d = image.createGraphics();
    g2d.setFont(font);
    final FontMetrics metrics = g2d.getFontMetrics();
    this.lineHeight = metrics.getHeight();
    this.prefferedSize.height = this.lineHeight * 8;
    for (String s : text) {
      prefferedSize.width = Math.max(prefferedSize.width,
          metrics.stringWidth(s));
    }
    this.prefferedSize.width = prefferedSize.width + 6
        * metrics.stringWidth(" ");
  }
View Full Code Here

        defaultStrokeColor = Color.black;

        graduations.fontSize = 12;
        graduations.font = new Font("DejaVu Sans Mono", 0, graduations.fontSize);
        graduations.fontColor = new Color(235, 235, 235, 255);
        graduations.fontMetrics = new FontMetrics(graduations.font) {
        };
        graduations.topMargin = 2;
        graduations.leftMargin = 2;
        graduations.textTopPosition = graduations.topMargin + graduations.fontSize;
        graduations.textBottomMargin = 3;


        tip.fontSize = 12;
        tip.font = new Font("DejaVu Sans Mono", 0, graduations.fontSize);
        tip.fontColor = new Color(25, 25, 25, 160);
        tip.fontMetrics = new FontMetrics(graduations.font) {
        };
        tip.backgroundColor = new Color(255, 255, 255, 160);

        hookLength = 16;
View Full Code Here

        this.startTime = startTime;
        this.chronoImage = new BufferedImage(roi.width, roi.height, BufferedImage.TYPE_3BYTE_BGR);
        this.chronoGraphics = (Graphics2D)chronoImage.getGraphics();

        Font bigFont, smallFont;
        FontMetrics bigFontMetrics;
        Rectangle2D bounds;
        int fontSize = 10;
        do {
            bigFont   = new Font("Sans", Font.BOLD, fontSize);
            smallFont = new Font("Sans", Font.BOLD, fontSize*9/10);
            bigFontMetrics = chronoGraphics.getFontMetrics(bigFont);
            bounds = bigFontMetrics.getStringBounds("0′00″0", chronoGraphics);
            fontSize += 1;
        } while (bounds.getWidth()*1.1 < roi.width && bounds.getHeight()*1.1 < roi.height);
        this.bigFont = bigFont;
        this.smallFont = smallFont;
        this.bigFontMetrics = bigFontMetrics;
View Full Code Here

        double strW = fm.stringWidth(str);
        g.drawString(str, xPos, (int)(yPos + fm.getAscent()*.48 + .5));
    }
    public static void drawStringRightCenter(Graphics g, String str, int xPos, int yPos)
    {
        FontMetrics fm = g.getFontMetrics();
        double strW = fm.stringWidth(str);
        g.drawString(str, xPos  - (int)(strW +.5), (int)(yPos + fm.getAscent()*.48 + .5));
    }
View Full Code Here

    }

    // y top
    public static void drawStringTop(Graphics g, String str, int xPos, int yPos)
    {
        FontMetrics fm = g.getFontMetrics();
        double strW = fm.stringWidth(str);
        g.drawString(str, xPos + (int)(-strW/2.0 + .5), yPos + fm.getAscent());
    }
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.