/**
   * Renders a label for the given rectangle. The label is fitted into the rectangle if possible.
   *
   * @param event the paint event to work with on
   * @param text the text to render
   * @param color the color to use
   * @param bounds the rectangle bounds
   */
  protected void render(
      final PaintEvent event, final String text, final RGB color, final IRectangle<N> bounds) {
    if (text != null) {
      // quite strange to create a new font object all the time?
      // it did not work when I tried to reuse a long-living font object!
      final Font font = new Font(event.display, fontName, 16, SWT.BOLD);
      try {
        event.gc.setFont(font);
        final Color c = new Color(event.display, color);
        try {
          event.gc.setForeground(c);
        } finally {
          c.dispose();
        }

        final Point p = event.gc.textExtent(text);
        p.x = p.x * 12 / 10; // make some space
        final float scale =
            (float) Math.min(bounds.getWidth() / (double) p.x, bounds.getHeight() / (double) p.y);
        final Transform transform = new Transform(event.display);
        try {
          transform.translate(
              (int) (bounds.getX() + bounds.getWidth() / 12d),
              bounds.getY() + (bounds.getHeight() - scale * p.y) / 2);
          transform.scale(scale, scale);
          event.gc.setTransform(transform);
          event.gc.drawString(text, 0, 0, true);
        } finally {
          transform.dispose();
        }
      } finally {
        font.dispose();
      }
    }
  }
示例#2
0
 /**
  * Applies a scale transform.
  *
  * @param scaleX the scale factor along the x-axis.
  * @param scaleY the scale factor along the y-axis.
  */
 @Override
 public void scale(double scaleX, double scaleY) {
   Transform swtTransform = new Transform(this.gc.getDevice());
   this.gc.getTransform(swtTransform);
   swtTransform.scale((float) scaleX, (float) scaleY);
   this.gc.setTransform(swtTransform);
   swtTransform.dispose();
 }