【问题标题】:Is it acceptable to make a graph this way for JPanel?以这种方式为 JPanel 制作图表是否可以接受?
【发布时间】:2011-07-30 14:10:40
【问题描述】:

我需要在扩展JPanel 的某个类中绘制一些统计数据。目前的计划是使用paintComponentGraphics 类,如下所示:

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    //create a rectangle to represent the outline of the graph
    g.drawRect(300, 50, 400, 350);

    //set up the datapoints
    for(int i = 0; i < data.size(); i++) {
        //put the datapoints where ever they need to go within this rectangle
        g.drawOval(x, y, width, height);
    }
}

出于某种原因,这很糟糕吗?更重要的是,是否有一些很棒的、易于使用的图形库我可以使用但不知道?

谢谢。

【问题讨论】:

    标签: java swing graphics graph paintcomponent


    【解决方案1】:

    你的代码对我来说看起来不错,除了可能将你的 x 和 y 点放在

    for(int i = 0; i < data.size(); i++) {
        g.drawOval(data.get(i).getX() - width/2, data.get(i).getY() - height/2, 
          width, height);
    }
    

    使椭圆居中。

    此外,您可能需要考虑转换数据,以使绘制的点保持在图表的域和范围的边界内。否则,您可能会在矩形之外有椭圆。

    您可能想查看 JFreeChart 以了解图形库。

    【讨论】:

      【解决方案2】:

      【讨论】:

      猜你喜欢
      • 2022-06-23
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2016-02-02
      • 2016-03-09
      • 2023-03-14
      • 2010-11-16
      • 1970-01-01
      相关资源
      最近更新 更多