1 | /**
|
---|
2 | * @(#)ZRect.java 12/12/99
|
---|
3 | * @author Peter Au
|
---|
4 | * All rights reserved.
|
---|
5 | */
|
---|
6 |
|
---|
7 | package vishnu.testvis.treemap.rectangle;
|
---|
8 |
|
---|
9 | import java.awt.*;
|
---|
10 |
|
---|
11 | public class ZRect extends Rect{
|
---|
12 | //-- no of doc that the rectangle represented
|
---|
13 | private int _capacity=0;
|
---|
14 |
|
---|
15 | //-- the new points that the rectangle will be translated (java coordination)
|
---|
16 | protected float _t_left_x=0f, _t_left_y=0f, _t_right_x=0f, _t_right_y=0f;
|
---|
17 | //-- if the rectangle has been choosen, set it to true
|
---|
18 | private boolean _chosen=false;
|
---|
19 |
|
---|
20 | public ZRect(float x1, float y1, float x2, float y2, Color c){
|
---|
21 | super(x1, y1, x2, y2, c);
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 | public ZRect(float x1, float y1, float x2, float y2, Color c, int s){
|
---|
26 | super(x1, y1, x2, y2, c);
|
---|
27 | _capacity = s;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public int getCapacity(){return _capacity;}
|
---|
31 | public void setCapacity(int i){_capacity=i;}
|
---|
32 |
|
---|
33 | public boolean isChosen(){return _chosen;}
|
---|
34 | public void setChosen(){_chosen=true;}
|
---|
35 | public void resetChosen(){_chosen=false;}
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * set the new coordinate, that the rectangle will move to
|
---|
39 | * pre: the coordinate is based on Java graphic coordination
|
---|
40 | * x1,y1 is the left upper corner and
|
---|
41 | * x2,y2 is the right lower corner
|
---|
42 | */
|
---|
43 | public void translateTo(float x1, float y1, float x2, float y2){
|
---|
44 | _t_left_x=x1; _t_left_y=y1;
|
---|
45 | _t_right_x=x2; _t_right_y=y2;
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | private static boolean almostEqual(float a, float b){
|
---|
51 | float tolerant = 0.00001F;
|
---|
52 | return (Math.abs(a-b) <= tolerant);
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * @param x is the input value
|
---|
58 | * @param x1 is the orginal x-coordinate of the rectangle
|
---|
59 | * @param y1 is the orginal y-coordinate of the rectangle
|
---|
60 | * @param x2 is the translated x-coordinate
|
---|
61 | * @param y2 is the translated y-coordinate
|
---|
62 | * @return y, based on x
|
---|
63 | */
|
---|
64 | private static float newY(
|
---|
65 | float x,
|
---|
66 | float x1, float y1, float x2, float y2,
|
---|
67 | int totalFrame, int currentFrame){
|
---|
68 | // return (x1==x2)? y2: ((x*(y1-y2) + x1*y2 - x2*y1) / (x1-x2));
|
---|
69 | if (almostEqual(x1,x2))
|
---|
70 | return ((y2-y1)/(float) totalFrame) * (float) (currentFrame+1) + y1;
|
---|
71 | else
|
---|
72 | return ((x*(y1-y2) + x1*y2 - x2*y1) / (x1-x2));
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * @param y is the input value
|
---|
77 | * @param x1 is the orginal x-coordinate of the rectangle
|
---|
78 | * @param y1 is the orginal y-coordinate of the rectangle
|
---|
79 | * @param x2 is the translated x-coordinate
|
---|
80 | * @param y2 is the translated y-coordinate
|
---|
81 | * @return x, based on y
|
---|
82 | */
|
---|
83 | private static float newX(
|
---|
84 | float y,
|
---|
85 | float x1, float y1, float x2, float y2,
|
---|
86 | int totalFrame, int currentFrame){
|
---|
87 | // return (y1==y2)? x2: ((y*(x1-x2) - x1*y2 + x2*y1)/(y1-y2));
|
---|
88 | if (almostEqual(y1,y2))
|
---|
89 | return ((x2-x1)/(float) totalFrame) * (float) (currentFrame+1) + x1;
|
---|
90 | else
|
---|
91 | return ((y*(x1-x2) - x1*y2 + x2*y1)/(y1-y2));
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * pre: translateTo is called
|
---|
97 | * @param how many animated pictures u want to create
|
---|
98 | * @retrun a list of animation picture (rectangles with diff positions)
|
---|
99 | */
|
---|
100 | public ZRect[] zooming(int noOfFrame){
|
---|
101 | ZRect[] vec = new ZRect[noOfFrame];
|
---|
102 | float dist1, dist2, t1, t2;
|
---|
103 |
|
---|
104 | switch (getDirection()){
|
---|
105 | case __NORTH: case __SOUTH:
|
---|
106 | dist1 = (_t_left_y - getUpperLeftY())/(float)noOfFrame;
|
---|
107 | dist2 = (_t_right_y - getLowerRightY())/(float)noOfFrame;
|
---|
108 | t1 = getUpperLeftY();
|
---|
109 | t2 = getLowerRightY();
|
---|
110 | //-- create the animated pictures, except the last image
|
---|
111 | for(int i=0;i<noOfFrame-1;i++){
|
---|
112 | t1 += dist1;
|
---|
113 | t2 += dist2;
|
---|
114 | vec[i] = new ZRect(
|
---|
115 | newX(t1,getUpperLeftX(),getUpperLeftY(),_t_left_x,_t_left_y,noOfFrame,i),
|
---|
116 | t1,
|
---|
117 | newX(t2,getLowerRightX(),getLowerRightY(),_t_right_x,_t_right_y,noOfFrame,i),
|
---|
118 | t2,
|
---|
119 | getColor());
|
---|
120 | }
|
---|
121 | vec[vec.length-1] =
|
---|
122 | new ZRect(_t_left_x, _t_left_y, _t_right_x, _t_right_y, getColor());
|
---|
123 |
|
---|
124 | break;
|
---|
125 | default:
|
---|
126 | //-- for WEST and EAST
|
---|
127 | dist1 = (_t_left_x - getUpperLeftX())/(float)noOfFrame;
|
---|
128 | dist2 = (_t_right_x - getLowerRightX())/(float)noOfFrame;
|
---|
129 | t1 = getUpperLeftX();
|
---|
130 | t2 = getLowerRightX();
|
---|
131 |
|
---|
132 | //-- create the animated pictures, except the last image
|
---|
133 | for(int i=0;i<noOfFrame-1;i++){
|
---|
134 | t1 += dist1;
|
---|
135 | t2 += dist2;
|
---|
136 | vec[i] = new ZRect(
|
---|
137 | t1,
|
---|
138 | newY(t1,getUpperLeftX(),getUpperLeftY(),_t_left_x,_t_left_y,noOfFrame,i),
|
---|
139 | t2,
|
---|
140 | newY(t2,getLowerRightX(),getLowerRightY(),_t_right_x,_t_right_y,noOfFrame,i),
|
---|
141 | getColor());
|
---|
142 | }
|
---|
143 | /**
|
---|
144 | * create the final picture here, the reason we don't do in the above
|
---|
145 | * for loop is because we avoid the 'tolerant' error from the floating
|
---|
146 | * point computation.
|
---|
147 | */
|
---|
148 | vec[vec.length-1] =
|
---|
149 | new ZRect(_t_left_x, _t_left_y, _t_right_x, _t_right_y, getColor());
|
---|
150 |
|
---|
151 |
|
---|
152 | } //-- switch
|
---|
153 |
|
---|
154 | return vec;
|
---|
155 | }
|
---|
156 |
|
---|
157 | }
|
---|