简体   繁体   English

Java Path2D性能问题

[英]Java Path2D performance issue

I have a program that utilizes Path2D.Float to draw a vector object (a large fractal design). 我有一个利用Path2D.Float绘制矢量对象(大型分形设计)的程序。 My code allows zooming and panning. 我的代码允许缩放和平移。 I have an axis object that has methods to convert world coordinates (pairs of doubles) to display coordinates (pairs of floats) based on the current scaling settings (stored in the axis object). 我有一个轴对象,该对象具有根据当前缩放设置(存储在轴对象中)将世界坐标(双精度对)转换为显示坐标(双精度浮点对)的方法。

Anyways, the vector graphic is large and detailed and contains many line segments in world coordinates. 无论如何,矢量图形既大又细,并且在世界坐标中包含许多线段。 Each time the user zooms or pans, new Path2D objects are created and rendered to the screen. 每次用户缩放或平移时,都会创建新的Path2D对象并将其渲染到屏幕上。

Everything is perfectly smooth when zoomed out. 缩小时,一切都非常流畅。 The problem occurs when I zoom in to a certain depth. 当我放大到一定深度时,会发生问题。 Apparently the Path2D lines get very long and this slows down their rendering (even though the vast majority is outside the viewing area!). 显然,Path2D线会变得很长,这会减慢其渲染速度(即使绝大多数在查看区域之外!)。 It's not my conversion algorithms consuming resources. 这不是我的转换算法消耗资源。 I profiled it and it's definitely the Java graphics drawing algorithm that's slowing down due to the size of the lines in comparison to the small clipping region. 我对它进行了剖析,这肯定是Java图形绘制算法由于与较小的裁剪区域相比行的大小而在放慢速度。

I was hoping there was a way to get Java to deal with the clipping of large lines automatically. 我希望有一种方法可以使Java自动处理大行的剪切。 I do call setClip() from the graphics object before drawing. 我确实在绘制之前从图形对象调用setClip()。 I don't see what's taking so much time. 我看不出要花多少时间。 Is there something problematic/inefficient about the clipping algorithm when lines are long in comparison the clipping rectangle? 与剪裁矩形相比,行长时,剪裁算法是否存在问题/效率低下? I don't think I'm zooming so far that my conversion from world coordinates to display coordinates is causing overflow. 我不认为我缩放得太远,以至于我从世界坐标到显示坐标的转换导致溢出。 I'll have to check for this. 我必须检查一下。 If that's the case I'll try using Path2D.double instead. 如果是这种情况,我将尝试使用Path2D.double代替。

Anyways, any help appreciated. 无论如何,任何帮助表示赞赏。 I'm sure I'll eventually figure this out but I hope someone that's encountered the same problem can give me a pointer so it doesn't take so long to figure out. 我敢肯定,我最终会解决这个问题的,但是我希望遇到相同问题的人能为我提供一个指导,这样就不需要花费太多时间了。

I've not used paths when zooming, but I have used them for drawing some very complex shapes with textures & gradients etc. Some issues I had were: 缩放时我没有使用路径,但是使用它们来绘制带有纹理和渐变等非常复杂的形状。我遇到的一些问题是:

In my experience, I had to avoid creating new Path2D objects on a per frame basis because of performance issues, not just for their recreation execution, but because it caused a lot of garbage collection with generating & then dropping so many so quickly, which slowed things down. 以我的经验,由于性能问题,我不得不避免在每个帧上创建新的Path2D对象,这不仅是因为重新执行它们,还因为它会导致大量垃圾收集,生成并丢弃的速度如此之快,这减慢了速度事情下来。 If your shape doesn't change, cache the generated path. 如果形状没有变化,请缓存生成的路径。

Avoid clipping with paths - where possible stick to rectangles - paths seem to give rough edges on curves and are more costly to use. 避免使用路径进行剪切-可能会粘到矩形上-路径似乎会在曲线上产生粗糙的边缘,并且使用起来更昂贵。

Even when clipping to smaller regions, simply asking to draw large regions could slow things down. 即使剪切到较小的区域,仅要求绘制较大的区域也可能使速度变慢。 Consider when the user zooms in to tessellate your shape, ie the shape is only as big as your viewport. 考虑用户何时放大以细分您的形状,即该形状仅与您的视口一样大。 Perhaps as you say maybe there is an issue with the clip function when dealing with large volume areas, so tessellation might help here. 也许就像您说的那样,在处理大体积区域时,剪辑功能可能存在问题,因此细分可以在这里提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM