1 /*
2 * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /* $Id: TextObject.java,v 8afef6dd0d58 2007/09/14 08:29:58 maxberger $ */
18
19 package net.sourceforge.jeuclid.layout;
20
21 import java.awt.Color;
22 import java.awt.Graphics2D;
23 import java.awt.font.TextLayout;
24 import java.awt.geom.AffineTransform;
25
26 /**
27 * @version $Revision: 8afef6dd0d58 $
28 */
29 public class TextObject implements GraphicsObject {
30
31 private final TextLayout layout;
32
33 private final Color color;
34
35 private final float xoffset;
36
37 private final float yoffset;
38
39 private final AffineTransform trans;
40
41 /**
42 * Default Constructor.
43 *
44 * @param textLayout
45 * Text Layout.
46 * @param xo
47 * X-Offset
48 * @param textColor
49 * text color.
50 */
51 public TextObject(final TextLayout textLayout, final float xo,
52 final Color textColor) {
53 assert textLayout != null;
54 this.layout = textLayout;
55 this.color = textColor;
56 this.xoffset = xo;
57 this.yoffset = 0.0f;
58 this.trans = null;
59 }
60
61 /**
62 * Constructor for more complex texts (operators).
63 *
64 * @param textLayout
65 * Text Layout.
66 * @param textColor
67 * text color.
68 * @param xo
69 * X-Offset for drawing.
70 * @param yo
71 * Y-Offset for drawing.
72 * @param transform
73 * Transformation to apply before drawing.
74 */
75 public TextObject(final TextLayout textLayout, final float xo,
76 final float yo, final AffineTransform transform,
77 final Color textColor) {
78 assert textLayout != null;
79 this.layout = textLayout;
80 this.color = textColor;
81 this.xoffset = xo;
82 this.yoffset = yo;
83 this.trans = transform;
84 }
85
86 /** {@inheritDoc} */
87 public void paint(final float x, final float y, final Graphics2D g) {
88 g.setColor(this.color);
89 final AffineTransform oldTrans = g.getTransform();
90 g.translate(x + this.xoffset, y + this.yoffset);
91 if (this.trans != null) {
92 g.transform(this.trans);
93 }
94 this.layout.draw(g, 0, 0);
95 g.setTransform(oldTrans);
96 }
97 }