如何创建在两种颜色之间交替的虚线?

发布于 2024-11-28 04:55:42 字数 233 浏览 1 评论 0原文

我正在使用 JFreeChart,并希望使用灰色和黑色破折号之间交替的线条来绘制一系列数据,中间没有空格。即grayblackgrayblackgrayblack

JFreeChart 只接受AWT Stroke 或Shape 对象作为渲染系列的方法。

据我了解,BasicStroke 只能有一种颜色,并且在不透明和透明之间交替。是否有另一个类实现了允许多种颜色的 Stroke ?或者有其他方法可以解决这个问题吗?

I'm using JFreeChart and want to plot a series of data using a line that alternates between gray and black dashes, with no space between. i.e. grayblackgrayblackgrayblack

JFreeChart will only accept an AWT Stroke or Shape object as the method of rendering series.

As I understand it, a BasicStroke can only have one colour and alternate between opaque and transparent. Is there another class that implements Stroke that would allow multiple colours? Or is there another way to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

灯下孤影 2024-12-05 04:55:42

我在使用 JFreeChart 时也遇到过类似的问题。我最终只是扩展了适当的绘图并覆盖了一些绘制方法。这不是一个理想的解决方案,但您可以使用 JFreeChart 执行的操作有一个限制。

I've had a similar issue with JFreeChart. I ended up simply extending the appropriate plot and overriding some of the draw methods. It's not an ideal solution but there is simply a limit on what you can do with JFreeChart.

素年丶 2024-12-05 04:55:42

我在这里将冒着被击落的风险,因为我是一名 Android 程序员,没有 AWT 或 JFreeChart 的具体经验,但我会尝试一下。

您能否通过叠加两条虚线并偏移其中一条虚线的虚线相位以使虚线啮合在一起来创建在两种颜色之间交替的虚线?我刚刚查看了 AWT 的 BasicStroke 类,我发现除了指定破折号数组之外,您还可以指定破折号“阶段”。

下面是一个可能有用的 URL,它显示了设置相位如何影响虚线笔画(靠近页面底部):

I'm going to risk being shot down in flames here because I'm an Android programmer without specific experience of AWT or JFreeChart, but I'll have a go.

Can you create your dashed line that alternates between two colours by superimposing two dashed lines, and offsetting the dash phase of one of them so that the dashes mesh together? I just looked at AWT's BasicStroke class and I see that as well as specifying a dash array, you can specify the dash 'phase' as well.

Here's a potentially useful URL that shows how setting the phase affects the dashed stroke (near the bottom of the page): http://docstore.mik.ua/orelly/java-ent/jfc/ch04_05.htm

我很坚强 2024-12-05 04:55:42

您可以尝试循环 LinearGradientPaint 与您的 BasicStroke

附录:要渲染锐边,请考虑 java.awt.TexturePaint 。要渲染正交键,请考虑 java.awt.geom .Path2D

You might try a cyclic LinearGradientPaint in conjunction with your BasicStroke.

Addendum: To render sharp edges, consider java.awt.TexturePaint. To render orthogonal bondaries, consider java.awt.geom.Path2D.

彼岸花似海 2024-12-05 04:55:42

我找到了一种方法来做到这一点,而无需重写任何 paint()paintComponent() 方法。本质上,您要做的就是使用 drawRect() 制作两个矩形,使用两种不同类型的笔画,其中一种笔画的相位属性略高于另一种,并使用两种不同的颜色。如下:


g.setColor(Color.black);
g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{5, 5}, 0));
g.drawRect(0, 0, 100, 100);
        
g.setColor(Color.white);
g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{5, 5}, 5));
g.drawRect(0, 0, 100, 100);

BasicStroke 构造函数的最后一个变量是阶段,这就是您要查找的内容。

I found a way to do it without overriding any paint() or paintComponent() methods. Essentially what you do is make two rectangles with drawRect(), using two different kinds of strokes, one with the stroke's phase property slightly higher than the other, and two different colors. Here it is:


g.setColor(Color.black);
g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{5, 5}, 0));
g.drawRect(0, 0, 100, 100);
        
g.setColor(Color.white);
g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{5, 5}, 5));
g.drawRect(0, 0, 100, 100);

The very last variable of the BasicStroke's constructor is the phase, which is what you're looking for.

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