简体   繁体   English

如何使用Path2D(或任何Shape抽屉)在Java中绘制SVG路径点?

[英]How to draw SVG path points in Java using Path2D (or any Shape drawer)?

I've been looking for a way to take the points from a SVG and apply that in a Path2D painter. 我一直在寻找一种方法来从SVG获取要点,并将其应用于Path2D画家中。

The parsing task is not my point here, the problem is understand how the points works in a canvas and how to apply it to a Path2D . 解析任务不是我要说的,问题是要了解这些点在画布中的工作方式以及如何将其应用于Path2D

For example, consider I have this SVG tag: 例如,考虑我有这个SVG标签:

 <svg> <path d = " m 51.688 5.25 c 10 20 30 40 50 60 z"> </path> </svg> 

This just moves the drawer to the first point, draws a curve using these points and closes the path. 这只是将抽屉移动到第一个点,使用这些点绘制曲线并关闭路径。

I tried to apply this to a Path2D like this: 我试图将其应用于这样的Path2D

float x = 200, y = 200;
Path2D.Float painter = new Path2D.Float();

painter.moveTo(51.688+x, 5.25+y);
painter.curveTo(10+x, 20+y, 30+x, 40+y, 50+x, 60+y);
painter.closePath();

As the points from SVG are relatives I summed x and y fields to points, but I got this result: 由于SVG的点是亲戚,因此我将x和y字段求和,但是得到了以下结果: 在此处输入图片说明

This was just a little test to check if the drawing works and I need use this with more complex paths, and as I need to draw up to 50 SVG paths Batik was not a good approach, considering it was so slow to render only one svg image of my project. 这只是一个小测试,以检查绘图是否正常工作,我需要在更复杂的路径上使用它,并且由于需要绘制最多50条SVG路径,因此Batik并不是一个好方法,因为仅渲染一个svg太慢了我的项目的图片。

Then what I need to know is if is possible to draw the SVG path using its points directly like this. 然后我需要知道的是,是否有可能像这样直接使用其点绘制SVG路径。 If yes, how to fix my ideia? 如果是,如何解决我的想法? If not, how a good way to draw the path? 如果没有,如何绘制道路? Ps: i'm targeting swing . 附言:我的目标是swing

If you would like to understand how SVG path commands work, the best source is to read the SVG specification . 如果您想了解SVG路径命令是如何工作的,最好的资料是阅读SVG规范 It is all explained clearly there. 在那里,一切都清楚地解释了。 I'm not sure why you came here to ask first. 我不确定你为什么来这里首先问。

Lower case path commands (like c ) are relative . 小写的路径命令(如c )是相对的 Their coordinates should be considered offsets from the end point of the last path segment. 应将其坐标视为距最后路径段终点的偏移量。

So the equivalent to your path description should be: 因此,等效于您的路径描述应为:

painter.moveTo(51.688, 5.25);
painter.curveTo(51.688 + 10, 5.25 + 20, 51.688 + 30, 5.25 + 40, 51.688 + 50, 5.25 + 60);
painter.closePath();

(I've left out your x and y parameters here for the sake of clarity). (为清楚起见,我在这里省略了xy参数)。

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

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