Forums

Full Version: counting the currently displayed nodes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This demo shows a way to count all currently displayed nodes. It does this by implementing a PaintListener which checks each postPaint event, if the bounds of each visible node item intersects with the bounds of the current layout and counts the current intersections.

By default all nodes are counted for which even a tiny bit can be seen (intersection). The second possibility offered is to count only nodes which are displayed completely (layout bounds contain item bounds). The number of current displayed nodes is shown in the upper left corner.

As usual with stuff in the rawish pap forum, for sure not the final insight.


The postPaint() method of the PaintListener.

Code:
public void postPaint(Display d, Graphics2D g) {
        
    Visualization vis = d.getVisualization();
    Iterator items = vis.visibleItems(nodeGroupName);

    Rectangle2D bounds = layout.getLayoutBounds();

    int count = 0;
    
    while (items.hasNext()) {
        NodeItem aItem = (NodeItem) items.next();
        if (onlyCompleteOnDisplay) {
            if (bounds.contains(aItem.getBounds())) {
                count++;
            }
         } else {
            if (!bounds.createIntersection(aItem.getBounds()).isEmpty()) {
                count++;
            }
         }
    }
    g.drawString(String.valueOf(count), 20, 20);
}

This demo requires the graphml file socialnet.xml from the prefuse demos.
Reference URL's