2010-02-17 69 views
1

我剛剛通過一個用於繪製一個圖表的代碼。該代碼寫入ColumnChartItemRendererupdateDisplayList函數中。我不擅長Flex的圖形部分。任何人都可以向我解釋一下這段代碼在做什麼?我可以看到最終的輸出結果,但我不確定這是如何實現的。在Flex中使用圖形

var rc:Rectangle = new Rectangle(0, 0, width , height); 
var g:Graphics = graphics; 

g.clear();   
g.moveTo(rc.left,rc.top); 
g.beginFill(fill); 
g.lineTo(rc.right,rc.top); 
g.lineTo(rc.right,rc.bottom); 
g.lineTo(rc.left,rc.bottom); 
g.lineTo(rc.left,rc.top); 
g.endFill(); 

問候,PK

回答

0

它基本上繪製一個矩形。

//clear any existing drawings 
g.clear(); 

設置當前繪圖位置的矩形區域,是0的左上角,0

g.moveTo(rc.left,rc.top); 

//start filling with the color specified by `fill` 
g.beginFill(fill); 

畫一條線,右上方的矩形的拐角從當前位置(這是左上角)。 lineTo方法更新當前位置,以便後續圖形從新點開始。

g.lineTo(rc.right,rc.top); 

繪製矩形的其餘方面:

g.lineTo(rc.right,rc.bottom); 
g.lineTo(rc.left,rc.bottom); 
g.lineTo(rc.left,rc.top); 

//end the fill. 
g.endFill(); 

退房的livedocs page for Graphics class更多信息。


所有Flex中可視化組件直接從UIComponent類繼承/間接。 UIComponentupdateDisplayList方法繪製對象和/或尺寸並定位其子元素。這是一種高級方法,您可以在創建UIComponent的子類時覆蓋它。當您在子類中覆蓋它時,應該使用正確的參數調用super.updateDisplayList以確保基類組件得到正確更新。

+0

謝謝Amarghosh和grapefrukt,我增加了一些東西。在所有這些代碼之前,它調用 super.updateDisplayList(unscaledWidth,unscaledHeight); 的方法。這實際上會做什麼? – Anoop 2010-02-17 09:07:03

+1

@Anoop看到我的更新。 – Amarghosh 2010-02-17 09:26:46

+0

感謝兄弟。你非常有幫助:)。 – Anoop 2010-02-17 09:30:11

1

即代碼在比特的迂迴的方式繪製一個矩形,雖然。 Flash中的繪圖API使用「繪圖頭」。我看不出有任何理由使用g而不是graphics而不是保存某些輸入內容。 g.clear()刪除之前繪製的任何東西。 g.moveTo(rc.left, rc.top)將其移動到位置,在此情況下是矩形的左上角(0,0)。 g.beginFill(fill)開始填充,沒有什麼奇怪的。 g.lineTo(x, y)調用將繪圖頭移動到矩形的四個角,最後完成填充。

你可以得到相同的結果這樣做:

graphics.clear(); 
graphics.beginFill(fill); 
graphics.drawRect(0, 0, width , height); 
// this last call is only needed if you're going to draw even more, 
// if not you can omit that too 
graphics.endFill(); 
0

Degrafa使這種事情更容易。