예제 #1
0
  /**
   * Compute the bounds of the text, if necessary.
   *
   * @param dc the current DrawContext.
   */
  protected void computeBoundsIfNeeded(DrawContext dc) {
    // Do not compute bounds if they are available. Computing text bounds is expensive, so only do
    // this
    // calculation if necessary.
    if (this.bounds != null) return;

    TextRenderer textRenderer =
        OGLTextRenderer.getOrCreateTextRenderer(dc.getTextRendererCache(), this.getFont());

    int width = 0;
    int maxLineHeight = 0;
    this.lineBounds = new Rectangle2D[this.lines.length];

    for (int i = 0; i < this.lines.length; i++) {
      Rectangle2D lineBounds = textRenderer.getBounds(lines[i]);
      width = (int) Math.max(lineBounds.getWidth(), width);

      double thisLineHeight = Math.abs(lineBounds.getY());
      maxLineHeight = (int) Math.max(thisLineHeight, maxLineHeight);

      this.lineBounds[i] = lineBounds;
    }
    this.lineHeight = maxLineHeight;

    // Compute final height using maxLineHeight and number of lines
    this.bounds =
        new Rectangle(
            this.lines.length,
            maxLineHeight,
            width,
            this.lines.length * maxLineHeight + this.lines.length * this.lineSpacing);
  }
    public void drawTextEdge(ModelImpl objectModel) {
      Renderable renderable = objectModel.getObj();
      TextDataImpl textData = (TextDataImpl) renderable.getTextData();
      if (textData != null) {
        model.colorMode.textColor(this, textData, objectModel);
        model.sizeMode.setSizeFactor2d(model.edgeSizeFactor, textData, objectModel);
        if (edgeRefresh) {
          builder.buildEdgeText((EdgeData) renderable, textData, model, currentTimeInterval);
        }
        if (textData.getSizeFactor() * renderer.getCharWidth('a') < PIXEL_LIMIT) {
          return;
        }
        String txt = textData.getLine().getText();
        Rectangle2D r = renderer.getBounds(txt);
        float posX =
            renderable.getModel().getViewportX()
                + (float) r.getWidth() / -2 * textData.getSizeFactor();
        float posY =
            renderable.getModel().getViewportY()
                + (float) r.getHeight() / -2 * textData.getSizeFactor();
        r.setRect(
            0,
            0,
            r.getWidth() / drawable.getDraggingMarkerX(),
            r.getHeight() / drawable.getDraggingMarkerY());
        textData.getLine().setBounds(r);

        renderer.draw3D(txt, posX, posY, 0, textData.getSizeFactor());
      }
    }
예제 #3
0
  /** The scene's implementation of the init method. */
  public void init(GLAutoDrawable drawable) {
    super.init(drawable);

    System.out.println("FlyingTextScene - init");

    final GL gl = drawable.getGL();

    // Create the text renderer
    renderer = new TextRenderer(new Font("Palatino Linotype", Font.TRUETYPE_FONT, 72), true);

    // Create the background texture
    BufferedImage bgImage = new BufferedImage(2, 2, BufferedImage.TYPE_BYTE_GRAY);
    Graphics2D g = bgImage.createGraphics();
    g.setColor(new Color(0.3f, 0.3f, 0.3f));
    g.fillRect(0, 0, 2, 2);
    g.setColor(new Color(0.7f, 0.7f, 0.7f));
    g.fillRect(0, 0, 1, 1);
    g.fillRect(1, 1, 1, 1);
    g.dispose();
    backgroundTexture = TextureIO.newTexture(bgImage, false);
    backgroundTexture.bind();
    backgroundTexture.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
    backgroundTexture.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
    backgroundTexture.setTexParameteri(GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
    backgroundTexture.setTexParameteri(GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);

    width = glComposite.getCanvas().getWidth();
    height = glComposite.getCanvas().getHeight();

    // Compute maximum width of text we're going to draw to avoid
    // popping in/out at edges
    maxTextWidth = (int) renderer.getBounds("Java 2D").getWidth();
    maxTextWidth = Math.max(maxTextWidth, (int) renderer.getBounds("OpenGL").getWidth());

    // Create random text
    textInfo.clear();
    for (int i = 0; i < NUM_STRINGS; i++) {
      textInfo.add(randomTextInfo());
    }

    // Set up properties; note we don't need the depth buffer in this demo
    gl.glDisable(GL.GL_DEPTH_TEST);

    glComposite.initAnimator(drawable, 100);
  }
 private void display(GLAutoDrawable drawable, Point p) {
   if (renderer == null) {
     renderer = new TextRenderer(new Font("SansSerif", Font.PLAIN, 10), true, true);
   }
   Neuron[] n = cells[p.x][p.y];
   renderer.begin3DRendering();
   renderer.setColor(0, 0, 1, 0.8f);
   float s1 = n[0].getState(), s2 = n[1].getState();
   float avg = (s1 + s2) / 2;
   String s = String.format("%5.3f", avg);
   Rectangle2D rect = renderer.getBounds(s);
   renderer.draw3D(s, p.x, p.y, 0, .7f); // TODO fix string n lines
   renderer.end3DRendering();
   GL gl = drawable.getGL();
   gl.glRectf(p.x, p.y - 2, p.x + (float) rect.getWidth() * avg * .7f, p.y - 1);
 }
예제 #5
0
  // Draw the scale label
  private void drawLabel(DrawContext dc, String text, Vec4 screenPoint) {
    TextRenderer textRenderer =
        OGLTextRenderer.getOrCreateTextRenderer(dc.getTextRendererCache(), this.defaultFont);

    Rectangle2D nameBound = textRenderer.getBounds(text);
    int x = (int) (screenPoint.x() - nameBound.getWidth() / 2d);
    int y = (int) screenPoint.y();

    textRenderer.begin3DRendering();

    textRenderer.setColor(this.getBackgroundColor(this.color));
    textRenderer.draw(text, x + 1, y - 1);
    textRenderer.setColor(this.color);
    textRenderer.draw(text, x, y);

    textRenderer.end3DRendering();
  }
    public void drawTextNode(ModelImpl objectModel) {
      Renderable renderable = objectModel.getObj();
      TextDataImpl textData = (TextDataImpl) renderable.getTextData();
      if (textData != null) {
        model.colorMode.textColor(this, textData, objectModel);
        model.sizeMode.setSizeFactor3d(model.nodeSizeFactor, textData, objectModel);
        if (nodeRefresh) {
          builder.buildNodeText((NodeData) renderable, textData, model, currentTimeInterval);
        }
        String txt = textData.getLine().getText();
        Rectangle2D r = renderer.getBounds(txt);
        textData.getLine().setBounds(r);
        float posX = renderable.x() + (float) r.getWidth() / -2 * textData.getSizeFactor();
        float posY = renderable.y() + (float) r.getHeight() / -2 * textData.getSizeFactor();
        float posZ = renderable.getRadius();

        renderer.draw3D(txt, posX, posY, posZ, textData.getSizeFactor());
      }
    }
예제 #7
0
 public Rectangle2D getSizeOfText(String text) {
   if (textRenderer == null) throw new RuntimeException();
   return textRenderer.getBounds(text);
 }