-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMindMapPanel.java
244 lines (210 loc) · 6.75 KB
/
MindMapPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package mindmapApplication;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JPanel;
public class MindMapPanel extends JPanel
{
ArrayList<JNode> nodes;
public MindMapPanel(int width, int height)
{
super();
setLayout(null);
setMinimumSize(new Dimension(width, height));
nodes = new ArrayList<>();
addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
focusRemove();
}
});
}
public void makeNodes(String query)
{
ArrayList<JNode> tmpNodes = new ArrayList<>();
ArrayList<Integer> indent = new ArrayList<>();
ArrayList<Integer> childNum = new ArrayList<>();
ArrayList<Integer> count = new ArrayList<>();
ArrayList<Integer> parentNum = new ArrayList<>();
ArrayList<Double> angle = new ArrayList<>();
ArrayList<Integer> depth = new ArrayList<>();
ArrayList<Color> color = new ArrayList<>();
nodes.clear();
removeAll();
updateUI();
int nodeNum = 0;
int code = 0;
String[] names = query.split("\n");
for(String name : names)
{
if(name.trim().equals(""))
{
tmpNodes.add(null);
indent.add(999);
childNum.add(999);
count.add(999);
parentNum.add(999);
angle.add(999D);
depth.add(999);
color.add(null);
continue;
}
JNode parent = null;
int cnt = 0;
while(cnt<name.length() && name.charAt(cnt)=='\t')
++cnt;
boolean found = false;
for(int i=indent.size()-1; i>=0; --i)
if(indent.get(i) < cnt)
{
found = true;
parent = tmpNodes.get(i);
childNum.set(i, childNum.get(i)+1);
parentNum.add(i);
depth.add(depth.get(i)+1);
break;
}
if(!found)
{
parentNum.add(999);
depth.add(0);
}
childNum.add(0);
count.add(0);
angle.add(Math.random()*Math.PI*2);
color.add(new Color(getRandom(150,255), getRandom(150,255), getRandom(150,255)));
name = name.trim();
int width = 70+name.length()*10;
int height = 40;
tmpNodes.add(new JNode(code++, this, name, 0, 0, width, height, Color.BLACK, parent));
indent.add(cnt);
++nodeNum;
}
for(int i=0; i<tmpNodes.size(); ++i)
{
JNode node = tmpNodes.get(i);
if(node != null)
{
int x, y;
int dep = depth.get(i);
if(dep == 0)
{
int width = this.getSize().width;
int height = this.getSize().height;
x = getRandom(width*4/10, width*6/10) - node.getWidth()/2;
y = getRandom(height*4/10, height*6/10) - node.getHeight()/2;
node.setLocation(x, y);
node.setBackground(new Color(getRandom(150,255), getRandom(150,255), getRandom(150,255)));
}
else if(dep == 1)
{
int length = (int)(120*Math.log10(nodeNum*50+1)/(depth.get(i)+1));
int parentX = node.getParentNode().getX()+node.getParentNode().getWidth()/2;
int parentY = node.getParentNode().getY()+node.getParentNode().getHeight()/2;
int idx = parentNum.get(i);
int cnt = count.get(idx);
int num = childNum.get(idx);
double initialAngle = angle.get(idx);
x = parentX - node.getWidth()/2 + (int)(length*Math.cos(initialAngle+Math.PI*2*cnt/num));
y = parentY - node.getHeight()/2 + (int)(length*Math.sin(initialAngle+Math.PI*2*cnt/num));
node.setLocation(x, y);
node.setBackground(color.get(idx));
count.set(idx, cnt+1);
}
else
{
int length = (int)(120*Math.log10(nodeNum*50+1)/(depth.get(i)+1));
int parentX = node.getParentNode().getX()+node.getParentNode().getWidth()/2;
int parentY = node.getParentNode().getY()+node.getParentNode().getHeight()/2;
JNode granpa = node.getParentNode().getParentNode();
int granpaX = granpa.getX()+granpa.getWidth()/2;
int granpaY = granpa.getY()+granpa.getHeight()/2;
int idx = parentNum.get(i);
int cnt = count.get(idx);
int num = childNum.get(idx);
double initialAngle = Math.atan2(granpaY-parentY, granpaX-parentX);
x = parentX - node.getWidth()/2 + (int)(length*Math.cos(initialAngle+Math.PI*2*(cnt+1)/(num+1)));
y = parentY - node.getHeight()/2 + (int)(length*Math.sin(initialAngle+Math.PI*2*(cnt+1)/(num+1)));
node.setLocation(x, y);
node.setBackground(color.get(idx));
count.set(idx, cnt+1);
}
}
}
for(JNode node : tmpNodes)
if(node != null)
{
for(int i=1; i<9; ++i)
add(node.getSelection().get(i));
add(node);
add(node.getSelection().get(0));
nodes.add(node);
}
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(new Color(150, 150, 150));
g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
for(JNode node : nodes)
if(node.getParentNode() != null)
node.draw(g2);
}
public ArrayList<ArrayList<Object>> getData()
{
ArrayList<ArrayList<Object>> data = new ArrayList<>();
for(JNode node : nodes)
data.add(node.getData());
return data;
}
public void readData(ArrayList<ArrayList<Object>> data)
{
nodes.clear();
removeAll();
updateUI();
for(ArrayList<Object> datus : data)
{
int code = ((Long)datus.get(0)).intValue();
int parentCode = ((Long)datus.get(1)).intValue();
String label = (String)datus.get(2);
int x = ((Long)datus.get(3)).intValue();
int y = ((Long)datus.get(4)).intValue();
int width = ((Long)datus.get(5)).intValue();
int height = ((Long)datus.get(6)).intValue();
int r = ((Long)datus.get(7)).intValue();
int g = ((Long)datus.get(8)).intValue();
int b = ((Long)datus.get(9)).intValue();
Color color = new Color(r, g, b);
JNode parent = parentCode==-1? null : nodes.get(parentCode);
nodes.add(new JNode(code, this, label, x, y, width, height, color, parent));
}
for(JNode node : nodes)
add(node);
}
public void focusRemove()
{
AttributePanel.setFocus(null);
for(JNode node : nodes)
node.setFocus(false);
}
private int getRandom(int a, int b)
{
return (int)(Math.random()*(b-a+1)+a);
}
}