示例#1
0
 void drawCenteredString(Graphics2D g2d, String string, int x0, int y0, float angle) {
   FontRenderContext frc = g2d.getFontRenderContext();
   Rectangle2D bounds = g2d.getFont().getStringBounds(string, frc);
   LineMetrics metrics = g2d.getFont().getLineMetrics(string, frc);
   if (angle == 0) {
     g2d.drawString(string, x0 - (float) bounds.getWidth() / 2, y0 + metrics.getHeight() / 2);
   } else {
     g2d.rotate(angle, x0, y0);
     g2d.drawString(string, x0 - (float) bounds.getWidth() / 2, y0 + metrics.getHeight() / 2);
     g2d.rotate(-angle, x0, y0);
   }
 }
示例#2
0
  private static final void drawShadowedStringCentered(
      final Graphics2D g2d,
      final String str,
      final double x,
      final double y,
      final Font font,
      final Paint paint,
      final double shadowOffset,
      final Paint shadowPaint) {

    g2d.setFont(font);

    final FontRenderContext frc = g2d.getFontRenderContext();
    final Rectangle2D bounds = font.getStringBounds(str, frc);
    final LineMetrics metrics = font.getLineMetrics(str, frc);
    final double width = bounds.getWidth(); // The width of our text
    final float lineHeight = metrics.getHeight(); // Total line height
    final float ascent = metrics.getAscent(); // Top of text to baseline

    final double cx = (x + (0 - width) / 2);
    final double cy = (y + (0 - lineHeight) / 2 + ascent);

    if (shadowOffset > 0) {
      drawString(g2d, str, cx + shadowOffset, cy - shadowOffset, shadowPaint);
      drawString(g2d, str, cx + shadowOffset, cy + shadowOffset, shadowPaint);
      drawString(g2d, str, cx - shadowOffset, cy - shadowOffset, shadowPaint);
      drawString(g2d, str, cx - shadowOffset, cy + shadowOffset, shadowPaint);
    }
    drawString(g2d, str, cx, cy, paint);
  }
  /*
   * Test method for 'java.awt.FontMetrics.getStringBounds(char[], int, int, Graphics)'
   */
  public final void testGetStringBoundsCharArrayIntIntGraphics() {
    String str = "toremove-This is a very long string for getting bounds!-toremove";
    char[] chars = str.toCharArray();
    int width = 237;
    Rectangle2D rect = fm.getStringBounds(chars, 9, 55, g);
    LineMetrics lm = fm.getLineMetrics(" ", g);
    assertNotNull(rect);
    assertEquals(rect, new Rectangle2D.Float(0, -lm.getAscent(), width, lm.getHeight()));

    try {
      rect = fm.getStringBounds(chars, 10, 155, g);
      fail(
          "IndexOutOfBoundsException wasn't thrown, when end index is more than count of elements in CharacterIterator");
    } catch (IndexOutOfBoundsException e) {
      // expected
    }

    try {
      rect = fm.getStringBounds(chars, -1, 55, g);
      fail("IndexOutOfBoundsException wasn't thrown, when initial offset < 0");
    } catch (IndexOutOfBoundsException e) {
      // expected
    }

    try {
      rect = fm.getStringBounds(chars, 45, 40, g);
      fail("IndexOutOfBoundsException wasn't thrown, when end index is less than start index");
    } catch (IndexOutOfBoundsException e) {
      // expected
    }
  }
  /*
   * Test method for 'java.awt.FontMetrics.getStringBounds(String, int, int, Graphics)'
   */
  public final void testGetStringBoundsStringIntIntGraphics() {
    String str = "toremove-This is a very long string for getting bounds!-toremove";
    int width = 237;
    Rectangle2D rect = fm.getStringBounds(str, 9, 55, g);
    LineMetrics lm = fm.getLineMetrics(" ", g);
    assertNotNull(rect);
    assertEquals(rect, new Rectangle2D.Float(0, -lm.getAscent(), width, lm.getHeight()));

    try {
      rect = fm.getStringBounds(str, 10, 65, g);
      fail(
          "IndexOutOfBoundsException wasn't thrown, when end index is more than number of chars in string");
    } catch (IndexOutOfBoundsException e) {
      // expected
    }

    try {
      rect = fm.getStringBounds(str, -1, 55, g);
      fail("IndexOutOfBoundsException wasn't thrown, when initial offset < 0");
    } catch (IndexOutOfBoundsException e) {
      // expected
    }

    try {
      rect = fm.getStringBounds(str, 45, 10, g);
      fail("IndexOutOfBoundsException wasn't thrown, when end index is less than start index");
    } catch (IndexOutOfBoundsException e) {
      // expected
    }
  }
  @Override
  public void paintEntity(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(Main.getHandlerForElement(this).getFontHandler().getFont());
    colorize(g2); // enable colors
    g2.setColor(fgColor);

    g2.fillOval(0, 0, getRectangle().width, getRectangle().height);
    // Measure the font and the message
    Rectangle2D bounds =
        Main.getHandlerForElement(this)
            .getFontHandler()
            .getFont()
            .getStringBounds("H", g2.getFontRenderContext());
    LineMetrics metrics =
        Main.getHandlerForElement(this)
            .getFontHandler()
            .getFont()
            .getLineMetrics("H", g2.getFontRenderContext());
    float width = (float) bounds.getWidth(); // The width of our text
    float lineheight = metrics.getHeight(); // Total line height
    float ascent = metrics.getAscent(); // Top of text to baseline

    // Now display the message centered horizontally and vertically in this
    float x0 = (getRectangle().width - width) / 2;
    float y0 = (getRectangle().height - lineheight) / 2 + ascent;
    g2.setColor(Color.WHITE);
    g2.drawString("H", x0, y0);
    g2.setColor(fgColor);
  }
 /*
  * Test method for 'java.awt.FontMetrics.getStringBounds(String, Graphics)'
  */
 public final void testGetStringBoundsStringGraphics() {
   String str = "This is a very long string for getting bounds!";
   int width = 237;
   Rectangle2D rect = fm.getStringBounds(str, g);
   LineMetrics lm = fm.getLineMetrics(" ", g);
   assertNotNull(rect);
   assertEquals(new Rectangle2D.Float(0, -lm.getAscent(), width, lm.getHeight()), rect);
 }
