2011-12-06 46 views
0

我正在嘗試在Squeak-Smalltalk中用Morphic畫出四分之一圓。 它是如何工作的?Squeak Circle部分

在此先感謝!

回答

2

最好的老師是圖像本身。如果你在CircleMorph上打開一個瀏覽器,你會看到它的超類EllipseMorph定義了#drawOn :,這是變形如何繪製自己。從那裏,你可以得到所有的信息和靈感,讓你的自定義變身。

更新:我的第一個想法是手工繪製(完全覆蓋#drawOn :),但Canvas中沒有任何明顯的候選項。通過讓圓圈在將剪裁矩形設置爲四分之一的同時繪製自己,它幾乎變成了一行。

更新2:四分之一圈的技巧是讓CircleMorph爲您完成大部分工作!我想出的最好的是:

QuarterCircleMorph>>drawOn: aCanvas 

    | realBounds | 
    "Save the actual bounds of the morph" 
    realBounds := bounds. 

    "Pretend the bounds are 4x as big" 
    bounds := bounds bottom: bounds bottom + bounds height. 
    bounds := bounds right: bounds right + bounds width. 

    "Let CircleMorph handle the drawing" 
    super drawOn: aCanvas. 

    "Restore the actual bounds" 
    bounds := realBounds. 

其中QuarterCircleMorph是CircleMorph的子類。因爲你無法通過它的真實界限,所有事情都可以實現。注:在實際的代碼中,評論將是過度殺傷(除了可能是4x的,但是,那麼這是一個標誌,可能是重構:))

+0

謝謝,但它沒有那麼有用。我不明白這一點。該EllipseMorphs繪製方法:'code' drawOn:aCanvas \t aCanvas isShadowDrawing \t \t ifTrue:[^ aCanvas fillOval:邊界填充樣式:自我填充樣式邊框寬度:0 BORDERCOLOR:無]。 \t aCanvas fillOval:bounds fillStyle:self fillStyle borderWidth:borderWidth borderColor:borderColor。 'code' 但是如何繪製圓或弧的一部分? 這將是很好,如果你可以給我一個代碼snipplet。 – Janus

+0

並且讓你擺脫所有的樂趣! –

+0

現在你知道Morphs自己畫的地方,你如何調整你的子圖中的方法來做你想做的事? –