JAVA - 图形未更新

JAVA - graphics not updating

(我浏览了本网站上提出的其他问题,但其中 none 对我有用,不确定我是否遗漏了什么)

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

这是我在 java 程序中调用方法的主要 class。

主要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++;
                }

油漆 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.


        }  
    }

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

        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); 

您正在添加和可视化一个主面板并更改第二个非可视化主面板。

// 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);