如何使用Java2D图形适当地绘制与间隔很近的点的粗线? [英] How do I draw thick lines with closely spaced points properly with Java2D graphics?

查看:129
本文介绍了如何使用Java2D图形适当地绘制与间隔很近的点的粗线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Java2D绘制地图。当我的地图缩小时,我的道路上充满了绘画作品。在绘制完整的美国状态时,这是屏幕的一小部分:



这是类似的路段,当放大距离时:



使用的线条样式是一条蓝色的实线,其宽度缩放为相当于2个像素。
我试过了各种渲染提示和行加入规则,似乎没有任何帮助。



我在运行OS / X 10.8,这在具有Sun JDK 1.6的Linux机器上也是可重现的。

所有形状和转换尽可能使用Java2D的双精度。
这条线的几何图形有很多间距很近的点,我怀疑绘制假象的原因是渲染器被比单个像素更近的连续点混淆。



是否有一种方法可以在不减少点的情况下改善缩小形状的外观?

编辑
绘图工件位于单独线段相交的点上,所以缺失的像素与即使端点相同,也不会遇到线帽(结束)。该图显示了两条线段之间的交汇点。我已经用7像素缩放线条样式(用白色进行XOR-ed)突出显示了每条线段,但是如果您仔细观察,您仍然可以看到部分原始蓝色线条(这是由于圆顶重叠和XOR绘制模式。)在普通的尺度上,两端似乎重叠,但是当缩小到普通的涂色模式时,会出现折线效果。

一种解决方法是加入所有在绘制它们之前,连续的线段会一起分割,但我仍然想知道绘制文物的真正原因。

解决方案

我无法重新创建你已经使用OS X 1.6 JDK的情况,但我仍然有一些建议。



如果你只是用这个来概述状态,考虑使用 GeneralPath class 。您可以使用 lineTo(x,y)方法建立线上的每个点。同样,因为我不能使用 Line2D.Double 重新创建您的问题,所以我不知道这是否会有任何不同。



其次,可能更重要的是,你如何放大和缩小。我正在使用 AffineTransform (带有 setScaleTo(x,y))在我的 Graphics2D 对象上,一切都在顺利进行。与通过缩放因子(或其他任何您可以执行的操作)缩放数据中的点的方法相比,这非常简单。您还必须通过该因子调整线条的行程,因为它会缩小一切。如果您愿意,我可以发布截图。


I'm trying to draw maps using Java2D. When my map is zoomed out my roads are full of drawing artefacts. This is a small part of the screen when drawing a complete US state:

This is a similar section of the road when zoomed closer in:

The line style used is a solid blue line with width scaled to be equivalent to 2 pixels. I've tried various rendering hints and line joining rules and nothing seems to help.

I'm using Open JDK 1.7 on a Mac running the OS/X 10.8 and this is also reproducible on a Linux machine with a Sun JDK 1.6.

All shapes and transforms are double precision as far as possible with Java2D. The geometry of the line has many closely spaced points and I suspect that the cause of the drawing artefacts is that the renderer is getting confused by consecutive points that are closer than a single pixel.

Is there a way to improve the appearance of the zoomed out shapes without thinning the points?

Edit The drawing artefacts are at the points where separate line segments meet, so the missing pixels are something to do with the line caps (ends) not meeting, even when the end points are identical. This image shows the meeting point between two line segments. I have highlighted each line segment in a 7 pixel scaled line style (XOR-ed with white) but if you look very closely you can still see part of the original blue line (this is due to the rounded caps overlapping and the XOR draw mode.) At ordinary scales the ends seem to overlap, but when zoomed out and back in ordinary paint mode there is a broken line effect.

One workaround would be to join all the contiguous line segments together before drawing them, but I would still like to know the real cause of the drawing artefacts.

解决方案

I am unable to recreate the situation you have using the OS X 1.6 JDK, but I still have some suggestions for you.

If you are just using this to outline states, consider using the GeneralPath class. You can use the lineTo(x,y) method to establish each of your points on the line. Again, because I can't recreate your problem using Line2D.Double, I don't know if this will actually be any different.

Second, and possibly more importantly, is how you are zooming in and out. I am using an AffineTransform (with setScaleTo(x,y)) on my Graphics2D object, and everything is working swimmingly. Compared to the alternative of scaling the points in your data by a zoom factor (or whatever else you could do), this is fairly easy. You'll also have to adjust the stroke of the lines by the factor, because it will scale everything down. I can post screenshots if you'd like.

这篇关于如何使用Java2D图形适当地绘制与间隔很近的点的粗线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