2017-03-08 150 views
1

我想扩展JavaFX类Line,因为我想让起点和终点成为一个圆或箭头或某物。像那样。除此之外,我想在将来添加标签。 问题是如何覆盖paint方法?什么方法负责画线,我该如何执行我的愿望? 到现在为止,我得到的,但如果我instaciate线路它doesn't改变外观:如何扩展JavaFX Line或其他形状

import javafx.scene.shape.Circle; 
import javafx.scene.shape.Line; 

public class LabeledLine extends Line { 
    private Circle startCircle; 
    private Circle endCircle; 

    public LabeledLine(){ 
     super(); 
     startCircle = new Circle(getStartX(), getStartY(), 10); 
     endCircle = new Circle(getEndX(), getEndY(), 10); 
     startCircle.setFill(getFill()); 
     endCircle.setFill(getFill()); 
    } 


    public LabeledLine(double startX, double startY, double endX, double endY){ 
     super(startX, startY, endX, endY); 
     startCircle = new Circle(getStartX(), getStartY(), 10); 
     endCircle = new Circle(getEndX(), getEndY(), 10); 
     startCircle.setFill(getFill()); 
     endCircle.setFill(getFill()); 
    } 

} 

回答

0

请注意,您所提供的实施将不会工作,例如你要做

LabeledLine l = new LabeledLine() ; 
l.setStartX(100); 

这实际上很难通过子类化Line的策略来解决这个问题。

无论如何,Shape都是通过委托给一个私有的本地对等类来呈现的,这个对等类在很多情况下(据我所知)允许通过硬件图形管道高效地渲染它们。所以在这里没有可访问的“绘画”方法供您重写。

相反,子类Group,让你的子类包装线(基本上,这是由Joshua布洛赫“在继承青睐组成”从有效的Java):

public class LabeledLine extends Group { 

    public LabeledLine(Line line) { 
     Circle startCircle = new Circle(10); 
     startCircle.centerXProperty().bind(line.startXProperty()); 
     startCircle.centerYProperty().bind(line.startYProperty()); 
     startCircle.fillProperty().bind(line.fillProperty()); 
     Circle endCircle = new Circle(10); 
     endCircle.centerXProperty().bind(line.endXProperty()); 
     endCircle.centerYProperty().bind(line.endYProperty()); 
     endCircle.fillProperty().bind(line.fillProperty()); 

     getChildren().addAll(line, startCircle, endCircle); 
    } 
} 

你可以这样使用它作为:

Line line = new Line(); 
Pane somePane = new Pane(); 
somePane.getChildren().add(new LabeledLine(line)); 

还要注意的是上述实际的实施不会增加状态或行为Group,所以你可以完全重构它作为一种方法:

public Group labelLine(Line line) { 
    Circle startCircle = new Circle(10); 
    startCircle.centerXProperty().bind(line.startXProperty()); 
    startCircle.centerYProperty().bind(line.startYProperty()); 
    startCircle.fillProperty().bind(line.fillProperty()); 
    Circle endCircle = new Circle(10); 
    endCircle.centerXProperty().bind(line.endXProperty()); 
    endCircle.centerYProperty().bind(line.endYProperty()); 
    endCircle.fillProperty().bind(line.fillProperty()); 

    return new Group(line, startCircle, endCircle); 
} 

然后

Pane somePane = new Pane(); 
Line line = new Line(); 
somePane.getChildren().add(labelLine(line)); 
+0

你试过你的实施吗?他们看起来很不错,但我想知道他们为什么不工作。如果我运行这个,我所看到的只是一条普通线路...... – jaaBeg16

0

据我所知,你不应该直接扩展基础形状的程度。而是延伸Region并使用合成Line作为子节点。

https://github.com/aalmiray/jsilhouette使用类似的方法。属性设置为“看起来像”形状,但不是。

0

我想这在这里,它运作良好:

public Group labelLine(Line line) { 
    Circle startCircle = new Circle(line.getStartX(), line.getStartY(), 10); 
    Circle endCircle = new Circle(line.getEndX(), line.getEndY(), 10); 

    return new Group(line, startCircle, endCircle); 
} 

仍然迷惘为什么你的实现将无法工作,因为它看起来不错。

相关问题