gpt4 book ai didi

Java 对象值不一致

转载 作者:行者123 更新时间:2023-12-02 05:29:09 25 4
gpt4 key购买 nike

我找不到任何其他解决方案来解决我的问题,因为我不确定如何用足够少的文字来描述它。

当我分配activeList时,它是currentActiveList的一个字段,是ActiveList类中随机生成的矩形,它接收到的值很好。

public class ActiveList {
Rectangle[] activeList = new Rectangle[10];

public ActiveList() {
for(int i = 0; i < activeList.length; i++)
activeList[i] = null;
}

public void addToList(Rectangle x) {
for(int i = 0; i < this.activeList.length; i++) {
if(this.activeList[i] == null) {
this.activeList[i] = x;
i = this.activeList.length+1;
}

else
this.activeList[activeList.length-1] = x;
}
}

public Rectangle[] getActiveList() {
return this.activeList;
}

public int getLength() {
//System.out.print(this.activeList.length);
return this.activeList.length;
}

public void deleteFromList(int x) {
this.activeList[x] = null;
}

public Rectangle getFromList(int x) {
Rectangle retVal = this.activeList[x];
//System.out.println("Returning getFromList(int x): " +retVal);
return retVal;
}

public void genRandomRectangle() {
Random randomNumberGenerator = new Random();
double[] pointVal = new double[4];
double randomInt = randomNumberGenerator.nextInt(400-10);
pointVal[0] = randomInt;
randomInt = randomNumberGenerator.nextInt(400-10);
pointVal[1] = randomInt;
randomInt = randomNumberGenerator.nextInt((int) (400-pointVal[0]));
if(randomInt < 5) {
randomInt = randomInt+pointVal[0]+5;
}

else
pointVal[2] = randomInt+pointVal[0];

randomInt = randomNumberGenerator.nextInt((int) (400-pointVal[1]));
if(randomInt < 5) {
randomInt = randomInt+pointVal[1]+5;
}

else
pointVal[3] = randomInt+pointVal[1];

Rectangle newRandom = new Rectangle(pointVal[0], pointVal[1], pointVal[2], pointVal[3]);
//System.out.println(pointVal[0]);
//System.out.println(pointVal[1]);
//System.out.println(pointVal[2]);
//System.out.println(pointVal[3]);
System.out.println("New Random: " +newRandom);

addToList(newRandom);
}
}

但是,当我尝试在主类 GraphicGen 中使用这些值时,currentActiveList 为其所有索引返回 null 值。

public class GraphicGen extends JPanel {
ActiveList currentActiveList = new ActiveList();
public static int gridSpaceX = 400;
public static int gridSpaceY = 400;
static JFrame mainFrame = new JFrame();

protected void paintComponent(Graphics g) {
//super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//int w = getWidth();
//int h = getHeight();
// Draw ordinate.
//g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
// Draw abcissa.
//g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
//double xInc = (double)(w - 2*PAD)/(data.length-1);
//double scale = (double)(h - 2*PAD)/maxValue();
// Mark data points.
//g2.setPaint(Color.red);

double[] coords = new double[4];
//g2.fill(new Ellipse2D.Double(coords[0], coords[1], 4, 4));
//g2.fill(new Ellipse2D.Double(coords[2], coords[3], 4, 4));
//g2.fill(new Ellipse2D.Double(100, 100, 4, 4));
System.out.println("Graphic Gen Active List Obj: " +currentActiveList);
System.out.println("Ya drew a new Main GUI!");
System.out.println("currentActiveList.getActiveList(): " +currentActiveList.getActiveList());
for(int i = 0; i < currentActiveList.getLength(); i++) {
//System.out.println("currentActiveList.getFromList(i): "+currentActiveList.getFromList(i));
//System.out.println("Graphic Gen Active List Obj: " +currentActiveList);
//System.out.println(activeList.getFromList(i).getTopLeftX());
if(currentActiveList.getFromList(i) != null) {
coords[0] = currentActiveList.getFromList(i).getTopLeftX();
System.out.println(coords[0]);
coords[1] = currentActiveList.getFromList(i).getTopLeftY();
System.out.println(coords[1]);
coords[2] = currentActiveList.getFromList(i).getBottomRightX();
System.out.println(coords[2]);
coords[3] = currentActiveList.getFromList(i).getBottomRightY();
System.out.println(coords[3]);
g2.draw(new Line2D.Double(coords[0], coords[1], coords[2], coords[1]));
g2.draw(new Line2D.Double(coords[0], coords[1], coords[0], coords[3]));
g2.draw(new Line2D.Double(coords[2], coords[1], coords[2], coords[3]));
g2.draw(new Line2D.Double(coords[0], coords[3], coords[2], coords[3]));
}
}

/*double x = 50;
double y = 50;
g2.fill(new Ellipse2D.Double(x, y, 4, 4));*/
/*for(int i = 0; i < data.length; i++) {
double x = PAD + i*xInc;
double y = h - PAD - scale*data[i];
g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
}*/
}

/*private int maxValue() {
int max = data[0];
for(int i = 0; i < data.length; i++) {
if(data[i] > max)
max = data[i];
}
return max;
}*/

public void callRepaintOnMain() {
mainFrame.repaint();
}

public void callGenRandom() {
currentActiveList.genRandomRectangle();
}

public static void main(String[] args) {
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.add(new GraphicGen());
mainFrame.setSize(gridSpaceX, gridSpaceY);
ButtonPrompt buttonPrompter = new ButtonPrompt();
mainFrame.setLocation(200,200);
mainFrame.setVisible(true);
}
}

