首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在GeneralPath XYShapeAnnotation of JFreechart中添加JFreechart形状

在GeneralPath XYShapeAnnotation of JFreechart中添加JFreechart形状
EN

Stack Overflow用户
提问于 2020-01-04 05:10:45
回答 1查看 171关注 0票数 1

在JFreechart中,我试图添加一个普通形状来突出Y大于X的区域。

为此,我定义了一条一般的路径。

代码语言:javascript
复制
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的图形。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-07 04:08:22

不要使用getGraphics();在随后的更新之后,返回的图形上下文将变成invalid。相反,在您的fillPaint构造函数中指定所需的XYShapeAnnotation。稍后对draw()的调用将相应地fill() Shape,如下面的示例所示。注意一些常见的缺陷:

如所示,here.

  • The形状坐标必须在数据空间中指定,如here.

  • The缠绕规则所示,定义fill()的内部,如所讨论的here.

代码语言:javascript
复制
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.geom.GeneralPath;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.ui.Layer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see https://stackoverflow.com/q/59588078/230513
 * @see http://stackoverflow.com/a/35236100/261156
 */
public class AnnotationTest {

    private static final BasicStroke STROKE = new BasicStroke(2.0f);
    private static final int N = 16;
    private static final int W = 1;
    private static final int H = W;

    public static void main(String[] args) {
        EventQueue.invokeLater(new AnnotationTest()::display);
    }

    private void display() {
        XYDataset data = createDataset();
        JFreeChart chart = ChartFactory.createXYLineChart("Annotation Test", "X", "Y",
            data, PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = chart.getXYPlot();
        XYLineAndShapeRenderer renderer
            = (XYLineAndShapeRenderer) plot.getRenderer();
        renderer.addAnnotation(new XYShapeAnnotation(initPath(4, 4),
            STROKE, Color.gray, Color.red), Layer.FOREGROUND);
        renderer.addAnnotation(new XYShapeAnnotation(initPath(8, 8),
            STROKE, Color.gray, Color.blue), Layer.FOREGROUND);
        renderer.addAnnotation(new XYShapeAnnotation(initPath(12, 12),
            STROKE, Color.gray, Color.green), Layer.FOREGROUND);
        ChartFrame frame = new ChartFrame("Annotation Test", chart);
        frame.pack();
        frame.setSize(640, 480);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private GeneralPath initPath(int x, int y) {
        GeneralPath path = new GeneralPath();
        path.moveTo(x, y);
        path.lineTo(x - W, y - H);
        path.lineTo(x + W, y - H);
        path.lineTo(x - W, y + H);
        path.lineTo(x + W, y + H);
        path.lineTo(x, y);
        return path;
    }

    private static XYDataset createDataset() {
        XYSeriesCollection result = new XYSeriesCollection();
        XYSeries series = new XYSeries("Test");
        for (int i = 0; i < N; i++) {
            series.add(i, i);
        }
        result.addSeries(series);
        return result;
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59588078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档