Problem in displaying nodes through PREFUSE library for Java?

懵懂的女人 提交于 2019-11-29 15:52:13

The paradigm for assigning different shapes to nodes is with a DataShapeAction

E.g. in the "Congress" demo (the same applies to Nodes as Tables):

int[] shapes = new int[]
            { Constants.SHAPE_RECTANGLE, Constants.SHAPE_DIAMOND };
DataShapeAction shape = new DataShapeAction(group, "Senate", shapes);

This assigns different shapes to data points based on the value in the "Senate" data field, i.e. senators are one shape, and congressmen are another shape, in some order (there are various controls for this in the API, see Constants.ORDINAL for an example).

So, in other words, you would probably use your "type" data field to indicate what kind of node the node was, and then use a DataShapeAction to assign different shapes.

Defining a new shape is certainly possible, but will require some more tinkering. I'll try to get back to you with a better answer, but I'm guessing the most straightforward way would be to write your own subclass of the noderenderer that was capable of drawing your desired shape, and then perhaps extending DataShapeAction to handle some flag for your new data type. More on that later, though, hopefully.

You don't need predicates to assign shapes. In fact, in order to draw custom shapes you have to subclass the shape drawing renderer ShapeRenderer. ShapeRenderer distinguishes between shapes using id number (int). These ints are in structure Constants for all the standard shapes - like bcr wrote, for example Constants.SHAPE_RECTANGLE.

Internally prefuse calls ShapeRenderer's protected Shape getRawShape(VisualItem item) function. In turn, this function calls other internals from ShapeRenderer in order to get the shape to draw. For example:

  • to get the shape id, getRawShape calls int stype = item.getShape() (set by shape action DataShapeAction)
  • then, hawing the shape id at hand, there is switch statement selecting proper shape to draw

    switch ( stype )  
    {  
    case Constants.SHAPE_NONE:  
        return null;  
    case Constants.SHAPE_RECTANGLE:  
        return rectangle(x, y, width, width);  
    case Constants.SHAPE_ELLIPSE:  
        return ellipse(x, y, width, width);  
    case Constants.SHAPE_TRIANGLE_UP:  
        return triangle_up((float)x, (float)y, (float)width);  
    ...  
    

In order to draw some other shapes (custom ones) you subclass ShapeRenderer and provide your own implementation of shape to draw and override getRawShape.
If you recognize the shape id as your own you return your shape otherwise you call super(item) in your implementation of getRawShape in order to call the standard one.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!