001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.bio.gui.glyph;
022
023import java.awt.Color;
024import java.awt.Graphics2D;
025import java.awt.Paint;
026import java.awt.Shape;
027import java.awt.geom.GeneralPath;
028import java.awt.geom.Rectangle2D;
029
030
031/**
032 * A Glyph that paints a two headed arrow within the bounds
033 * 
034 * @author Mark Southern
035 * @since 1.5
036 */
037public class TwoHeadedArrowGlyph implements Glyph {
038    private Paint fillPaint;
039    private Rectangle2D.Float bounds = new Rectangle2D.Float(0, 0, 0, 0);
040    private Shape arrowShape;
041
042    public TwoHeadedArrowGlyph() {
043        fillPaint = Color.BLUE;
044    }
045
046    public TwoHeadedArrowGlyph(Rectangle2D.Float bounds) {
047        this();
048        setBounds(bounds);
049    }
050
051    public Rectangle2D.Float getBounds() {
052        return bounds;
053    }
054
055    public void setBounds(Rectangle2D.Float r) {
056        if (bounds.equals(r)) {
057            return;
058        }
059
060        float q1 = r.height * 0.25F;
061        float q2 = r.height * 0.5F;
062        float q3 = r.height * 0.75F;
063        float arrowHeadSize = r.height;
064        GeneralPath p = new GeneralPath();
065
066        if ((r.width - (2F * arrowHeadSize)) > 0) {
067            p.moveTo(r.x, r.y + q2);
068            p.lineTo(r.x + arrowHeadSize, r.y);
069            p.lineTo(r.x + arrowHeadSize, r.y + q1);
070            p.lineTo((r.x + r.width) - arrowHeadSize, r.y + q1);
071            p.lineTo((r.x + r.width) - arrowHeadSize, r.y);
072            p.lineTo(r.x + r.width, r.y + q2);
073            p.lineTo((r.x + r.width) - arrowHeadSize, r.y + r.height);
074            p.lineTo((r.x + r.width) - arrowHeadSize, r.y + q3);
075            p.lineTo(r.x + arrowHeadSize, r.y + q3);
076            p.lineTo(r.x + arrowHeadSize, r.y + r.height);
077        } else {
078            p.moveTo(r.x, r.y + q1);
079            p.lineTo(r.x + r.width, r.y + q1);
080            p.lineTo(r.x + r.width, r.y + q3);
081            p.lineTo(r.x, r.y + q3);
082        }
083
084        p.closePath();
085        arrowShape = p;
086
087        bounds = r;
088    }
089
090    public void render(Graphics2D g) {
091        if (arrowShape != null) {
092            g.setPaint(fillPaint);
093            g.fill(arrowShape);
094        }
095    }
096}