随机生成器方法由 Action 监听器调用。

public class ButtonPrompt extends GraphicGen {

ActionListener actionListenerRandom = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
//currentActiveList.genRandomRectangle();
callGenRandom();
callRepaintOnMain();
}
};

JButton randomBtn = new JButton("Add Random Rectangle");
JButton inputCoordinates = new JButton("Input Rectangle Coordinates");

public ButtonPrompt() {
JFrame f = new JFrame("Add Rectangles");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoxLayout boxLayout = new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS);
f.setLayout(boxLayout);

randomBtn.addActionListener(actionListenerRandom);

f.setSize(200, 200);
f.setLocation(600, 200);
f.setVisible(true);
f.add(randomBtn);
f.add(inputCoordinates);
f.pack();
}
}

这是范围界定或引用问题吗?我真的很茫然。

最佳答案

这里:

public static void main(String[] args) {
...
mainFrame.add(new GraphicGen());
...
ButtonPrompt buttonPrompter = new ButtonPrompt();
}

ButtonPrompt 扩展 GraphicGen,它是一个JPanel。在 ButtonPrompt 的构造函数中,您创建了一个 JFrame 并向其中添加了两个 JButton

因此,当您的应用启动时,屏幕上将出现两个 JFrame,一个是 mainFrame,其中包含 GraphicGen,另一个是 buttonPrompter > 其中包含两个按钮。

当您点击按钮时,actionPerformed() 被调用,它实际上调用 buttonPromptercallGenRandom() -- 如果随机生成逻辑正确,生成的矩形将添加到buttonPrompter中。但您没有将此 buttonPrompter 添加到任何一个 JFrame 中,您将看不到它。

<小时/>

您可能想要什么:

ButtonPrompt 不会扩展 GraphicGen,而是为 ButtonPrompt 提供您添加到的 GraphicGen 的引用mainFrame

public class ButtonPrompt extends GraphicGen {
JButton randomBtn = new JButton("Add Random Rectangle");
JButton inputCoordinates = new JButton("Input Rectangle Coordinates");
final GraphicGen gg;

public ButtonPrompt(GraphicGen gg) {
this.gg = gg;
......

ActionListener actionListenerRandom = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
gg.callGenRandom();
gg.callRepaintOnMain();
}
};

randomBtn.addActionListener(actionListenerRandom);

......
}
}

在你的main()中:

public static void main(String[] args) {
...
GraphicGen gg = new GraphicGen();
mainFrame.add(gg);
...
ButtonPrompt buttonPrompter = new ButtonPrompt(gg);
}
<小时/>

此外,代码还有其他问题。

例如,GraphicGen是一个JPanel主类,它有一个JFrame字段,它实际上包含GraphicGen 应用程序运行时的实例——这看起来很糟糕。我对 Swing 不太了解...是否必须调用包含 JFramerepaint() 而不是仅仅调用 JPanel code> 的 repaint(),如果有的话?

关于Java 对象值不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25709692/

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