在 Flex 中使用图形

发布于 2024-08-21 10:15:17 字数 484 浏览 5 评论 0原文

我只是在浏览用于绘制一张图表的代码。这段代码写在ColumnChartItemRendererupdateDisplayList函数中。我不擅长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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞笑我太脆弱 2024-08-28 10:15:17

该代码正在绘制一个矩形,尽管有点迂回。
Flash中的绘图API使用“绘图头”。除了节省一些打字之外,我看不出使用 g 而不是 graphics 的任何原因。 g.clear() 擦除之前绘制的任何内容。
g.moveTo(rc.left, rc.top) 将其移动到位,在本例中为矩形的左上角 (0,0)。 g.beginFill(fill) 开始填充,这并不奇怪。
g.lineTo(x, y) 调用将绘图头移动到矩形的四个角,最后 g.endFill() 完成填充。

这样做可以得到相同的结果:

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(); 

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 of graphics 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 finally g.endFill() completes the fill.

You can get the same result doing this:

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(); 
心欲静而疯不止 2024-08-28 10:15:17

它基本上绘制了一个矩形。

//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 Graphics页面以获取更多信息。


Flex 中的所有可视组件都直接/间接继承自 UIComponent 类。 updateDisplayList 方法UIComponent 绘制对象和/或调整其子对象的大小和位置。这是一个高级方法,您可以在创建 UIComponent 的子类时重写该方法。当您在子类中重写它时,您应该使用正确的参数调用 super.updateDisplayList 以确保基类组件正确更新。

It basically draws a rectangle.

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

Set the current drawing position to the top-left corner of the rectangle, which is 0, 0

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

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

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.

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

Draw the remaining sides of the rectangle:

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

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

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 of UIComponent draws the object and/or sizes and positions its children. This is an advanced method that you might override when creating a subclass of UIComponent. When you override it in your child class, you should call super.updateDisplayList with the correct parameters to make sure that the base class components are properly updated.

一曲琵琶半遮面シ 2024-08-28 10:15:17

Degrafa 使这种事情变得更加容易。

Degrafa makes this kind of thing much easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文