Java 如何在JFreeChart中绘制填充矩形?

Java 如何在JFreeChart中绘制填充矩形?,java,swing,jfreechart,Java,Swing,Jfreechart,我正在使用Swing和JFreeChart制作一个小应用程序。我必须显示一个XYLineChart,我想在上面画一些填充的矩形。我使用了xyshapeanotation来绘制矩形,并尝试用Graphics2D来填充它们,但没有效果。我得到的矩形显示在图表上,但没有填充。代码如下所示: Shape rectangle = new Rectangle2D.Double(0, 0, 7, 1); g2.fill(rectangle); XYShapeAnnotation shapeAnnotation

我正在使用Swing和JFreeChart制作一个小应用程序。我必须显示一个
XYLineChart
,我想在上面画一些填充的矩形。我使用了
xyshapeanotation
来绘制矩形,并尝试用
Graphics2D
来填充它们,但没有效果。我得到的矩形显示在图表上,但没有填充。代码如下所示:

Shape rectangle = new Rectangle2D.Double(0, 0, 7, 1);
g2.fill(rectangle);
XYShapeAnnotation shapeAnnotation = new XYShapeAnnotation(rectangle, new BasicStroke(2.f), Color.BLACK);
shapeAnnotation.setToolTipText("1");
plot.addAnnotation(shapeAnnotation);
XYShapeAnnotation shapeAnnotation = new XYShapeAnnotation(
    rectangle, new BasicStroke(2.f), Color.BLACK, Color.BLACK);
我认为问题在于填充矩形的位置与图表不相关,但我真的不知道如何解决这个问题。我还想知道是否可以将图表中的线条显示在矩形上,因为我找不到任何方法。

使用允许您同时指定
outlinePaint
fillPaint
的构造函数。你可能想要这样的东西:

Shape rectangle = new Rectangle2D.Double(0, 0, 7, 1);
g2.fill(rectangle);
XYShapeAnnotation shapeAnnotation = new XYShapeAnnotation(rectangle, new BasicStroke(2.f), Color.BLACK);
shapeAnnotation.setToolTipText("1");
plot.addAnnotation(shapeAnnotation);
XYShapeAnnotation shapeAnnotation = new XYShapeAnnotation(
    rectangle, new BasicStroke(2.f), Color.BLACK, Color.BLACK);
作为基于此的具体示例,以下更改产生如下结果:

 renderer.addAnnotation(new XYShapeAnnotation(ellipse, stroke, color, color));

要在矩形上显示图表中的线条,请指定注释的背景层,如图所示


成功了,谢谢。你还知道如何在矩形上显示图表线条吗?@novayhulk14:很高兴这有帮助;关于“矩形上的线条”,您可能已经看到了在我之前缓存的页面。非常感谢!这正是我想要的:)