【发布时间】:2015-04-21 04:48:56
【问题描述】:
(我查看了本网站上提出的其他问题,但没有一个对我有用,不确定我可能错过了什么)
我在使用 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.
}
}
【问题讨论】: