繁体   English   中英

绘画方法被多次调用...如何限制呢?

[英]the paint method is called multiple times… how to restrict that?

   // A program for drawing simple graphs
    public class Graphpane extends JPanel { 

    int node,i,j,flag;  // node is for storing no. of nodes... i,j are counters.. flag is for storing option yes or no
    ArrayList<Integer> xaxis= new ArrayList<Integer>(); // arraylist for storing x-cordinates
    ArrayList<Integer> yaxis= new ArrayList<Integer>(); //arraylist for storing y ordinates
    Scanner in= new Scanner(System.in);

    public Graphpane(){

        super();
        System.out.println("Create your own graph!!! :)");
        System.out.println("Enter the number of nodes in your graph:");
        node= in.nextInt();
        System.out.println("Enter the x and the y vertices respectively");
        System.out.println("<<<x cordinate shouldn't exceed 600 and y ordinate shouldn't exceed 400>>>");
        for(i=0;i<node;i++)
        {
            System.out.println("Enter the x co-ordinate for"+ i+ "node");
            xaxis.add(in.nextInt());
            System.out.println("Enter the y ordinate for"+ i+ "node");
            yaxis.add(in.nextInt());
        }
        repaint();
    }

    public void paint(Graphics g) // paint method
    {
        for(i=0;i<node;i++)
        {
            g.fillArc(xaxis.get(i)- 3, yaxis.get(i)-3,6,6,0,360);// drawing  points on panel
        }
                for(i=0;i<node;i++){    
            for(j=0;j<node;j++)  // asking whether the two points are connected or not.. if connected then drawing a line between the two
            {
                if(i==j)  // would skip for the same points as simple graph.. no loops
                {
                    continue;
                }
                else{
                    System.out.println("Are the points of index  "+i+   "  and  "+j+"  connected?? Say 1 for yes or else for no");
                    flag=in.nextInt(); // flag for recording whether connected or not
                    if(flag==1)
                    {
                         g.drawLine(xaxis.get(i),yaxis.get(i),xaxis.get(j),yaxis.get(j));
   }
        //xaxis is arraylist containing x cordinates and yaxis for containing y ordinates.. i and j are indices         
        //drawing lines between the two points if connected         
                    }
     //**Here I am asked whether my two points are connected question multiple times but I want it just once**                  

                }
            }
    }
    }

绘画方法被多次调用...如何限制呢?

你不能 当程序在其自身上调用重新绘制或Swing RepaintManager确定何时需要重新绘制组件时,组件将重新绘制其lef。

//**Here I am asked whether my two points are connected 
//question multiple times but I want it just once**  

绘画方法仅用于绘画。 不应有用户交互。 也就是说,您不应该显示选项窗格或任何其他类型的消息供用户响应。

因此,与用户交互的代码需要在paintComponent()方法之外进行编码。

另外,自定义绘画应在paintComponent()方法中完成,而不是paint()方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM