001    /*
002     * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: LineObject.java,v 88b901bf20fb 2008/06/07 14:12:27 maxberger $ */
018    
019    package net.sourceforge.jeuclid.layout;
020    
021    import java.awt.BasicStroke;
022    import java.awt.Color;
023    import java.awt.Graphics2D;
024    import java.awt.Stroke;
025    import java.awt.geom.Line2D;
026    
027    /**
028     * @version $Revision: 88b901bf20fb $
029     */
030    public class LineObject implements GraphicsObject {
031    
032        private final float x1;
033    
034        private final float y1;
035    
036        private final float x2;
037    
038        private final float y2;
039    
040        private final float width;
041    
042        private final Color col;
043    
044        private final boolean dash;
045    
046        /**
047         * Default Constructor.
048         * 
049         * @param color
050         *            Color of the line.
051         * @param lineWidth
052         *            StrokeWidth of the line.
053         * @param offsetY
054         *            Y Offset from baseline.
055         * @param offsetX
056         *            X Offset from left.
057         * @param offsetY2
058         *            Y2 Offset from baseline.
059         * @param offsetX2
060         *            X2 Offset from left.
061         */
062        public LineObject(final float offsetX, final float offsetY,
063                final float offsetX2, final float offsetY2, final float lineWidth,
064                final Color color) {
065            this.x1 = offsetX;
066            this.y1 = offsetY;
067            this.x2 = offsetX2;
068            this.y2 = offsetY2;
069            this.width = lineWidth;
070            this.col = color;
071            this.dash = false;
072        }
073    
074        /**
075         * Default Constructor.
076         * 
077         * @param color
078         *            Color of the line.
079         * @param lineWidth
080         *            StrokeWidth of the line.
081         * @param offsetY
082         *            Y Offset from baseline.
083         * @param offsetX
084         *            X Offset from left.
085         * @param offsetY2
086         *            Y2 Offset from baseline.
087         * @param offsetX2
088         *            X2 Offset from left.
089         * @param dashed
090         *            if true line is dashed instead of solid.
091         */
092        public LineObject(final float offsetX, final float offsetY,
093                final float offsetX2, final float offsetY2, final float lineWidth,
094                final Color color, final boolean dashed) {
095            this.x1 = offsetX;
096            this.y1 = offsetY;
097            this.x2 = offsetX2;
098            this.y2 = offsetY2;
099            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    }