示例#7
0
  /**
   * Returns the height of the band.
   *
   * @param g2 the graphics device.
   * @return The height of the band.
   */
  public double getHeight(Graphics2D g2) {

    double result = 0.0;
    if (this.markers.size() > 0) {
      LineMetrics metrics = this.font.getLineMetrics("123g", g2.getFontRenderContext());
      result =
          this.topOuterGap
              + this.topInnerGap
              + metrics.getHeight()
              + this.bottomInnerGap
              + this.bottomOuterGap;
    }
    return result;
  }
示例#8
0
    /** Method to repaint this ThreeDSideView. */
    @Override
    public void paint(Graphics g) {
      Dimension dim = getSize();
      g.setColor(Color.WHITE);
      g.fillRect(0, 0, dim.width, dim.height);
      g.setColor(Color.BLACK);
      g.drawLine(0, 0, 0, dim.height - 1);
      g.drawLine(0, dim.height - 1, dim.width - 1, dim.height - 1);
      g.drawLine(dim.width - 1, dim.height - 1, dim.width - 1, 0);
      g.drawLine(dim.width - 1, 0, 0, 0);

      String layerName = (String) dialog.threeDLayerList.getSelectedValue();
      Layer selectedLayer = dialog.curTech.findLayer(layerName);
      for (Iterator<Layer> it = dialog.curTech.getLayers(); it.hasNext(); ) {
        Layer layer = it.next();
        if (layer.isPseudoLayer()) continue;
        // if (!layer.isVisible()) continue;
        if (layer == selectedLayer) g.setColor(Color.RED);
        else g.setColor(Color.BLACK);
        GenMath.MutableDouble thickness = dialog.threeDThicknessMap.get(layer);
        GenMath.MutableDouble distance = dialog.threeDDistanceMap.get(layer);
        double dis = distance.doubleValue() + thickness.doubleValue() / 2;
        int yValue =
            dim.height - (int) ((dis - lowHeight) / (highHeight - lowHeight) * dim.height + 0.5);
        int yHeight = (int) (thickness.doubleValue() / (highHeight - lowHeight) * dim.height + 0.5);
        if (yHeight == 0) {
          g.drawLine(0, yValue, dim.width / 3, yValue);
        } else {
          // yHeight -= 4;
          int firstPart = dim.width / 6;
          int pointPos = dim.width / 4;
          g.drawLine(0, yValue - yHeight / 2, firstPart, yValue - yHeight / 2);
          g.drawLine(0, yValue + yHeight / 2, firstPart, yValue + yHeight / 2);
          g.drawLine(firstPart, yValue - yHeight / 2, pointPos, yValue);
          g.drawLine(firstPart, yValue + yHeight / 2, pointPos, yValue);
          g.drawLine(pointPos, yValue, dim.width / 3, yValue);
        }
        String string = layer.getName();
        Font font = new Font(User.getDefaultFont(), Font.PLAIN, 9);
        g.setFont(font);
        FontRenderContext frc = new FontRenderContext(null, true, true);
        GlyphVector gv = font.createGlyphVector(frc, string);
        LineMetrics lm = font.getLineMetrics(string, frc);
        double txtHeight = lm.getHeight();
        Graphics2D g2 = (Graphics2D) g;
        g2.drawGlyphVector(
            gv, dim.width / 3 + 1, (float) (yValue + txtHeight / 2) - lm.getDescent());
      }
    }
  @SuppressWarnings("boxing")
  private boolean lmEquals(LineMetrics lm1, LineMetrics lm2) {
    assertEquals("Ascent", lm1.getAscent(), lm2.getAscent());
    assertEquals("Baseline Index", lm1.getBaselineIndex(), lm2.getBaselineIndex());
    float[] offsets = lm2.getBaselineOffsets();
    assertNotNull(offsets);
    for (int i = 0; i < offsets.length; i++) {
      assertEquals("Baseline offset[" + i + "]", lm1.getBaselineOffsets()[i], offsets[i]);
    }
    assertEquals("Descent", lm1.getDescent(), lm2.getDescent());
    assertEquals("Height", lm1.getHeight(), lm2.getHeight());
    assertEquals("Leading", lm1.getLeading(), lm2.getLeading());
    assertEquals("NumChars", lm1.getNumChars(), lm2.getNumChars());
    assertEquals(
        "Strikethrough offset", lm1.getStrikethroughOffset(), lm2.getStrikethroughOffset());
    assertEquals(
        "Strikethrough thickness",
        lm1.getStrikethroughThickness(),
        lm2.getStrikethroughThickness());
    assertEquals("Underline offset", lm1.getUnderlineOffset(), lm2.getUnderlineOffset());
    assertEquals("Underline thickness", lm1.getUnderlineThickness(), lm2.getUnderlineThickness());

    return true;
  }
示例#10
0
 /**
  * Draws a watermark that shows the version number if it is < 1.0
  *
  * @param g2 the graphics context
  */
 private void drawWatermark(Graphics2D g2) {
   if ("hide".equals(System.getProperty("info.gridworld.gui.watermark"))) return;
   g2 = (Graphics2D) g2.create();
   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
   Rectangle rect = getBounds();
   g2.setPaint(new Color(0xE3, 0xD3, 0xD3));
   final int WATERMARK_FONT_SIZE = 100;
   String s = resources.getString("version.id");
   if ("1.0".compareTo(s) <= 0) return;
   g2.setFont(new Font("SansSerif", Font.BOLD, WATERMARK_FONT_SIZE));
   FontRenderContext frc = g2.getFontRenderContext();
   Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
   float centerX = rect.x + rect.width / 2;
   float centerY = rect.y + rect.height / 2;
   float leftX = centerX - (float) bounds.getWidth() / 2;
   LineMetrics lm = g2.getFont().getLineMetrics(s, frc);
   float baselineY = centerY - lm.getHeight() / 2 + lm.getAscent();
   g2.drawString(s, leftX, baselineY);
 }