1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.trend.impl;
14 import java.awt.Color;
16 import java.awt.Graphics2D;
17 import java.awt.font.GlyphVector;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.Rectangle2D;
21 import org.simantics.g2d.utils.GridUtil;
22 import org.simantics.scenegraph.ExportableWidget.OutputWidget;
25 * TextNode, the text is centered and scaled to fit inside the bounds.
27 * @author toni.kalajainen
30 public class TextNode extends TrendGraphicalNode {
32 private static final long serialVersionUID = 8508750881358776559L;
36 Color color = Color.BLACK;
38 transient double sx, sy, tw, th, tx, ty;
41 public void setFont(Font font) {
46 public void setText(String text) {
51 public void setColor(Color color) {
55 public void layout() {
56 GlyphVector glyphVector = font.createGlyphVector(GridUtil.frc, text);
57 Rectangle2D tr = glyphVector.getVisualBounds().getBounds2D();
61 sy = getHeight() / th;
65 // Fix horiz scale param sx
68 // Scale-down to fit rectangle width
75 // Text is ok horizontally. No need to scale.
79 // Fix vert scale param sy
82 // Scale-down to fit rectangle height
89 // Text is ok vertically. No need to scale.
93 tx += (bounds.getWidth() - tw) / 2;
94 ty += (bounds.getHeight() - th) / 2;
98 protected void doRender(Graphics2D g) {
99 if (text == null || bounds == null || font == null || color == null)
105 AffineTransform at = g.getTransform();
107 g.translate( bounds.getX(), bounds.getY() );
111 g.drawString(text, 0, 0);
112 g.setTransform( at );