【问题标题】:JAVA - graphics not updatingJAVA - 图形不更新
【发布时间】: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.


        }  
    }

【问题讨论】:

  • 为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)或SSCCE(简短、自包含、正确示例)。
  • 类名应以大写字符开头。

标签: java swing java-2d


【解决方案1】:

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

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

【讨论】:

  • 谢谢!从来没有意识到我正在编辑一个而错过了另一个
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-07
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 2012-10-01
    相关资源
    最近更新 更多