incrementing the x position of an image and then decrement it back is not working in java -


im trying make 8 puzzle game , problem when value of x1 let 70 of particular image's x position incremented until reaches 170 , decremented until reaches 70 again, decrementation not working , image not moving original position anymore shaking only. why happening. in order clarify stuff can check code below:

run.java

public class run extends applet implements runnable{     public image im;         public int x1=70, y1=70;     public int x2=170, y2=70;     public int x3=270, y3=70;     public int x4=70, y4=170;     public int x5=170, y5=170;     public int x6=270, y6=170;     public int x7=70, y7=270;     public int x8=170, y8=270;     public int x9=270, y9=270;      public image offscreen;     public graphics d;     public bufferedimage container;     public bufferedimage[] tile = new bufferedimage[10];     private file loadthis;       public void combi1(){          if(x1<170 && x1>=70){              x1+=1; y2+=1; x5-=1; y4-=1;              return;                 }          if(x1>=70){              x1-=1; y2-=1; x5+=1; y4+=1;              return;                 }       }       public void run(){          try{             uri imageurl = getclass().getresource("container.png").touri();             loadthis = new file(imageurl);             container = imageio.read(loadthis);                         for(int i=0; i<10; i++){                 if(i>=1){                     uri tileurl = getclass().getresource("tile" + +".png").touri();                     loadthis = new file(tileurl);                     tile[i] = imageio.read(loadthis);                 }             }                                  }catch (ioexception e1){                 e1.printstacktrace();             } catch (urisyntaxexception e) {                 system.out.println(e);             }          while(true){                                combi1();              repaint();                     try{             thread.sleep(2);         }catch(interruptedexception e){             e.printstacktrace();         }         }      } } 

aiprojects.java

public class aiprojects extends run{     public void init(){         setsize(450,450);           thread th = new thread(this);         th.start();         offscreen = createimage(450,450);         d = offscreen.getgraphics();     }     public void paint(graphics g){         d.setcolor(color.black);                 d.fillrect(0, 0, 450, 450);         d.drawimage(container, 52, 52,340,340, this);         d.drawimage(tile[9], x9, y9, this);         d.drawimage(tile[1], x1, y1, this);         d.drawimage(tile[2], x2, y2, this);         d.drawimage(tile[3], x3, y3, this);         d.drawimage(tile[4], x4, y4, this);         d.drawimage(tile[5], x5, y5, this);         d.drawimage(tile[6], x6, y6, this);         d.drawimage(tile[7], x7, y7, this);         d.drawimage(tile[8], x8, y8, this);          g.drawimage(offscreen, 0, 0, this);     }     public void update(graphics g){         paint(g);     } } 

this code not actual code have right posted here make things clear regards problem. in advance

given

    if(x1<170 && x1>=70){          x1+=1; y2+=1; x5-=1; y4-=1;          return;             }      if(x1>=70){          x1-=1; y2-=1; x5+=1; y4+=1;          return;             } 

what happens when x1 hits 170 ? subtract 1. next time come in , since x1 169 add 1, making 170. , on ad infinitum.

i think need variable direction, 1 or -1. when hit 170 or 70, reverse value of direction. in cases use direction change x1 thus

x1 += direction; 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -