[英]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.