Problem with selecting and highlighting node

2008-10-12
2013-05-29
  • Kalpesh Gada

    Kalpesh Gada - 2008-10-12

    Hello,
               I have tried the Shortest Path Demo and I am trying to use the feature of providing dropdown to select a node and then highlighting the corresponding selected node with different color (blue).

    I am using Jung API 2.0 and following is my modified code:
    ---------------------------------------------------------------------------------------------------------------------------------
    //Added the dropdown to the JPanel
    controls.add(new JLabel("Show Vertex", SwingConstants.LEFT));
            controls.add(getSelectionBox());
    ----------------------------------------------------------------------------------------------------------------------------------
    private Component getSelectionBox() {

            Set<String> s = new TreeSet<String>();
           
            for (String v : graph.getVertices()) {
                s.add(v);
            }
            final JComboBox choices = new JComboBox(s.toArray());
            choices.setSelectedIndex(-1);
            choices.setBackground(Color.WHITE);
            choices.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String v = (String)choices.getSelectedItem();
                        pickVertex = v;
                        vv.getRenderContext().setVertexFillPaintTransformer(new MyVertexFillPaintFunction<String>());
                        //drawShortest();
                        repaint();
                }
            });
            return choices;
        }
    ----------------------------------------------------------------------------------------------------------------------------------

        public class MyVertexFillPaintFunction<V> implements Transformer<V,Paint> {

            public Paint transform( V v ) {
                if ( v == pickVertex) {
                    return Color.BLUE;
                }
                else return Color.RED;
        }
        }

    Can anyone help me out where am I going wrong?

    Thanks in advance.

     
    • Joshua O'Madadhain

      Kalpesh:

      You haven't specified a problem. 

      Joshua

       
    • Kalpesh Gada

      Kalpesh Gada - 2008-10-12

      Sorry for not making the post clear.

      Actually the problem is I can see the change in the dropdown menu but I cannot see the change of the node color which is selected from the drop down menu.

       
    • Tom Nelson

      Tom Nelson - 2008-10-12

      Your MyVertexFillPaintFunction has no knowledge of your pickVertex. You need to create a VertexFillPaintFunction that shares knowledge about what vertex has been picked, and set it on the RenderContext only one time.

      Tom Nelson

       
    • Kalpesh Gada

      Kalpesh Gada - 2008-10-13

      Well,
                 I do have VertexFillPaintFunction which has the knowledge of the picked node.

      public class MyVertexFillPaintFunction<V> implements Transformer<V,Paint> {

      public Paint transform( V v ) {
      if ( v == pickVertex) {
      return Color.BLUE;
      }
      else return Color.RED;
      }
      }

      Also I have put

      vv.getRenderContext().setVertexFillPaintTransformer(new MyVertexFillPaintFunction<String>());

      in the constructor at the time of initialization.

      One more observation: When I debug my application, if I create a break point on the following line:

      if ( v == pickVertex) {    // First line in the function

      it never enters that function whereas in the sample program of ShortestPathDemo it does enter the line when a drop down is selected.

      Any clues ???

       
      • Joshua O'Madadhain

        Kalpesh:

        Please show the entirety of your vertex paint function.  My guess is that (as Tom said) it does not have access to the code which keeps track of which vertex/vertices have been picked.

        Joshua

         
    • Kalpesh Gada

      Kalpesh Gada - 2008-10-27

      Hi Joshua,
                       Thanks for the reply. Actually your reason is right that it does not have access to the code which keeps track of the vertices picked.

      Also when I put some print statement in transform(V v) method for ShortestPathDemo, the statement gets printed whenever I click on the drop down menu or click on the graph. But if I put any print statement in my transform method for MyVertexFillPaintMethod, it is never invoked at all.

      I have one more question. Is it right to say that when repaint() method is called after selecting from drop down menu, internally the transform(V v) is called on MyVertexFillPaintMethod and the color of each node is returned ?
      My code still remains the same.
      ----------------------------------------------------------- --------------------------------------------------------------

      public class MyVertexFillPaintFunction<V> implements Transformer<V,Paint> {
             
              public Paint transform( V v ) {
                  System.out.println("Blue or Red");
                  if ( v == pickVertex) {
                      return Color.BLUE;
                  }
                  else return Color.RED;
          }
          }

      -------------------------------------------------------------------------------------------------------------------------------
      //In the constructor

      graph = new DirectedSparseGraph<String, String>();

      final Layout<String,String> layout = new ISOMLayout<String,String>(graph);
              vv = new VisualizationViewer<String, String>(layout);

      vv.getRenderContext().setVertexFillPaintTransformer(new MyVertexFillPaintFunction<String>());

      -------------------------------------------------------------------------------------------------------------------------------

      private Component getSelectionBox() {

              Set<String> s = new TreeSet<String>();
             
              for (String v : graph.getVertices()) {
                  s.add(v);
              }
              final JComboBox choices = new JComboBox(s.toArray());
              choices.setSelectedIndex(-1);
              choices.setBackground(Color.WHITE);
              choices.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      String v = (String)choices.getSelectedItem();
                          pickVertex = v;
                          repaint();
                  }
              });
              return choices;
          }
      -----------------------------------------------------------------------------------------------------------------------
      I appreciate all your help.
      Thanks

       
      • Joshua O'Madadhain

        Kalpesh:

        Please take a look at how the existing samples handle picked vertices.  There's a built-in mechanism in JUNG to handle this which you seem not to be using.

        "if I put any print statement in my transform method for MyVertexFillPaintMethod, it is never invoked at all. "

        Are you sure that you're not doing something like creating two separate instances of VisualizationViewer (maybe overwriting one with the other)?

        I don't really understand what you're getting at with your last question, especially the last sentence.  Please expand.

        Joshua

         
    • Kalpesh Gada

      Kalpesh Gada - 2008-10-30

      Hi Joshua,
                      Thanks a lot for the reply. I am finally able to select the node and change it to a different color. I actually had two statements one after another due to which the setVertexFillPaintTransformer
      was getting overwritten (according to my understanding). Following are the those two statements:
      ----------------------------------------------------------------------------------------------------------------------------
      vv.getRenderContext().setVertexFillPaintTransformer(new MyVertexFillPaintFunction<String>());

      vv.getRenderer().setVertexRenderer(new GradientVertexRenderer<String, String>(Color.white,
                              Color.red, Color.white, Color.blue, vv
                                      .getPickedVertexState(), false));
      --------------------------------------------------------------------------------------------------------------------------------

      It does work fine but now the change is not spontaneous. Once I select the node and only when I do some zoom-in/out or try do some mouse movement activities in the graph, the graph gets updated and then the changed color is displayed. I have around 132 nodes in my graph and 260 edges.
      Can this be the reason for slow performance and update of graph?

      Thanks,
      Kalpesh

       
      • Joshua O'Madadhain

        Kalpesh:

        Sounds like repaint() is not getting called; not sure why.  Tom?

        Joshua

         

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks