有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

这是一个可以接受的java图形吗?

我需要在某人的类中绘制一些统计数据,该类扩展了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);
    }
}

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

谢谢


共 (2) 个答案

  1. # 2 楼答案

    在我看来,您的代码看起来不错,除了可能将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的图形库