问题
I am working on a gui transformations project and i am making use of the java swing timer. The prob is that I have multiple if statements in the actionPerformed method of a jbutton. When I execute the first if statement I update a value. But when it comes to the 2nd if statement, it does not enter it. I want the first if statement to execute completely and then the 2nd and so on.. How do I achieve this? Just examine the timer part. The code has been tested for other parts Here is an extract of the code.. Thanks a lot for your help
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if( valcompo[0] == 1 ) {
Panel.Translate=true;
final Polygon temp = Panel.poly;
double[] matrix = new double[2];
matrix[0]=Double.parseDouble(tTx.getText())* Panel.scale; //getting x-coord
matrix[1]=Double.parseDouble(tTy.getText())*- Panel.scale; //getting y-coord
xTransVal=matrix[0];
yTransVal=matrix[1];
xTrans=matrix[0];
xTransTemp = xTrans/Panel.scale; //regaining the x value input
yTransTemp = yTrans/Panel.scale;
ptsTrans = new double[temp.N][2];
for(int i=0;i<temp.N;i++){
ptsTrans[i][0]=temp.X[i];
ptsTrans[i][1]=temp.Y[i];
}
final double matrixTemp[] = new double[] {2,2}; //sending 2 px at a time
Timer timer = new Timer(100,new ActionListener() {
public void actionPerformed(ActionEvent e) {
ptsTrans = Transformation.translation(ptsTrans, matrixTemp);
double [] xpts = new double[temp.N];
double [] ypts = new double[temp.N];
for(int i=0;i<temp.N;i++){
xpts[i]= ptsTrans[i][0];
ypts[i]= ptsTrans[i][1];
}
Panel.currentPolygon = new Polygon(xpts,ypts,temp.N);
Panel.undoPoly.add(Panel.poly);
Panel.poly= Panel.currentPolygon;
Panel.currentPolygon=null;
Panel.enable=true;
if(xTransVal > 0 || yTransVal > 0 ) {
if (xTransVal > 0) {
xTransVal--;
}
if (yTransVal > 0) {
yTransVal--;
}
repaint();
} else {
valcompo[0]=0;
((Timer)e.getSource()).stop();
System.out.println("stopped timer!");
System.out.println("Valcompo value changed");
}
}
});
timer.setInitialDelay(1000);
timer.start();
}
if(valcompo[1] == 2 && valcompo[0]==0) { //does not enter her and valcompo[1] is 2
System.out.println("Entered rotation!!");
Panel.Translate=false;
Panel.Rotate=true;
Panel.Scale=false;
Panel.Shear=false;
Panel.Reflect=false;
Panel.Fill=false;
//The code continues
回答1:
if this statement gets executed
if(xTransVal > 0 || yTransVal > 0 )
{
if (xTransVal > 0)
{
xTransVal--;
}
if (yTransVal > 0)
{
yTransVal--;
}
repaint();
}
your "valcompo[0]" does not get set to 0 since "valcompo[0]==0" only exists in the else statement. And you are using && operator at the and so you need both of them to me true to execute the last if statement.
来源:https://stackoverflow.com/questions/61185208/java-value-not-getting-updated-in-timer