KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > gview > object > GElementArrow


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.gview.object;
33
34 import org.antlr.xjlib.appkit.gview.base.Rect;
35 import org.antlr.xjlib.appkit.gview.base.Vector2D;
36 import org.antlr.xjlib.appkit.gview.shape.SArrow;
37 import org.antlr.xjlib.foundation.XJXMLSerializable;
38
39 import java.awt.*;
40
41 public class GElementArrow extends GElement implements XJXMLSerializable {
42
43     protected SArrow arrow;
44     protected Vector2D target;
45
46     public GElementArrow() {
47         setArrow(new SArrow());
48     }
49
50     public void setArrow(SArrow arrow) {
51         this.arrow = arrow;
52     }
53
54     public SArrow getArrow() {
55         return arrow;
56     }
57
58     public void setArrowLength(double length) {
59         arrow.setLength(length);
60     }
61
62     public void setSource(double x, double y) {
63         super.setPosition(x, y);
64     }
65
66     public void setTarget(double x, double y) {
67         arrow.setAnchor(x, y);
68
69         target = new Vector2D(x, y);
70         Vector2D direction = getPosition().sub(target);
71         arrow.setDirection(direction);
72     }
73
74     public void setTarget(Vector2D target) {
75         this.target = target;
76     }
77
78     public Vector2D getTarget() {
79         return target;
80     }
81
82     @Override JavaDoc
83     public void move(double dx, double dy) {
84         super.move(dx, dy);
85         target.shift(dx, dy);
86         arrow.getAnchor().shift(dx, dy);
87     }
88
89     @Override JavaDoc
90     public Rect getFrame() {
91         return new Rect(getPosition(), target, 2, 2);
92     }
93
94     @Override JavaDoc
95     public boolean isInside(Point p) {
96         return getFrame().contains(p);
97     }
98
99     @Override JavaDoc
100     public void draw(Graphics2D g) {
101         if(isVisibleInClip(g)) {
102             g.setColor(color);
103             drawShape(g);
104         }
105     }
106
107     @Override JavaDoc
108     public void drawShape(Graphics2D g) {
109         super.drawShape(g);
110         arrow.draw(g);
111         g.drawLine((int)getPositionX(), (int)getPositionY(), (int)target.x, (int)target.y);
112     }
113
114 }
115
Popular Tags