【发布时间】:2020-01-04 05:10:45
【问题描述】:
我正在尝试添加一个通用形状以突出显示 JFreechart 中 Y 大于 X 的区域。
为此,我定义了一条通用路径。
int x3Points[] = {0, (int) Math.max(maxX, maxY), (int) Math.max(maxX, maxY), 0};
int y3Points[] = {0, (int) Math.max(maxX, maxY), (int) maxY, (int) maxY};
GeneralPath filledPolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, x3Points.length);
filledPolygon.moveTo(x3Points[0], y3Points[0]);
for (int index = 1; index < x3Points.length; index++) {
filledPolygon.lineTo(x3Points[index], y3Points[index]);
}
filledPolygon.lineTo(x3Points[0], y3Points[0]);
filledPolygon.closePath();
Graphics graphics = getGraphics(); // i am not sure if calling getGraphics() method of JFrame is a good idea. but if not how to get Graphics() to draw on chart
Graphics2D g2d = (Graphics2D) graphics;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setPaint(Color.gray);
g2d.fill(filledPolygon);
g2d.dispose();
XYShapeAnnotation xyShapeAnnotation = new XYShapeAnnotation(filledPolygon, new BasicStroke(2.f), Color.black);
renderer.addAnnotation(xyShapeAnnotation, Layer.BACKGROUND);
形状已添加到图表中,但未填充黑色。我猜我使用的 getGraphics() 方法不正确。但是如何获取 JFreechart 的图形。
【问题讨论】:
-
就我而言,我正在设置
GeneralPath.WIND_EVEN_ODD。当我删除getGraphics()并简单地使用addAnnotation()形状未填充时,它仅显示形状的边框
标签: java jfreechart