gpt4 book ai didi

java - JSwing - 从构造函数中调用子例程不会初始化数组(它应该这样做)

转载 作者:行者123 更新时间:2023-12-02 11:30:10 29 4
gpt4 key购买 nike

我有一个 PieChart 类,它扩展了 JPanel 组件,我用它来绘制(你相信吗)饼图。

我有一个名为 createArcs() 的子例程,它创建用于绘制饼图的弧数组。必须在类中的其他位置访问正在创建的这些弧(用于测试鼠标是否位于它们上方)。

当我从 paintComponent() 子例程中调用 createArcs() 时,一切正常。这并不完全理想,因为这意味着每次调用 paintComponent() 时,都会不必要地重新计算弧。

当从构造函数中调用 createArcs() 时,JPanel 会弹出,但仅绘制背景。我正在测试其他图形对象,它们也无法渲染除背景(纯白色)以外的任何内容。看起来好像数组已初始化,因为我可以从 PaintComponent() 子函数中访问数组中弧的属性,但它只是无法绘制它们。

下面是工作代码,我想从构造函数中调用 createArcs() 而不是 paintComponent() 子函数。感谢您提供的所有帮助:)。

对于任何格式错误,我深表歉意,我是新人:^)

import java.awt.*;
import java.awt.geom.Arc2D;

import javax.swing.*;

public class PieChart extends JPanel {

public static final int xBuffer = 10;
public static final int yBuffer = 10;

public Arc2D arcs[];

private static final long serialVersionUID = 1L;

int y[];

public PieChart(int y[]) { //Constructor sets up the array
this.y = y;
}

public void setBounds(int x, int y, int width) { //Overrides so that the panel can only ever be a square
super.setBounds(x, y, width, width);
}

public int getSegment(int x, int y) {
int ret = -1;
for (int i = 0; i < arcs.length; i++) {
if (arcs[i].contains(x, y)) {
ret = i;
}
}
return ret;
}

public void paintComponent(Graphics g) {

//Arcs are calculated every time paintComponent is called :((
createArcs();
//Sets up background
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());

//Create an array same size as the int array

Graphics2D g2 = (Graphics2D) g;

g.setColor(Util.generateColor(true));

//Draws the arcs for the pie chart
g.setColor(Util.generateColor(true));
for (int i = 0; i < arcs.length; i++) {
g2.fill(arcs[i]);
g.setColor(Util.generateColor(false));
}

}

public void createArcs() {
arcs = new Arc2D[y.length];
float normalisedY[] = new float[y.length];

int maxY = 0;
int total = 0;

//Find maximum element and total of the data
for (int i = 0; i < y.length; i++) {
if (maxY < y[i]) {
maxY = y[i];
}
total += y[i];
}

//Normalise the Y values in degrees
for (int i = 0; i < y.length; i++) {
normalisedY[i] = 360 * (((float) y[i] / (float) total));
}

int degreesTravelled = 0;
//Creates arcs in a circle
for (int i = 0; i < normalisedY.length; i++) {
Arc2D arc = new Arc2D.Double(0, 0, getWidth(), getHeight(), degreesTravelled, Math.round(normalisedY[i]), Arc2D.PIE);
arcs[i] = arc;
degreesTravelled += Math.round(normalisedY[i]);
}
}

}

这是不起作用的代码:

import java.awt.*;
import java.awt.geom.Arc2D;

import javax.swing.*;

public class PieChart extends JPanel {

public static final int xBuffer = 10;
public static final int yBuffer = 10;

public Arc2D arcs[];

private static final long serialVersionUID = 1L;

int y[];

public PieChart(int y[]) { //Constructor sets up the array
this.y = y;
createArcs();
}

public void setBounds(int x, int y, int width) { //Overrides so that the panel can only ever be a square
super.setBounds(x, y, width, width);
}

public int getSegment(int x, int y) {
int ret = -1;
for (int i = 0; i < arcs.length; i++) {
if (arcs[i].contains(x, y)) {
ret = i;
}
}
return ret;
}

public void paintComponent(Graphics g) {

//Arcs are calculated every time paintComponent is called :((

//Sets up background
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());

//Create an array same size as the int array

Graphics2D g2 = (Graphics2D) g;

g.setColor(Util.generateColor(true));

//Draws the arcs for the pie chart
g.setColor(Util.generateColor(true));
for (int i = 0; i < arcs.length; i++) {
g2.fill(arcs[i]);
g.setColor(Util.generateColor(false));
}

}

public void createArcs() {
arcs = new Arc2D[y.length];
float normalisedY[] = new float[y.length];

int maxY = 0;
int total = 0;

//Find maximum element and total of the data
for (int i = 0; i < y.length; i++) {
if (maxY < y[i]) {
maxY = y[i];
}
total += y[i];
}

//Normalise the Y values in degrees
for (int i = 0; i < y.length; i++) {
normalisedY[i] = 360 * (((float) y[i] / (float) total));
}

int degreesTravelled = 0;
//Creates arcs in a circle
for (int i = 0; i < normalisedY.length; i++) {
Arc2D arc = new Arc2D.Double(0, 0, getWidth(), getHeight(), degreesTravelled, Math.round(normalisedY[i]), Arc2D.PIE);
arcs[i] = arc;
degreesTravelled += Math.round(normalisedY[i]);
}
}

}

最佳答案

Arc2D arc = new Arc2D.Double(0, 0, getWidth(), getHeight(), degreesTravelled, Math.round(normalisedY[i]), Arc2D.PIE);

看起来您有基于面板宽度/高度的逻辑。

显然,创建面板时,在框架可见之前不会有尺寸。

绘画方法应该只绘制组件的状态,而不是改变状态。

因此,也许您需要向面板添加一个 ComponentListener 来监听面板大小的变化,此时您将初始化数组中的值。

关于java - JSwing - 从构造函数中调用子例程不会初始化数组(它应该这样做),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49351098/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com