【发布时间】:2016-04-12 01:26:03
【问题描述】:
我正在尝试使我的饼图与模拟器运行期间更改的数据保持同步。
我的问题是饼图没有以正确的方式显示数据,我不知道问题出在哪里。
public void paintComponent(Graphics g) {
super.paintComponent(g);
int totalAmountOfCars = this.adhocCars + this.reservationCars + this.parkpassCars;
float percentageAdHoc = totalAmountOfCars <= 0 || this.adhocCars <= 0 ? 0 : adhocCars/totalAmountOfCars * 100;
float percentageParkPass = totalAmountOfCars <= 0 || this.parkpassCars <= 0 ? 0 :parkpassCars/totalAmountOfCars * 100;
float percentageReservation = totalAmountOfCars <= 0 || this.reservationCars <= 0 ? 0 :reservationCars/totalAmountOfCars * 100;
float adHocAngle = totalAmountOfCars <= 0 || this.adhocCars <= 0 ? 0 : 360/percentageAdHoc*100;
float parkPassAngle = totalAmountOfCars <= 0 || this.parkpassCars <= 0 ? 0 : 360/percentageParkPass*100;
float reservationAngle = totalAmountOfCars <= 0 || this.reservationCars <= 0 ? 0 : 360/percentageReservation*100;
Dimension prefSize = this.getPreferredSize();
g.setColor(Color.WHITE);
g.fillRect(prefSize.width/2, prefSize.height/2, 200, 200);
在这里我计算弧的角度..
g.setColor(Color.RED);
g.fillArc(prefSize.width/2 + 10, prefSize.height/2 + 10, 180, 180, 0,(int)adHocAngle);
g.setColor(Color.BLUE);
g.fillArc(prefSize.width/2 + 10, prefSize.height/2 + 10, 180, 180, (int)adHocAngle, (int)adHocAngle+(int)parkPassAngle);
g.setColor(Color.ORANGE);
g.fillArc(prefSize.width/2 + 10, prefSize.height/2 + 10, 180, 180,(int)adHocAngle+(int)parkPassAngle, (int)reservationAngle);
这部分应该填充它,在某些情况下,整个字符都填充为橙色,而在其他情况下,它是 99% 的红色和一条蓝色的细线。例如,而不是 30%、40%、30%。
【问题讨论】:
-
我们大多数人都很难猜出哪里出了问题,因为我们无法测试小的孤立代码。我们不希望看到一个完整的大型程序,因为这会让我们不知所措,其中大部分与您的问题完全无关,但是当发布者付出一点努力来创建一个小的可编译和可运行演示时,我们非常感谢程序,mcve。请查看链接以了解有关我们要求您发布的内容的更多详细信息。
-
还有这个:
" My issue is that the piechart is not showing the data the correct way..."——告诉我们一点用处。您的程序行为异常究竟有多糟糕?细节很重要。 -
360/percentageAdHoc*100;360看起来很可疑。 Java 三角函数通常以弧度为单位。 -
建议 1:如果您仍然需要我们的帮助,请再次考虑创建并发布 minimal reproducible example——您是否愿意这样做取决于您,但它会再次帮助您 更更容易。建议 2:大部分代码应该在 paintComponent 之外,在单独的 且易于测试 方法中。这可以使您的调试更容易。
标签: java swing pie-chart java-2d angle