如何创建在两种颜色之间交替的虚线?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在使用 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.
我在这里将冒着被击落的风险,因为我是一名 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
您可以尝试循环
LinearGradientPaint
与您的BasicStroke
。附录:要渲染锐边,请考虑 java.awt.TexturePaint 。要渲染正交键,请考虑 java.awt.geom .Path2D。
You might try a cyclic
LinearGradientPaint
in conjunction with yourBasicStroke
.Addendum: To render sharp edges, consider java.awt.TexturePaint. To render orthogonal bondaries, consider java.awt.geom.Path2D.
我找到了一种方法来做到这一点,而无需重写任何
paint()
或paintComponent()
方法。本质上,您要做的就是使用drawRect()
制作两个矩形,使用两种不同类型的笔画,其中一种笔画的相位属性略高于另一种,并使用两种不同的颜色。如下:BasicStroke
构造函数的最后一个变量是阶段,这就是您要查找的内容。I found a way to do it without overriding any
paint()
orpaintComponent()
methods. Essentially what you do is make two rectangles withdrawRect()
, 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:The very last variable of the
BasicStroke
's constructor is the phase, which is what you're looking for.