2017-03-08 151 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); 
} 

仍然迷惘爲什麼你的實現將無法工作,因爲它看起來不錯。

相關問題