简体   繁体   English

如何在Java 2D GeneralPath上设置不同的笔划?

[英]How to set different strokes on a Java 2D GeneralPath?

I want to draw a math symbol with GeneralPath. 我想用GeneralPath绘制一个数学符号。

GeneralPath gp1 = new GeneralPath();
gp1.moveTo( 50, 10 );
gp1.lineTo( 50, 80 );
gp1.closePath();

Now I want to draw one line thicker that the other and the connection should look nice. 现在我想绘制一条更粗的线,另一条线和连接看起来不错。 Lets take the < for example where I want the upper line / four times thicker the \\ line. 让我们拿<,例如我想要上线/四倍厚的\\ line。 If I draw the lines separately the connection looks wrong. 如果我单独绘制线条,则连接看起来不对。

There is no way to draw two parts of the path with two different line thicknesses in one call to Graphics2D.draw . 在对Graphics2D.draw一次调用中,无法绘制具有两种不同线宽的路径的两个部分。 You would have to do it in two calls. 你必须在两个电话中完成。 You might be able to address the connection by setting the cap on the lines differently, like to rounded, for example. 例如,您可以通过以不同方式设置线条上限来解决连接问题,例如舍入。 If you do this, and the two lines end at the same point, the rounded edge should cover the ends and make it look better. 如果这样做,并且两条线在同一点结束,圆形边缘应覆盖两端并使其看起来更好。 As long as you're not using alpha values to set transparency, this should work great. 只要您不使用alpha值来设置透明度,这应该很有效。

g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_ROUND);

If you're using transparencies, the above approach would make the join be darker because it's being drawn on twice. 如果您使用的是透明胶片,则上述方法会使连接变暗,因为它会被绘制两次。 In this case, you could try a different approach. 在这种情况下,您可以尝试不同的方法。 Define one shape and use Graphics2D.fill and set all of the points and arcs manually, making one line slightly thicker. 定义一个形状并使用Graphics2D.fill并手动设置所有点和弧,使一条线稍厚。 You would absolutely have to have anti-aliasing turned on for it to work, and I'm not sure how it would look. 您必须打开抗锯齿功能才能正常工作,我不确定它的外观。

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

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