Reputation: 1212
(I have looked around at other questions asked on this site but none of them were of use to me, not sure I might have missed something)
I have a problem with displaying a rectangle using Java. The issue I am facing is that, the coordinate of the rectangle aren't fixed. They are getting updated from another method. At the momemt I'm using something similar such as g.fillRect(a,b,c,d) where a, b, c, and d are all variables. The problem is, they are not updating to the right coordinates.They all remain at 0.
This is my main class for how I invoke the method in my java program.
main class
//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++;
}
For the paint class:
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.
}
}
Upvotes: 2
Views: 101
Reputation: 35011
You are adding and visualizing one mainPanel and altering a 2nd non-visual one.
// HERE IS YOUR FIRST
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;
//HERE IS YOUR SECOND
mp = new mainPanel(numProcc);
Upvotes: 3
Reputation: 285403
You have TWO MainPanels, one you display and the other you update:
f1.add(new mainPanel(numProcc)); //**** MainPanel ONE ****
// ...
mainPanel mp;
mp = new mainPanel(numProcc); // **** MainPane TWO ****
Solution: use only one.
mainPanel mp;
mp = new mainPanel(numProcc);
f1.add(mp);
// ...
// mainPanel mp;
// mp = new mainPanel(numProcc);
Upvotes: 3