在 Flex 中使用图形
我只是在浏览用于绘制一张图表的代码。这段代码写在ColumnChart
的ItemRenderer
的updateDisplayList
函数中。我不擅长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
I was just going through one code used to draw one chart. This code is written in the updateDisplayList
function of the ItemRenderer
of ColumnChart
. I am not good at the graphics part of Flex. Can anybody please explain me what this code is doing? I can see the final output, but am not sure how is this achieved.
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();
Regards, PK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该代码正在绘制一个矩形,尽管有点迂回。
Flash中的绘图API使用“绘图头”。除了节省一些打字之外,我看不出使用
g
而不是graphics
的任何原因。g.clear()
擦除之前绘制的任何内容。g.moveTo(rc.left, rc.top)
将其移动到位,在本例中为矩形的左上角 (0,0)。g.beginFill(fill)
开始填充,这并不奇怪。g.lineTo(x, y)
调用将绘图头移动到矩形的四个角,最后g.endFill()
完成填充。这样做可以得到相同的结果:
That code is drawing a rectangle, albeit in a bit of a roundabout way.
The drawing api in flash uses a "draw head". I can't see any reason for using
g
instead ofgraphics
other than to save some typing.g.clear()
erases anything that has been drawn before.g.moveTo(rc.left, rc.top)
moves that into position, in this case the top left corner of the rectangle (0,0).g.beginFill(fill)
starts a fill, nothing surprising there.The
g.lineTo(x, y)
calls move the draw head around to the the four corners of the rectangle and finallyg.endFill()
completes the fill.You can get the same result doing this:
它基本上绘制了一个矩形。
将当前绘制位置设置为矩形的左上角,即 0, 0
从当前位置(即左上角)向矩形的右上角画一条线。
lineTo
方法更新当前位置,以便后续绘图从新点开始。绘制矩形的其余边:
查看 livedocs
Graphics
类页面以获取更多信息。Flex 中的所有可视组件都直接/间接继承自
UIComponent
类。 updateDisplayList 方法UIComponent
绘制对象和/或调整其子对象的大小和位置。这是一个高级方法,您可以在创建UIComponent
的子类时重写该方法。当您在子类中重写它时,您应该使用正确的参数调用super.updateDisplayList
以确保基类组件正确更新。It basically draws a rectangle.
Set the current drawing position to the top-left corner of the rectangle, which is 0, 0
Draw a line to top-right corner of the rectangle from the current location (which is top-left corner). The
lineTo
method updates the current location so that subsequent drawings start from the new point.Draw the remaining sides of the rectangle:
Check out the livedocs page for
Graphics
class for more info.All the visual components in Flex inherit directly/indirectly from the
UIComponent
class. The updateDisplayList method ofUIComponent
draws the object and/or sizes and positions its children. This is an advanced method that you might override when creating a subclass ofUIComponent
. When you override it in your child class, you should callsuper.updateDisplayList
with the correct parameters to make sure that the base class components are properly updated.Degrafa 使这种事情变得更加容易。
Degrafa makes this kind of thing much easier.