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: LineObject.java,v 88b901bf20fb 2008/06/07 14:12:27 maxberger $ */
18
19 package net.sourceforge.jeuclid.layout;
20
21 import java.awt.BasicStroke;
22 import java.awt.Color;
23 import java.awt.Graphics2D;
24 import java.awt.Stroke;
25 import java.awt.geom.Line2D;
26
27 /**
28 * @version $Revision: 88b901bf20fb $
29 */
30 public class LineObject implements GraphicsObject {
31
32 private final float x1;
33
34 private final float y1;
35
36 private final float x2;
37
38 private final float y2;
39
40 private final float width;
41
42 private final Color col;
43
44 private final boolean dash;
45
46 /**
47 * Default Constructor.
48 *
49 * @param color
50 * Color of the line.
51 * @param lineWidth
52 * StrokeWidth of the line.
53 * @param offsetY
54 * Y Offset from baseline.
55 * @param offsetX
56 * X Offset from left.
57 * @param offsetY2
58 * Y2 Offset from baseline.
59 * @param offsetX2
60 * X2 Offset from left.
61 */
62 public LineObject(final float offsetX, final float offsetY,
63 final float offsetX2, final float offsetY2, final float lineWidth,
64 final Color color) {
65 this.x1 = offsetX;
66 this.y1 = offsetY;
67 this.x2 = offsetX2;
68 this.y2 = offsetY2;
69 this.width = lineWidth;
70 this.col = color;
71 this.dash = false;
72 }
73
74 /**
75 * Default Constructor.
76 *
77 * @param color
78 * Color of the line.
79 * @param lineWidth
80 * StrokeWidth of the line.
81 * @param offsetY
82 * Y Offset from baseline.
83 * @param offsetX
84 * X Offset from left.
85 * @param offsetY2
86 * Y2 Offset from baseline.
87 * @param offsetX2
88 * X2 Offset from left.
89 * @param dashed
90 * if true line is dashed instead of solid.
91 */
92 public LineObject(final float offsetX, final float offsetY,
93 final float offsetX2, final float offsetY2, final float lineWidth,
94 final Color color, final boolean dashed) {
95 this.x1 = offsetX;
96 this.y1 = offsetY;
97 this.x2 = offsetX2;
98 this.y2 = offsetY2;
99 this.width = lineWidth;
100 this.col = color;
101 this.dash = dashed;
102 }
103
104 /** {@inheritDoc} */
105 public void paint(final float x, final float y, final Graphics2D g) {
106 g.setColor(this.col);
107 final Stroke oldStroke = g.getStroke();
108 if (this.dash) {
109 final float dashWidth = 3.0f * this.width;
110 g.setStroke(new BasicStroke(this.width, BasicStroke.CAP_SQUARE,
111 BasicStroke.JOIN_BEVEL, this.width, new float[] {
112 dashWidth, dashWidth, }, 0));
113 } else {
114 g.setStroke(new BasicStroke(this.width));
115 }
116 g.draw(new Line2D.Float(x + this.x1, y + this.y1, x + this.x2, y
117 + this.y2));
118 g.setStroke(oldStroke);
119
120 }
121 }