annotate java/src/uk/ac/qmul/eecs/ccmi/simpletemplate/ArrowHead.java @ 8:ea7885bd9bff tip

fixed bug : render solid line as dotted/dashed when moving the stylus from dotted/dashed to solid
author ccmi-guest
date Thu, 03 Jul 2014 16:12:20 +0100
parents 9418ab7b7f3f
children
rev   line source
fiore@0 1 /*
fiore@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
fiore@0 3
fiore@0 4 Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
fiore@0 5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
fiore@0 6
fiore@0 7 This program is free software: you can redistribute it and/or modify
fiore@0 8 it under the terms of the GNU General Public License as published by
fiore@0 9 the Free Software Foundation, either version 3 of the License, or
fiore@0 10 (at your option) any later version.
fiore@0 11
fiore@0 12 This program is distributed in the hope that it will be useful,
fiore@0 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
fiore@0 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fiore@0 15 GNU General Public License for more details.
fiore@0 16
fiore@0 17 You should have received a copy of the GNU General Public License
fiore@0 18 along with this program. If not, see <http://www.gnu.org/licenses/>.
fiore@0 19 */
fiore@0 20
fiore@0 21 package uk.ac.qmul.eecs.ccmi.simpletemplate;
fiore@0 22
fiore@0 23 import java.awt.BasicStroke;
fiore@0 24 import java.awt.Color;
fiore@0 25 import java.awt.Graphics2D;
fiore@0 26 import java.awt.Stroke;
fiore@0 27 import java.awt.geom.GeneralPath;
fiore@0 28 import java.awt.geom.Point2D;
fiore@0 29 import java.io.IOException;
fiore@0 30
fiore@0 31 /**
fiore@0 32 This class defines arrowheads of diverse shapes.
fiore@0 33 */
fiore@0 34 public enum ArrowHead {
fiore@0 35 TAIL("Tail"),
fiore@0 36 TRIANGLE("Triangle"),
fiore@0 37 BLACK_TRIANGLE("Black Triangle"),
fiore@0 38 V("V"),
fiore@0 39 HALF_V("Half V"),
fiore@0 40 DIAMOND("Diamond"),
fiore@0 41 BLACK_DIAMOND("Black Diamond");
fiore@0 42
fiore@0 43 private ArrowHead(String name) {
fiore@0 44 this.lowerCaseName = name;
fiore@0 45 }
fiore@0 46
fiore@0 47 public static ArrowHead getArrowHeadFromString(String arrowHeadName) throws IOException{
fiore@0 48 ArrowHead h;
fiore@0 49 String name = arrowHeadName.toUpperCase().replace(" ", "_");
fiore@0 50 try {
fiore@0 51 h = ArrowHead.valueOf(name);
fiore@0 52 }catch (IllegalArgumentException e){
fiore@0 53 throw new IOException(e);
fiore@0 54 }
fiore@0 55 return h;
fiore@0 56 }
fiore@0 57
fiore@0 58 @Override
fiore@0 59 public String toString(){
fiore@0 60 return lowerCaseName;
fiore@0 61 }
fiore@0 62
fiore@0 63 /**
fiore@0 64 Draws the arrowhead.
fiore@0 65 @param g2 the graphics context
fiore@0 66 @param p a point on the axis of the arrow head
fiore@0 67 @param q the end point of the arrow head
fiore@0 68 */
fiore@0 69 public void draw(Graphics2D g2, Point2D p, Point2D q){
fiore@0 70 GeneralPath path = getPath(p, q);
fiore@0 71 Color oldColor = g2.getColor();
fiore@0 72 if (this == BLACK_DIAMOND || this == BLACK_TRIANGLE)
fiore@0 73 g2.setColor(Color.BLACK);
fiore@0 74 else
fiore@0 75 g2.setColor(Color.WHITE);
fiore@0 76 g2.fill(path);
fiore@0 77 g2.setColor(oldColor);
fiore@0 78 Stroke oldStroke = g2.getStroke();
fiore@0 79 g2.setStroke(new BasicStroke());
fiore@0 80 g2.draw(path);
fiore@0 81 g2.setStroke(oldStroke);
fiore@0 82 }
fiore@0 83
fiore@0 84 /**
fiore@0 85 Gets the path of the arrowhead
fiore@0 86 @param p a point on the axis of the arrow head
fiore@0 87 @param q the end point of the arrow head
fiore@0 88 @return the path
fiore@0 89 */
fiore@0 90 public GeneralPath getPath(Point2D p, Point2D q){
fiore@0 91 GeneralPath path = new GeneralPath();
fiore@0 92 final double ARROW_ANGLE = Math.PI / 6;
fiore@0 93 final double ARROW_LENGTH = 10;
fiore@0 94
fiore@0 95 double dx = q.getX() - p.getX();
fiore@0 96 double dy = q.getY() - p.getY();
fiore@0 97 double angle = Math.atan2(dy, dx);
fiore@0 98 double x1 = q.getX()
fiore@0 99 - ARROW_LENGTH * Math.cos(angle + ARROW_ANGLE);
fiore@0 100 double y1 = q.getY()
fiore@0 101 - ARROW_LENGTH * Math.sin(angle + ARROW_ANGLE);
fiore@0 102 double x2 = q.getX()
fiore@0 103 - ARROW_LENGTH * Math.cos(angle - ARROW_ANGLE);
fiore@0 104 double y2 = q.getY()
fiore@0 105 - ARROW_LENGTH * Math.sin(angle - ARROW_ANGLE);
fiore@0 106
fiore@0 107 path.moveTo((float)q.getX(), (float)q.getY());
fiore@0 108 path.lineTo((float)x1, (float)y1);
fiore@0 109 if (this == V)
fiore@0 110 {
fiore@0 111 path.moveTo((float)x2, (float)y2);
fiore@0 112 path.lineTo((float)q.getX(), (float)q.getY());
fiore@0 113 }
fiore@0 114 else if (this == TRIANGLE || this == BLACK_TRIANGLE)
fiore@0 115 {
fiore@0 116 path.lineTo((float)x2, (float)y2);
fiore@0 117 path.closePath();
fiore@0 118 }
fiore@0 119 else if (this == DIAMOND || this == BLACK_DIAMOND)
fiore@0 120 {
fiore@0 121 double x3 = x2 - ARROW_LENGTH * Math.cos(angle + ARROW_ANGLE);
fiore@0 122 double y3 = y2 - ARROW_LENGTH * Math.sin(angle + ARROW_ANGLE);
fiore@0 123 path.lineTo((float)x3, (float)y3);
fiore@0 124 path.lineTo((float)x2, (float)y2);
fiore@0 125 path.closePath();
fiore@0 126 }
fiore@0 127 return path;
fiore@0 128 }
fiore@0 129
fiore@0 130 private String lowerCaseName;
fiore@0 131 }