gpt4 book ai didi

JAVA - 图形不更新

转载 作者:行者123 更新时间:2023-12-03 22:38:55 25 4
gpt4 key购买 nike

(我查看了本网站上提出的其他问题,但没有一个对我有用,不确定我是否遗漏了什么)

我在使用 Java 显示矩形时遇到问题。我面临的问题是,矩形的坐标不固定。他们正在从另一种方法更新。目前我正在使用类似的东西,例如 g.fillRect(a,b,c,d),其中 a、b、c 和 d 都是变量。 问题是,它们没有更新到正确的坐标。它们都保持在 0。

这是我在 Java 程序中调用方法的主要类。

主类

 //show graph
f1.add(new mainPanel(numProcc)); //shows data for graph
f1.pack();
processDetail.dispose();
//call up process builder
connect2c c = new connect2c (); // compile and run the C code

int i = 0;
String[] value = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
mainPanel mp;
mp = new mainPanel(numProcc);
while (i < numProcc)
{
c.returnData(value[i]);
mp.getDataForDisplay();
//openFile f = new openFile (value[i]);


i++;
}

对于绘画类:

 class mainPanel extends JPanel
{
int xCoor =0;
int yCoor =0;
int width =0;
int height =0;

public mainPanel(int x)
{
//constructor staff was here
}

public void getDataForDisplay ()
{
//store in global variable

xCoor = 100; //these four should update the global variable in this class
yCoor = 150;
width = 50;
height = 50;
System.out.println("OK WERE HERE"); //used to see if we got to method...which it does
repaint(); //thought maybe this worked but it didnt
}

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


g.setColor(Color.RED);
g.fillRect (xCoor, yCoor, width, height);
System.out.println("X is:" + xCoor); //used to test value, keep getting 0 should get 100.


}
}

最佳答案

您有两个主面板,一个显示,另一个更新:

        f1.add(new mainPanel(numProcc)); //**** MainPanel ONE ****

// ...
mainPanel mp;
mp = new mainPanel(numProcc); // **** MainPane TWO ****

解决方案:只使用一个

        mainPanel mp;
mp = new mainPanel(numProcc);

f1.add(mp);

// ...
// mainPanel mp;
// mp = new mainPanel(numProcc);

关于JAVA - 图形不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29757427/

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