1 import javax.swing.JPanel;
3 import java.awt.BasicStroke;
5 import java.awt.Graphics;
6 import java.awt.Graphics2D;
7 import java.awt.event.MouseEvent;
8 import java.awt.event.MouseListener;
9 import java.awt.event.MouseMotionListener;
10 import java.awt.geom.Line2D;
11 import java.awt.image.BufferedImage;
12 import java.util.ArrayList;
22 private boolean bundlingActive;
23 private int mousePosX, mousePosY;
24 private int radius = 5;
25 private ArrayList<Edge> edges;
26 private ArrayList<Node> nodes;
28 private Node selectedNode;
35 setBackground(Color.WHITE);
37 edges =
new ArrayList<Edge>();
38 nodes =
new ArrayList<Node>();
39 bundlingActive =
false;
47 addMouseListener(
new MouseListener() {
50 public void mouseReleased(MouseEvent e) {
53 if(selectedNode != null){
54 boolean occupied =
false;
55 for(
Node node : nodes){
56 if(
clickedNode(e, node) && !node.equals(selectedNode)){
62 selectedNode.
setCoords(mousePosX, mousePosY);
66 nodes.add(
new Node(e.getX(), e.getY()));
70 for(
Node node : nodes){
72 if(!node.equals(selectedNode)){
73 edges.add(
new Edge(selectedNode, node));
82 for(
Node node : nodes){
85 ArrayList<Edge> edgesToDelete =
new ArrayList<Edge>();
86 for(
Edge edge : edges){
87 if(edge.isIncident(node))
88 edgesToDelete.add(edge);
90 edges.removeAll(edgesToDelete);
94 for(
Edge edge : edges){
101 paintComponent(getGraphics());
105 public void mousePressed(MouseEvent e) {
108 for(
Node node : nodes){
117 for(
Node node : nodes){
127 public void mouseExited(MouseEvent e) {
131 public void mouseEntered(MouseEvent e) {
135 public void mouseClicked(MouseEvent e) {
139 addMouseMotionListener(
new MouseMotionListener() {
142 public void mouseMoved(MouseEvent e) {
147 public void mouseDragged(MouseEvent e) {
148 if(selectedNode != null){
149 mousePosX = e.getX();
150 mousePosY = e.getY();
151 paintComponent(getGraphics());
158 public void paintComponent(Graphics g){
159 super.paintComponent(g);
160 BufferedImage bimg =
new BufferedImage(getSize().width, getSize().width,
161 BufferedImage.TYPE_INT_ARGB);
162 Graphics2D g2d = bimg.createGraphics();
165 g2d.setColor(
new Color(0, 0, 0, 0.10f));
166 g2d.setStroke(
new BasicStroke(3));
167 for(
Edge edge : edges){
169 g2d.drawPolyline(edge.subNodesXInt(), edge.subNodesYInt(), edge.getSubNodes().size());
172 g2d.drawLine(edge.getStartNode().getXInt(), edge.getStartNode().getYInt(),
173 edge.getEndNode().getXInt(), edge.getEndNode().getYInt());
177 int[] pixels = bimg.getRGB(0, 0, getSize().width, getSize().height, null, 0, getSize().width);
178 for(
int p=0; p < getSize().width*getSize().height; p++){
179 int alpha = pixels[p]>>24 & 255;
181 pixels[p] = alpha<<24 | alpha<<16 | (255-alpha);
183 bimg.setRGB(0, 0, getSize().width, getSize().height, pixels, 0, getSize().width);
186 g2d.setColor(Color.BLACK);
187 for(
Node node : nodes){
188 if(node.equals(selectedNode)){
190 g2d.fillOval(mousePosX-radius, mousePosY-radius, radius*2, radius*2);
192 g2d.fillOval(node.getXInt()-radius, node.getYInt()-radius, radius*2, radius*2);
193 g2d.drawLine(node.getXInt(), node.getYInt(), mousePosX, mousePosY);
197 g2d.fillOval(node.getXInt()-radius, node.getYInt()-radius, radius*2, radius*2);
199 this.getGraphics().drawImage(bimg, 0, 0, null);
207 public void addEdge(
int startNodeIndex,
int endNodeIndex){
208 edges.add(
new Edge(nodes.get(startNodeIndex), nodes.get(endNodeIndex)));
217 nodes.add(
new Node(x, y));
244 double stepSize = 0.4;
246 for(
int c=0; c < cycles; c++){
248 e.increaseSubNodes();
249 for(
int i=0; i < iterations; i++){
251 double kp = stiffness/e.magnitude()*(e.getSubNodes().size()-1);
252 ArrayList<Node> tempSubNodes =
new ArrayList<Node>();
253 tempSubNodes.add(e.getStartNode());
254 for(
int n=1; n < e.getSubNodes().size()-1; n++){
257 Node s = e.getSubNodes().get(n);
258 Node pre = e.getSubNodes().get(n-1);
259 Node suc = e.getSubNodes().get(n+1);
264 double comp = e.compatibility(f);
266 Node t = f.getSubNodes().get(n);
267 double nDist = Double.POSITIVE_INFINITY;
268 for(
Node u : f.getSubNodes()){
282 double springForceX = kp * ((pre.
getX()-s.
getX()))
284 double springForceY = kp * ((pre.
getY()-s.
getY()))
286 tempSubNodes.add(
new Node(s.
getX()+esForceX*stepSize+springForceX*stepSize,
287 s.
getY()+esForceY*stepSize+springForceY*stepSize));
289 tempSubNodes.add(e.getEndNode());
290 e.setTempSubNodes(tempSubNodes);
293 e.finalizeSubNodes();
294 bundlingActive =
true;
295 progress += (100.0/cycles)/iterations;
298 paintComponent(getGraphics());
301 iterations = (int)Math.round(iterations*2/3);
302 stepSize = stepSize/2;
304 bundlingActive =
true;
306 paintComponent(getGraphics());
315 paintComponent(getGraphics());
323 for(
int i=0; i<nodes.size(); i++)
324 for(
int j=i+1; j<nodes.size(); j++)
325 edges.add(
new Edge(nodes.get(i), nodes.get(j)));
326 paintComponent(getGraphics());
333 bundlingActive =
false;
334 for(
Edge edge: edges)
335 edge.deleteSubNodes();
336 paintComponent(getGraphics());
357 return (e.getX() >= node.
getX()-radius && e.getX() <= node.
getX()+radius &&
358 e.getY() >= node.
getY()-radius && e.getY() <= node.
getY()+radius);