简体   繁体   English

如何创建一个带有两个圆的形状?

[英]How to create a Shape with two Circles in it?

I have created a ListView where is dynamically draws a circle of a certain color based on the objects fields. 我创建了一个ListView,它根据对象字段动态绘制某种颜色的圆。 The cell can have three states, exported, has mandatory info, does not have either. 单元可以具有三个状态,即已导出,具有必填信息,而没有一个。 The first two they have their own circle color, I would like "doesn't have either" to implement both of their graphics Two circles . 他们有自己的圈子颜色的前两个,我想“没有任何”同时实现其图形的两个圆的。 The problem is you can only set one graphic for a cell. 问题是您只能为一个单元格设置一个图形。

I tried finding a workaround, by changing the centerX of a circle and using Shape.union on both of them, but it only displays circEx. 我尝试通过更改圆的centerX并在两个圆上都使用Shape.union来找到一种解决方法,但它仅显示circEx。 Is there any way to implement this? 有什么办法可以实现呢?

listView.setCellFactory(new Callback<ListView<BusinessCard>, ListCell<BusinessCard>>(){
                    @Override
                    public ListCell<BusinessCard> call(ListView<BusinessCard> list){
                        return new ColorCell();
                    }
});



//Colors circled that indicates status of card on listView
static class ColorCell extends ListCell<BusinessCard> {
    @Override
    public void updateItem(BusinessCard item, boolean empty) {
        super.updateItem(item, empty);

        //Probably should have one circle and setFil in if statements
        Circle circMan = new Circle(0,0,3,Color.web("#ff9999"));
        Circle circEx = new Circle(10,0,3,Color.web("#808080"));  // old #e1eaea 
        Circle circDone = new Circle(0,0,3,Color.web("#99ff99")); //old #99ff99    

         if(item != null){
             setTextFill(Color.BLACK);
             setText(item.toString());

             if(item.wasExported() && !item.hasMand()){
                 setGraphic(Shape.union(circMan, circEx)); //TODO
             }               
             else if(item.wasExported()){
                 setGraphic(circEx);
             }              
             else if(!item.hasMand()){
                 setGraphic(circMan);
             }
             else{
                 setGraphic(circDone);
             }
        }
    }
}

You can use a Pane as graphic , eg HBox . 您可以使用Pane作为graphic ,例如HBox

Also you should probably not recreate the circles over and over again in the updateItem method. 另外,您可能不应该在updateItem方法中一遍updateItem重新创建圆。

static class ColorCell extends ListCell<BusinessCard> {

    private final Circle manDone = new Circle(3);
    private final Circle ex = new Circle(3);
    private final HBox circles = new HBox(4, manDone, ex);

    private static final Color EXPORTED_COLOR = Color.web("#808080");
    private static final Color MAN_COLOR = Color.web("#ff9999");
    private static final Color DONE_COLOR = Color.web("#99ff99");

    {
        setGraphic(circles);

        // hide circles
        manDone.setFill(Color.TRANSPARENT);
        ex.setFill(Color.TRANSPARENT);

        setTextFill(Color.BLACK);
    }

    @Override
    public void updateItem(BusinessCard item, boolean empty) {
        super.updateItem(item, empty);

        if (item == null) {
            // hide circles
            manDone.setFill(Color.TRANSPARENT);
            ex.setFill(Color.TRANSPARENT);

            setText(null);
        } else {
            setText(item.toString());

            ex.setFill(item.wasExported() ? EXPORTED_COLOR : Color.TRANSPARENT);
            manDone.setFill(item.hasMand()
                               ? (item.wasExported() ? DONE_COLOR : Color.TRANSPARENT)
                               : MAN_COLOR);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM