Menu

Picking and Selection

2004-10-23
2013-05-29
  • Michael Boeni

    Michael Boeni - 2004-10-23

    Hi all

    How do I take notice of a pick, respectively when a vertex is selected? I would like to change the color of the selected vertex and the connected vertices, depending on the selection.

    Cheers,
    Michael

     
    • Joshua O'Madadhain

      Michael:

      Take a look at VisualizationViewer.isPicked(); you can use that in the Renderer to highlight a picked vertex (and its neighbors, if you like).  It's not ideal, but it does sort of work. 

      Joshua

       
    • Michael Boeni

      Michael Boeni - 2004-10-25

      Hello Joshua

      I have tried to get to grips with VisualizationViewer in my custom VertexColorFunction and am using the following code:

      public class JungVertexColorFunction implements VertexColorFunction
      {
        Color rootColor = new Color(240, 200, 200);
        Color vertexBGColor = new Color(200,200,240);
        Color VertexPickedColor = new Color (200, 240, 200);
        Color VertexSelectedColor = new Color(240, 160, 160);
        PickedInfo pickedInfo;
        VisualizationViewer viz;

        public JungVertexColorFunction()
        {
         
        }
       
        public JungVertexColorFunction(VisualizationViewer viz)
        {
          this.viz = viz;
        }

        public Color getBackColor(Vertex v)
        {
          if (viz.isPicked(v) )
            System.out.println("PICK-O-MATIC!");
         
          if (v.getUserDatum("SELECTED") != null)return VertexSelectedColor;
          if (v.getUserDatum("root").equals("true") ) return rootColor;
          return vertexBGColor;
        }
      }

      I seem to be doing something wrong, as nothing at all happens when I click. I have 'started' the viz thread using init in the calling method.

      Cheers,
      M.

       
    • Michael Boeni

      Michael Boeni - 2004-10-25

      Anyone?

       
      • Joshua O'Madadhain

        Michael:

        Sorry; I haven't had time to get back to you with a more considered reply.  In the meantime, take a look at how VisualizationViewer itself does this, in conjunction with one of the demos that allows you to pick a node.

        Joshua

         
    • Michael Boeni

      Michael Boeni - 2004-10-27

      I took apart the VisualizationViewer and tried a few things - not to success though. I have the following construct now:

      I do the following to init/add the VisualizationViewer:
      -<snip>-
             viz = new VisualizationViewer(myCircleLayout, sr );
             viz.setBackground(Color.white);
             display.add(viz);
             viz.revalidate();
             viz.repaint();
             viz.init();
      -<snip>-

      and in my custom VertexColorFunction, I do the following:

      -<snip>-
        public JungVertexColorFunction(VisualizationViewer viz)
        {
          this.viz = viz;
        }

        public Color getBackColor(Vertex v)
        {

          if(viz.isPicked(v)) return VertexPickedColor;
          if (v.getUserDatum("root").equals("true") ) return rootColor;
          return vertexBGColor;
        }

        public Color getForeColor(Vertex v)
        {
          if (v.getUserDatum("SELECTED") != null)return Color.black;
          return Color.black;
        }
      }
      -<snip>-

      The problem now is that '     if(viz.isPicked(v)) return VertexPickedColor;' (in my VertexColorFunction)  throws a null pointer exception. I cant even pick, the exception is there as soon as I initialize the graph.

      Any ideas?

      Cheers,
      M.

       
      • Joshua O'Madadhain

        Michael:

        Sorry, I haven't given you very good guidance on this; I've been distracted with other things.  Let's try this again.  :)

        AbstractRenderer provides the isPicked() method.  So your renderer implementation (assuming it extends AbstractRenderer) has access to that.  So, as you can see in the current version of SettableRenderer, the renderer can just call isPicked(v) inside paintVertex(v).

        This, however, doesn't work if you specify a VertexColorFunction.  So what I've done in the soon-to-be-released version of SettableRenderer is the following:
        (1) SettableRenderer provides this method:

            public boolean isPicked(Vertex v)
            {
                return super.isPicked(v);
            }

        This does nothing except make the Renderer "isPicked" method public.  (I haven't yet decided whether this method should be made public in AbstractRenderer.)

        (2) Create a VertexColorFunction like the one that you have, except that it takes a SettableRenderer as its parameter instead of a VisualizationViewer.  (I think that the problem with your current version is that vv isn't initialized yet when you pass it to the JungVertexColorFunction constructor.)  Then you can call sr.isPicked().

        We hope to make this a less clunky process in the new visualization architecture; however, it's looking as though the new viz architecture won't make it out in the upcoming release.  In compensation, we will offer (among other things) the new SettableRenderer, along with some mods to VisualizationViewer (courtesy of contributor Tom Nelson) that enable zooming and panning.

        Hope this helps.

        Joshua

         
    • Michael Boeni

      Michael Boeni - 2004-10-27

      Many thanks, Joshua.

      Will try it out tomorrow (we already have 11pm now) ..:)

      Cheers,
      M.

       
    • Michael Boeni

      Michael Boeni - 2004-10-28

      As it seems, the isPicked method in the SettableRenderer is also Protected...

      FYI: Currently, I dont use a custom SR...I actually refrained from that as you have something coming anyway...I'll probably have to wait for the release.

      ...
      Cheers,
      Michael

       
    • Michael Boeni

      Michael Boeni - 2004-10-28

      Hi Joshua

      I have been able to make it work (using the VV) - but with a rather ugly workaround. But it works for the moment.

      The next issue is selection. I can pick a vertex (and it highlights accordingly) but how to select it? How can I select it by clicking (or double-clicking) it? I would like to  set/reset its selection status somehow (e.g. using a UserDatum entry).

      Is there an intended way to do this in Jung?

      Cheers,
      Michael

       
      • Joshua O'Madadhain

        Michael:

        Can you clarify what you mean by selection (and how it differs from picking)?

        I should mention that isPicked() queries a UserDatum key, so  it may be that the tagging you need is already there.

        Joshua

         
    • Michael Boeni

      Michael Boeni - 2004-10-28

      Hi Joshua

      Well, in my terms. Picking is clicking and holding a node to drag it around. Selecting is marking it for an operation to come.

      What is the userDatum that you mentioned? And how are vertices de-selected (select one, multiple, or none)?

      i would like to differentiate picking from selecting as many times, I just move vertices around without intending to do anything with them. In the other case, for example, i want to select two nodes to do some pathfinding...What would be good is something like:

      - one click: Pick (to move around, etc)
      - double click: Select (another doubleclick deselects again)

      How to do this?

      Cheers,
      M.

       
      • Joshua O'Madadhain

        Michael:

        OK, so a selection is a picking that persists. 

        For the UserDatum: take a look at VisualizationViewer.isPicked().

        Nodes are picked by a mousepressed event, and "unpicked" by a mousereleased event.  (Take a look at the GraphMouse class inside VisualizationViewer.)

        For selection: probably the easiest thing to do would be to create a parallel tag (or data structure, or whatever) that would keep track of selected vertices, and respond to clicks (or double clicks, or whatever).  For example (taken from a tool that I helped to design and build:

                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1
                            && v != null) {
                        if (isSelected(v)) {
                            nsGraphModel.unselect(v);
                        } else {
                            nsGraphModel.select(v);
                        }
                    }
                }

        Clearly we need a better mechanism built into the JUNG viz system for responding to events.  We're working on it.  :P :)

        Joshua

         

Log in to post a comment.