java - Shape not stopping at the down boundary -


i have circle i'm dragging around screen. when hits left boundary, stops. when hits right boundary, stops. when hit top boundary stops. when hits bottom boundary, reason, keeps on going. please help.

this mouseevents class:

public class mouseevents {  int x1; int x2; int y1; int y2; int dbmppafx;// difference-between-mouse-pointer-position-and-first-x-position int dbmppafy;// difference-between-mouse-pointer-position-and-first-y-position int width; int height; static boolean inside_; static string whichbound = ""; static string text = "";  mouseevents(int x1, int y1, int width, int height) {      this.x1 = x1;     this.y1 = y1;     this.width = width;     this.height = height;     this.x2 = x1 + width;     this.y2 = y1 + height;  }  static string hitbound(mouseevents e, int width, int height) {     whichbound = "";      if (e.x1 <= 0) {         whichbound = "left";         e.x1 = 0;         e.x2 = e.x1 + e.width;      }     if (e.x2 >= width) {         whichbound = "right";         e.x2 = width;         e.x1 = e.x2 - e.width;      }     if (e.y1 <= 0) {         whichbound = "up";         e.y1 = 0;         e.y2 = e.y1 + e.height;      }     if (e.y2 >= height) {         whichbound = "down";         e.y2 = height;         e.y1 = e.y2 - e.height;      }      return whichbound;  }  static void showlocationofxandy(mouseevents e, int x, int y) {      e.text = integer.tostring(x) + "," + integer.tostring(y);  }  static boolean inside_(mouseevents e, int x, int y) {     if (x > e.x1 && x < e.x2 && y > e.y1 && y < e.y2) {         e.dbmppafx = x - e.x1;         e.dbmppafy = y - e.y1;         e.x1 = x - e.dbmppafx;         e.y1 = y - e.dbmppafy;         e.x2 = e.x1 + e.width;         e.y2 = e.y1 + e.height;          return true;     } else {         return false;     }  }  static void dragshape(mouseevents e, int x, int y) {      e.x1 = x - e.dbmppafx;     e.y1 = y - e.dbmppafy;     e.x2 = e.x1 + e.width;     e.y2 = e.y1 + e.height; }  } 

this panel class:

public class panel extends jpanel { int panelwidth; int panellength;  static panel panel = new panel(400, 400); static jframe frame = new jframe("monster"); static mouseevents circle = new mouseevents(250, 250, 50, 50); static events here = new events();  @override public dimension getpreferredsize() {     return new dimension(panelwidth, panellength); }  panel(int width, int length) {     panelwidth = width - 9;     panellength = length - 9;  }  public void paintcomponent(graphics g) {      super.paintcomponent(g);      g.setcolor(color.black);      g.drawoval(circle.x1, circle.y1, circle.width, circle.height);     g.drawstring(circle.text, 100, 100);  }  public static void main(string args[]) {      frame.add(panel);     panel.addmouselistener(here);     panel.addmousemotionlistener(here);      frame.showframe(frame, false);  }  } 

this events class:

public class events implements actionlistener, mouselistener, mousemotionlistener { string s;  public void mousedragged(mouseevent e) {      if (mouseevents.inside_ && mouseevents.hitbound(panel.circle,400,400).equals("")) {         dragshape(panel.circle, e.getx(), e.gety());         s = mouseevents.hitbound(panel.circle,400,400);         system.out.println(s);      }      if (mouseevents.inside_ && mouseevents.hitbound(panel.circle,400,400).equals("left")) {         if(panel.circle.x1>=0){             dragshape(panel.circle, e.getx(), e.gety());         }      }     if (mouseevents.inside_ && mouseevents.hitbound(panel.circle,400,400).equals("right")) {         if(panel.circle.x2<=400){             system.out.println("what...."+panel.circle.x2);             dragshape(panel.circle, e.getx(), e.gety());             showlocationofxandy(panel.circle, panel.circle.x2, panel.circle.y2);         }     }     if (mouseevents.inside_ && mouseevents.hitbound(panel.circle,400,400).equals("up")) {         if(panel.circle.y1>=0){             dragshape(panel.circle, e.getx(), e.gety());         }     }     if (mouseevents.inside_ && mouseevents.hitbound(panel.circle,400,400).equals("down")) {         if(panel.circle.y2<=400){             system.out.println("what...."+panel.circle.y2);              dragshape(panel.circle, e.getx(), e.gety());             showlocationofxandy(panel.circle, panel.circle.x2, panel.circle.y2);          }     }      panel.frame.repaint();    }  public void mousemoved(mouseevent e) {     showlocationofxandy(panel.circle, panel.circle.x2, panel.circle.y2);      panel.frame.repaint();  }  public void mouseclicked(mouseevent e) {  }  public void mouseentered(mouseevent e) {  }  public void mouseexited(mouseevent e) {  }  public void mousepressed(mouseevent e) {      panel.circle.inside_ = inside_(panel.circle, e.getx(), e.gety());     system.out.println(panel.circle.inside_);     system.out.println(hitbound(panel.circle,400,400));  }  public void mousereleased(mouseevent e) {      panel.circle.inside_ = false;        system.out.println("x1:"+panel.circle.x1+"\ny1:"+panel.circle.y1+"\nx2:"+panel.circle.x2+"\ny2:"+panel.circle.y2);  }  public void actionperformed(actionevent e) {  }  } 

if knows did wrong, please help. thanks.

simple answer, in bottom of mousedragged() method, before repaint(), add hitbound() this:

... mouseevents.hitbound(panel.circle,400,400); panel.frame.repaint(); ... 

so why happen? in each case in mousedragged method, check if hitbound equals 1 of different edge cases. works well, , code enters sections accordingly. when in 1 of cases, check if circle in valid position move, , if is, let user move circle again.

this culprit. example: in last case doesn't work, check following:

mouseevents.hitbound(panel.circle,400,400).equals("down") 

which true. continue check:

if(panel.circle.y2 <= 400) 

which true, because hitbound() call set y2 equal 400!

why other cases work then? other cases have same problem, can't see because call hitbound() later in next if cases. halt position of circle @ border, planned.

so mentioned before; simple fix problem add call hitbound() before repainting.


Popular posts from this blog