There are very few line styles provided to us in Java2D. Apart from straight lines and dotted lines, there seems to be no other style. If you are careful with your shoes, you will find that, TWaver provides a special line with twists and turns.
There are also a variety of implementation methods for this kind of twists and turns line. Remember the LinkUI customized in the previous articles, it is also a variety of methods, such:
Colorful links
Link
Today, we will introduce the arrow mobile Link. Let's take a look at it:
This is a line from the from node to the node. The line is formed by an arrow. This line looks much more beautiful than the traditional flow, many customers have mentioned this style. I will explain the implementation details in detail in this article.
First, you need to customize an ArrowLink to inherit from the Link. In ArrowLink, You need to define several variables for it, for example: line width, color, length of each arrow segment, number of flow arrows to be filled, transparency, whether it is from flow to, and offset (used to show flow), etc. If you don't talk so much about it, check the Code directly:
1 public class ArrowLink extends Link {
2
3 public ArrowLink (){
4 super ();
5 init ();
6}
7
8 public ArrowLink (Object id ){
9 super (id );
10 init ();
11}
12
13 public ArrowLink (Node from, Node ){
14 super (from, );
15 init ();
16}
17
18 public ArrowLink (Object id, Node from, Node ){
19 super (id, from, );
20 init ();
21}
22
23 private void init (){
24 this. putLinkColor (new Color (0, 0, 0, 0 ));
25 this. putLinkOutlineWidth (0 );
26 this. setLinkType (TWaverConst. LINK_TYPE_PARALLEL );
27 this. putLinkAntialias (true );
28 this. putClientProperty ("lineWidth", 3.0f );
29 this. putClientProperty ("lineColor", Color. blue );
30 this. putClientProperty ("offset", 0.0 );
31 this. putClientProperty ("segmentLength", 8.0 );
32 this. putClientProperty ("fillSegmentCount", 5 );
33 this. putClientProperty ("defaultAlpha", 0.2 );
34 this. putClientProperty ("from", true );
35}
36
37 public String getUIClassID (){
38 return ArrowLinkUI. class. getName ();
39}
40
41}
After the link is customized, the most important thing is to re-draw the LinkUI. The custom ArrowLinkUI class inherits from the LinkUI and reloads the paintBody method. We need to draw an arrow in the paintBody, arrows are actually relatively simple to implement. We can get the Link length and know the length of each segment on the Link, and then calculate the number of arrows to be drawn.
1 int count = (int) (length/segmentLength );
Based on the number of arrows, you can obtain the position of each point of the arrow to be drawn:
1 List points = TWaverUtil. divideShape (this. path, count );
After obtaining this position, we can calculate the positions of the other two points of the arrow with this point as the center point:
1 Point2D p0 = new Point. Double ();
2 transform. transform (point, p0 );
3 Point2D p1 = new Point. Double ();
4 transform. transform (new Point. Double (point. getX () + segmentLength/2 * sign, point. getY ()-segmentLength/2), p1 );
5 Point2D p2 = new Point. Double ();
6 transform. transform (new Point. Double (point. getX () + segmentLength/2 * sign, point. getY () + segmentLength/2), p2 );
So an arrow can be drawn.
Other arrows can be drawn cyclically in the same way. Note that the arrows rotate with the node position, so we need to calculate the angle and position of the arrow rotation point:
1 AffineTransform transform = new AffineTransform ();
2 transform. translate (point. getX (), point. getY ());
3 transform. rotate (angle );
4 transform. translate (-point. getX (),-point. getY ());
Finally, there is a flow effect. Here we set an offset parameter to indicate the flow offset. The transparency of the current arrow is determined based on the offset and the number of filled flow arrows:
1 double alpha = (Double) this. element. getClientProperty ("defaultAlpha ");
2 if (offset * count> = I & offset * count-fillSegmentCount <= I ){
3 alpha = 1-(offset * count-I)/fillSegmentCount * 0.5;
4}
The complete code for drawing arrows is as follows:
1 public class ArrowLinkUI extends LinkUI {
2
3 public ArrowLinkUI (TNetwork, Link link ){
4 super (network, link );
5}
6
7 public void paintBody (Graphics2D g2d ){
8 super. paintBody (g2d );
9 this. drawFlowing (g2d );
10}
11
12 private void drawFlowing (Graphics2D g2d ){
13 double length = TWaverUtil. getLength (this. path );
14 if (length <= 0 ){
15 return;
16}
17
18 double segmentLength = (Double) this. element. getClientProperty ("segmentLength ");
19 int count = (int) (length/segmentLength );
20 List points = TWaverUtil. divideShape (this. path, count );
21 if (points. size () <2 ){
22 return;
23}
24 int fillSegmentCount = (Integer) this. element. getClientProperty ("fillSegmentCount ");
25 double offset = (Double) this. element. getClientProperty ("offset ");
26 Color lineColor = (Color) this. element. getClientProperty ("lineColor ");
27 boolean from = (Boolean) this. element. getClientProperty ("from ");
28 boolean fromLeft = this. getFromPoint (). x <= this. getToPoint (). x;
29
30 g2d. setStroke (new BasicStroke (Float) this. element. getClientProperty ("lineWidth ")));
31 for (int I = 0; I
32 Point2D point = (Point2D) points. get (I );
33 Point2D point1, point2;
34 double angle = 0;
35 if (I = points. size ()-1 ){
36 point1 = (Point2D) points. get (I-1 );
37 point2 = point;
38} else {
39 point1 = point;
40 point2 = (Point2D) points. get (I + 1 );
41}
42 angle = getAngle (point1, point2 );
43 int sign = (fromLeft & from |! FromLeft &&! From )? -1: 1;
44 if (angle =-Math. PI/2 ){
45 sign = point2.getY ()> point1.getY ()? 1:-1;
46} else if (angle = Math. PI/2 ){
47 sign = point2.getY ()> point1.getY ()? -1: 1;
48}
49 double alpha = (Double) this. element. getClientProperty ("defaultAlpha ");
50 if (offset * count> = I & offset * count-fillSegmentCount <= I ){
51 alpha = 1-(offset * count-I)/fillSegmentCount * 0.5;
52}
53 g2d. setColor (new Color (lineColor. getRed (), lineColor. getGreen (), lineColor. getBlue (), (int) (255 * alpha )));
54
55 AffineTransform transform = new AffineTransform ();
56 transform. translate (point. getX (), point. getY ());
57 transform. rotate (angle );
58 transform. translate (-point. getX (),-point. getY ());
59
60 Point2D p0 = new Point. Double ();
61 transform. transform (point, p0 );
62 Point2D p1 = new Point. Double ();
63 transform. transform (new Point. Double (point. getX () + segmentLength/2 * sign, point. getY ()-segmentLength/2), p1 );
64 Point2D p2 = new Point. Double ();
65 transform. transform (new Point. Double (point. getX () + segmentLength/2 * sign, point. getY () + segmentLength/2), p2 );
66 GeneralPath path = new GeneralPath ();
67 path. moveTo (p1.getX (), p1.getY ());
68 path. lineTo (Response getx (), response Gety ());
69 path. lineTo (p2.getX (), p2.getY ());
70 g2d. draw (path );
71}
72}
73
74 private static double getAngle (Point2D p1, Point2D p2 ){
75 if (p1.getX () = p2.getX ()){
76 if (p2.getY () = p1.getY ()){
77 return 0;
78}
79 else if (p2.getY ()> p1.getY ()){
80 return Math. PI/2;
81}
82 else {
83 return-Math. PI/2;
84}
85}
86 return Math. atan (p2.getY ()-p1.getY ()/(p2.getX ()-p1.getX ()));
87}
88}
With this kind of mobile arrow, we can draw more rich and colorful interfaces, and finally give a complete example for everyone to learn: