简体   繁体   中英

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**                  

                }
            }
    }
    }

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

You can't. A component will repaint itslef whenthe program invokes repaint on itself or the Swing RepaintManager determines when a component need to be repainted.

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

A painting method is for painting only. There should be no user interaction. That is you should not be displaying an option pane or any other kind of message for the user to respond to.

So the code that interacts with the user need so be coded outside of the paintComponent() method.

Also, custom painting should be done in the paintComponent() method, not the paint() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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