/** * 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()); } }
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); }
/** 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); }
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()); } }
/** * Draw the label's text. This method assumes that the text renderer context has already been set * up. * * @param textRenderer renderer to use. */ protected void doDrawText(TextRenderer textRenderer) { Color color = this.material.getDiffuse(); Color backgroundColor = this.computeBackgroundColor(color); float opacity = (float) this.getOpacity(); int x = this.screenPoint.x; int y = this.screenPoint.y; float[] compArray = new float[3]; if (AVKey.TEXT_EFFECT_SHADOW.equals(this.effect) && backgroundColor != null) { backgroundColor.getRGBColorComponents(compArray); textRenderer.setColor(compArray[0], compArray[1], compArray[2], opacity); this.drawMultiLineText(textRenderer, x + 1, y - 1); } color.getRGBColorComponents(compArray); textRenderer.setColor(compArray[0], compArray[1], compArray[2], opacity); this.drawMultiLineText(textRenderer, x, y); }
public void drawText(String text, double x, double y) { if (textRenderer == null) throw new RuntimeException(); // gl.glDisable(GL.GL_DEPTH_TEST); // Uses 3D rendering so it can take advantage of the glTranslate // function textRenderer.begin3DRendering(); textRenderer.setColor(currentColor); textRenderer.draw3D( text, (float) (scaleX * (brushPos.getX() + x)), (float) (scaleY * (brushPos.getY() + y)), 0, 1); textRenderer.end3DRendering(); textRenderer.flush(); // gl.glEnable(GL.GL_DEPTH_TEST); }
/** * Draw the label's text. This method sets up the text renderer, and then calls {@link * #doDrawText(TextRenderer) doDrawText} to actually draw the text. * * @param dc Current draw context. * @param textRenderer Text renderer. */ protected void drawText(DrawContext dc, TextRenderer textRenderer) { GL gl = dc.getGL(); Angle heading = this.rotation; double headingDegrees; if (heading != null) headingDegrees = heading.degrees; else headingDegrees = 0; boolean matrixPushed = false; try { int x = this.screenPoint.x; int y = this.screenPoint.y; if (headingDegrees != 0) { gl.glPushMatrix(); matrixPushed = true; gl.glTranslated(x, y, 0); gl.glRotated(headingDegrees, 0, 0, 1); gl.glTranslated(-x, -y, 0); } if (this.isDrawInterior()) this.drawInterior(dc); textRenderer.begin3DRendering(); try { this.doDrawText(textRenderer); // Draw other labels that share the same text renderer configuration, if possible. if (this.isEnableBatchRendering()) this.drawBatchedText(dc, textRenderer); } finally { textRenderer.end3DRendering(); } } finally { if (matrixPushed) { gl.glPopMatrix(); } } }
// 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(); }
protected void drawMultiLineText(TextRenderer textRenderer, int x, int y) { if (this.lines == null) { String msg = Logging.getMessage("nullValue.StringIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } for (int i = 0; i < this.lines.length; i++) { String line = this.lines[i]; Rectangle2D bounds = this.lineBounds[i]; int xAligned = x; if (this.textAlign.equals(AVKey.CENTER)) xAligned = x - (int) (bounds.getWidth() / 2); else if (this.textAlign.equals(AVKey.RIGHT)) xAligned = x - (int) (bounds.getWidth()); y -= this.lineHeight; textRenderer.draw3D(line, xAligned, y, 0, 1); y -= this.lineSpacing; } }
public void finalizeDisplay() { if (textRenderer != null) textRenderer.flush(); gl.glFlush(); }
public Rectangle2D getSizeOfText(String text) { if (textRenderer == null) throw new RuntimeException(); return textRenderer.getBounds(text); }
public void reinitRenderer() { renderer = new TextRenderer(renderer.getFont(), antialised, fractionalMetrics, null, mipmap); }
public void disposeRenderer() { renderer.flush(); renderer.dispose(); }
/** Here we actually draw the scene. */ public void display(GLAutoDrawable drawable) { super.display(drawable); System.out.println("GLScene - display"); GL gl = drawable.getGL(); try { // Update velocities and positions of all text float deltaT = 0.1f; Point2f tmp = new Point2f(); for (Iterator<TextInfo> iter = textInfo.iterator(); iter.hasNext(); ) { TextInfo info = (TextInfo) iter.next(); // Randomize things a little bit at run time if (random.nextInt(1000) == 0) { info.angularVelocity = INIT_ANG_VEL_MAG * (randomAngle() - 180); info.velocity = randomVelocity(INIT_VEL_MAG, INIT_VEL_MAG); } // Now update angles and positions info.angle += info.angularVelocity * deltaT; tmp.set(info.velocity); tmp.scale(deltaT); info.position.add(tmp); // Update color info.curTime += deltaT; if (info.curTime > 2 * Math.PI) { info.curTime -= 2 * Math.PI; } int rgb = Color.HSBtoRGB(info.h, (float) (0.5 * (1 + Math.sin(info.curTime)) * info.s), info.v); info.r = ((rgb >> 16) & 0xFF) / 255.0f; info.g = ((rgb >> 8) & 0xFF) / 255.0f; info.b = (rgb & 0xFF) / 255.0f; // Wrap angles and positions if (info.angle < 0) { info.angle += 360; } else if (info.angle > 360) { info.angle -= 360; } // Use maxTextWidth to avoid popping in/out at edges // Would be better to do oriented bounding rectangle computation if (info.position.x < -maxTextWidth) { info.position.x = info.position.x + glComposite.getCanvas().getWidth() + 2 * maxTextWidth; } else if (info.position.x > glComposite.getCanvas().getWidth() + maxTextWidth) { info.position.x = info.position.x - glComposite.getCanvas().getWidth() - 2 * maxTextWidth; } if (info.position.y < -maxTextWidth) { info.position.y = info.position.y + glComposite.getCanvas().getHeight() + 2 * maxTextWidth; } else if (info.position.y > glComposite.getCanvas().getHeight() + maxTextWidth) { info.position.y = info.position.y - glComposite.getCanvas().getHeight() - 2 * maxTextWidth; } } gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glComposite .getGLU() .gluOrtho2D( 0, glComposite.getCanvas().getWidth(), 0, glComposite.getCanvas().getHeight()); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); // Draw the background texture backgroundTexture.enable(); backgroundTexture.bind(); TextureCoords coords = backgroundTexture.getImageTexCoords(); int w = glComposite.getCanvas().getWidth(); int h = glComposite.getCanvas().getHeight(); float fw = w / 100.0f; float fh = h / 100.0f; gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE); gl.glBegin(GL.GL_QUADS); gl.glTexCoord2f(fw * coords.left(), fh * coords.bottom()); gl.glVertex3f(0, 0, 0); gl.glTexCoord2f(fw * coords.right(), fh * coords.bottom()); gl.glVertex3f(w, 0, 0); gl.glTexCoord2f(fw * coords.right(), fh * coords.top()); gl.glVertex3f(w, h, 0); gl.glTexCoord2f(fw * coords.left(), fh * coords.top()); gl.glVertex3f(0, h, 0); gl.glEnd(); backgroundTexture.disable(); // Render all text renderer.beginRendering(w, h); // Note we're doing some slightly fancy stuff to position the text. // We tell the text renderer to render the text at the origin, and // manipulate the modelview matrix to put the text where we want. gl.glMatrixMode(GL.GL_MODELVIEW); // First render drop shadows renderer.setColor(0, 0, 0, 0.5f); for (Iterator<TextInfo> iter = textInfo.iterator(); iter.hasNext(); ) { TextInfo info = (TextInfo) iter.next(); gl.glLoadIdentity(); gl.glTranslatef( info.position.x + dropShadowDistance, info.position.y - dropShadowDistance, 0); gl.glRotatef(info.angle, 0, 0, 1); renderer.draw(info.text, 0, 0); renderer.flush(); } // Now render the actual text for (Iterator<TextInfo> iter = textInfo.iterator(); iter.hasNext(); ) { TextInfo info = (TextInfo) iter.next(); gl.glLoadIdentity(); gl.glTranslatef(info.position.x, info.position.y, 0); gl.glRotatef(info.angle, 0, 0, 1); renderer.setColor(info.r, info.g, info.b, 1); renderer.draw(info.text, 0, 0); renderer.flush(); } renderer.endRendering(); } catch (Exception e) { e.printStackTrace(); } }
public Font getFont() { return renderer.getFont(); }
public void beginRendering() { renderer.beginRendering(drawable.getViewportWidth(), drawable.getViewportHeight()); }
public void endRendering() { renderer.endRendering(); }
public void beginRendering() { renderer.begin3DRendering(); }
public void setColor(float r, float g, float b, float a) { renderer.setColor(r, g, b, a); }