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