Advertisement
Guest User

LeftMouseClick

a guest
Mar 21st, 2013
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.97 KB | None | 0 0
  1.     if (SwingUtilities.isLeftMouseButton(e)){
  2.        
  3.      
  4.         //reset nodes prior to painting
  5.         for (Node n : NodeIDsAndColors.keySet()){
  6.             n.AssignedAColor = false;
  7.             n.setColor(Color.BLUE);
  8.             NodeIDsAndColors.put(n,Color.BLUE);
  9.         }
  10.        
  11.        
  12.         //reset the graphical clusters that are drawn with left click.
  13.         resetGraphicalClusters();
  14.  
  15.         LinkedList<ClickableNode> ToBeClustered = new LinkedList<ClickableNode>();
  16.         List cna = getClickableNodes();
  17.        
  18.         boolean AtLeastOneNode = false;
  19.         float xPointClick = e.getX();
  20.        
  21.       // LinkedHashMap<Node,Color> ReplacementMap = new LinkedHashMap<Node,Color>();
  22.           if (cna.size() > 0)    {
  23.               AtLeastOneNode = true;
  24.           }
  25.         for (int i = 0; i < cna.size(); i++) {
  26.             ClickableNode cn = (ClickableNode) cna.get(i);
  27.              
  28.                //retrieve node
  29.            
  30.                Node n = cn.getTarget();
  31.                currentNodePointX = cn.getTargetArea().x;
  32.                currentNodePointY = cn.getTargetArea().y;
  33.                
  34.  
  35.                //figure out how to actually color the nodes
  36.                //if (currentNodePointX >= xPointClick){
  37.                if (currentNodePointX <= xPointClick){ //left
  38.                    ToBeClustered.add(cn);
  39.                    //NodeIDsAndColors.put(n,Color.RED);
  40.                } else {
  41.                    NodeIDsAndColors.put(n,Color.BLUE);
  42.                }
  43.  
  44.         }
  45.         //sort the list (bubble sorting)
  46.         for (int i = 0; i < ToBeClustered.size()-1; i++){
  47.             for (int j = 0; j < ToBeClustered.size()-1; j++){
  48.                 if (Math.abs(ToBeClustered.get(j).getTargetArea().x - xPointClick) >
  49.                         Math.abs(ToBeClustered.get(j+1).getTargetArea().x - xPointClick)){
  50.                    
  51.                     ClickableNode TempNode = ToBeClustered.get(j);
  52.                     ToBeClustered.set(j,ToBeClustered.get(j+1));
  53.                     ToBeClustered.set(j+1,TempNode);
  54.                 }
  55.             }
  56.         }
  57.        
  58.         //each linked list is an individual cluster.
  59.         //the 'clusters' object contains all clusters.
  60.         LinkedList<LinkedList<Node>> ClustersPreFiltering = new LinkedList<LinkedList<Node>>();
  61.        
  62.         int NumberOfClusters = 0;
  63.         for (int i = 0; i < ToBeClustered.size(); i++){
  64.                 boolean ClusterThisNode = true;
  65.                 for (int q = 0; q < ToBeClustered.get(i).getTarget().getNumberOfSons(); q++){
  66.                     if (ToBeClustered.get(i).getTarget().getSon(q).AssignedAColor){
  67.                         ClusterThisNode = false;
  68.                     }
  69.                 }
  70.                
  71.                 if (ClusterThisNode == true){
  72.                 //ToBeClustered.get(i).AssignedAColor = false;
  73.  
  74.                 LinkedList<Node> ThisCluster = new LinkedList<Node>();
  75.                 NumberOfClusters++;
  76.                
  77.                 /*Set the color of each node in the selected area to a color
  78.                  *depending on its spatial proximity to the clicked line.
  79.                  *The nodes are added to the list 'ThisCluster' and at the
  80.                  *end of the loops, the cluster is added to the meta list of
  81.                  * clusters. The intended result is to have the cut tree group
  82.                  * clusters at the loosely specified clustering threshold
  83.                  * (the clicked point) by highlighting the descended trees in
  84.                  * different colors. The cluster lists give the names of nodes.
  85.                  */
  86.                
  87.             //store a list of all nodes
  88.             LinkedList<Node> AllDescendants = new LinkedList<Node>();
  89.    
  90.                
  91.             //initialize: check for additional nodes
  92.             boolean ThereAreMoreSons = true;
  93.    
  94.                //start from the root node
  95.                 LinkedList<Node> SomeNodes = new LinkedList<Node>();
  96.                 SomeNodes.add(ToBeClustered.get(i).getTarget());
  97.                
  98.                 AllDescendants.add(ToBeClustered.get(i).getTarget());
  99.      
  100.             while (ThereAreMoreSons == true){
  101.        
  102.               //don't keep going, unless you have a reason to
  103.                ThereAreMoreSons = false;        
  104.        
  105.              //initialize a list for the next search
  106.              LinkedList<Node> AdditionalNodes = new LinkedList<Node>();
  107.        
  108.              //initialize through the old list, add hits to the new list
  109.            for (int j = 0; j < SomeNodes.size(); j++){
  110.                  if (SomeNodes.get(j).getNumberOfSons() != 0){
  111.                         ThereAreMoreSons = true;
  112.                         AdditionalNodes.addAll(SomeNodes.get(j).getSons());
  113.                         AllDescendants.addAll(SomeNodes.get(j).getSons());
  114.                      }
  115.                 }
  116.        
  117.                 //replace the old list with the new list
  118.              SomeNodes = AdditionalNodes;
  119.        
  120.             }
  121.                
  122.                // Node OneSon = ToBeClustered.get(i).getTarget().getSon(0)
  123.                 Color theColor;
  124.                 for (int a = 0; a < AllDescendants.size(); a++){
  125.        
  126.                     Node noodle = AllDescendants.get(a);
  127.                     //ClickableNode noodle = (ClickableNode) n.get(a);
  128.                    // Color theColor = clusterColor(ToBeClustered.size());
  129.                     if (noodle.AssignedAColor == false) {
  130.                         theColor = clusterColor(NumberOfClusters);
  131.                         noodle.setColor(theColor);
  132.                         if (noodle.hasFather()) {
  133.                         noodle.getFather().AssignedAColor = true;
  134.                         }
  135.                     } else {
  136.                         theColor = noodle.getColor();
  137.                     }
  138.                     NodeIDsAndColors.put(noodle,theColor);
  139.                     noodle.AssignedAColor = true;
  140.                     if (noodle.isLeaf()) {
  141.                         ThisCluster.add(noodle);
  142.                         noodle.AssignedAColor = true;
  143.                     }
  144.                 }
  145.                
  146.                 ClustersPreFiltering.add(ThisCluster);
  147.                 }
  148.            
  149.         }
  150.        
  151.         if (AtLeastOneNode){
  152.             isMouseClicked = true;
  153.            
  154.         } else {
  155.             isMouseClicked = false;
  156.         }
  157.        
  158.     //re-make clusters on click    
  159.     Clusters = new LinkedList<LinkedList<Node>>();
  160.    
  161.    
  162.     for (int k = 0; k < ClustersPreFiltering.size(); k++) {
  163.  
  164.                      
  165.     boolean KeepThisCluster = false;
  166.    
  167.         for (int l = 0; l < ClustersPreFiltering.get(k).size(); l++) {
  168.             if (ClustersPreFiltering.get(k).get(l).isLeaf()){
  169.                 KeepThisCluster = true;
  170.             }
  171.                
  172.         }
  173.        
  174.         //this cluster is retained.
  175.         if (KeepThisCluster == true){
  176.             Clusters.add(ClustersPreFiltering.get(k));
  177.         } else { //color blue
  178.                    
  179.         for (int l = 0; l < ClustersPreFiltering.get(k).size(); l++){
  180.             ClustersPreFiltering.get(k).get(l).AssignedAColor = false;
  181.             ClustersPreFiltering.get(k).get(l).setColor(Color.BLUE);
  182.             NodeIDsAndColors.put(ClustersPreFiltering.get(k).get(l),Color.BLUE);
  183.   //          System.out.println("Line 1068 - You made it to the coloring, bro.");
  184.         }
  185.    
  186.         }
  187.        
  188.     }
  189.    
  190.    //     System.out.println("Line 1253 - Number of Clusters: " + Clusters.size());  
  191.        
  192.         /*At this point, the program takes the Clusters and 'converts' them to
  193.          * the GraphicalCluster class, so that the plottable rectangles are
  194.          * available for plot.
  195.          */
  196.        
  197.         for (int i = 0; i < Clusters.size(); i++) {
  198.            
  199.             GraphicalCluster gc = new GraphicalCluster();
  200.            
  201.             LinkedList<String> clusterNames = new LinkedList<String>();
  202.            
  203.             for (int j = 0; j < Clusters.get(i).size(); j++) {
  204.                
  205.                 if (Clusters.get(i).get(j).hasName()) {
  206.                 clusterNames.add(Clusters.get(i).get(j).getName());
  207.                 }
  208.                
  209.             }
  210.            
  211.             gc.setAssociatedCluster(Clusters.get(i));
  212.            
  213.             gc.GraphicalCluster(
  214.                     Clusters.get(i),
  215.                     clusterNames,
  216.                     LeafName,
  217.                     LeafNameX,
  218.                     LeafNameY);
  219.            
  220.             Color color = Clusters.get(i).get(0).getColor();
  221.             int red = color.getRed();
  222.             int blue = color.getBlue();
  223.             int green = color.getGreen();
  224.             int alpha = 250;
  225.            
  226.             Color newColor = new Color(red, green, blue, alpha);
  227.  
  228.             gc.setColor(newColor);
  229.            
  230.             lemon.add(gc);
  231.         }
  232.        
  233.        
  234.        
  235.        
  236.         repaint();
  237.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement