swing - removeMouseListener() won't work (Java) -


i have problem in supposed simple task. following class represents jpanel picture. after every time draw shape drag , release want mouse unresponsive in regards frame/panel/component. trying removing mouselisteners in every possible way can see in method mousereleased(...) happens when finish drawing shape, mouse continues responsive , every time press the button on frame continues draw shapes(with flawed logic).

how remove mouselistener can click , whatever want mouse when addshape() method complete? thanks! package question2;

import java.awt.borderlayout; import java.awt.color; import java.awt.component; import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter; import java.awt.event.mousemotionlistener;  import javax.swing.jbutton; import javax.swing.joptionpane; import javax.swing.jpanel;  @suppresswarnings("serial") public class picturepanel extends jpanel { // todo     static final int none = -1,             lines_only = 0, bounded_shapes = 1, all_shapes = 2,             rectangle = 0, oval = 1, line = 2,             deletion = 11;      private int shapesmode;     private int actionmode;     private picture<shape> picture;     private jpanel controls;     private jbutton addshapebtn, removeshapebtn;      private shape tempshape; // drawing picture dragging     private question2.point startdrag, enddrag;       public picturepanel(int shapesmode) {         this.shapesmode = shapesmode;         picture = new picture<shape>();          addshapebtn = new jbutton("add shape");         removeshapebtn = new jbutton("remove shape");          controlslistener l = new controlslistener();         addshapebtn.addactionlistener(l);         removeshapebtn.addactionlistener(l);          controls = new jpanel();         controls.add(addshapebtn);         controls.add(removeshapebtn);          this.setlayout(new borderlayout());         this.add(controls, borderlayout.south);         tempshape = new line(0, 0, 1000, 100, color.black);         picture.add(tempshape);      }      private class mouselistener extends mouseadapter implements mousemotionlistener {         public void mouseclicked(mouseevent e) {             switch (actionmode) {                 case deletion:                     question2.point clickposition = new question2.point(e.getx(), e.gety());                     picture.remove(clickposition);                     picturepanel.this.removemouselistener(this);                      repaint();                     actionmode = none;                     break;              }             question2.point clickposition = new question2.point(e.getx(), e.gety());             picture.remove(clickposition);             picturepanel.this.removemouselistener(this);              repaint();         }      }     private class controlslistener implements actionlistener {          @override         public void actionperformed(actionevent e) {             if (e.getsource() == addshapebtn) {                 if (shapesmode == lines_only) {                     addshape(line);                 } else if (shapesmode == bounded_shapes) {                     addshape(chooseboundedshape(picturepanel.this));                 } else {                     addshape(chooseanyshape(picturepanel.this));                 }              } else if (e.getsource() == removeshapebtn) {                 removeshape();             }          }     }      private void addshape(int shapetype) {          this.addmouselistener(new mouseadapter() {             public void mousepressed(mouseevent e) {                 startdrag = new question2.point(e.getx(), e.gety());                 enddrag = startdrag;                 repaint();              }              public void mousereleased(mouseevent e) {                 picture.add(tempshape);                  removemousemotionlistener(this);                 (java.awt.event.mouselistener m: picturepanel.this.getmouselisteners()){                     picturepanel.this.removemouselistener(m);                 }                 repaint();             }          });          this.addmousemotionlistener(new mousemotionadapter() {             public void mousedragged(mouseevent e) {                 picturepanel.this.repaint();                  enddrag = new question2.point(e.getx(), e.gety());                 switch (shapetype) {                     case line:                         tempshape =                                 new line(startdrag.getx(), startdrag.gety(), enddrag.getx(),                                         enddrag.gety(), new color(((int) (math.random() * 255)), ((int) (math.random() * 255)),                                                 ((int) (math.random() * 255))));                          break;                     case oval:                         tempshape =                                 new oval(math.min(startdrag.getx(), enddrag.getx()),                                         math.min(startdrag.gety(), enddrag.gety()),                                         math.abs(enddrag.getx() - startdrag.getx()),                                         math.abs(enddrag.gety() - startdrag.gety()),                                         new color(((int) (math.random() * 255)), ((int) (math.random() * 255)), ((int) (math.random() * 255))), false);                         break;                     case rectangle:                         tempshape =                                 new rectangle(math.min(startdrag.getx(), enddrag.getx()),                                         math.min(startdrag.gety(), enddrag.gety()),                                         math.abs(enddrag.getx() - startdrag.getx()),                                         math.abs(enddrag.gety() - startdrag.gety()),                                         new color(((int) (math.random() * 255)), ((int) (math.random() * 255)), ((int) (math.random() * 255))), false);                         break;                 }                 repaint();             }         });      }      private void removeshape() {         system.out.println("click on shape remove");         mouselistener ml = new mouselistener();         picturepanel.this.addmouselistener(ml);      }      public picture<shape> getpicture() {         return picture;     }      public void paintcomponent(graphics g) {         super.paintcomponent(g);         picture.show(g);         tempshape.draw(g);     }      public static int chooseshapestype(component parent) {         int choice = joptionpane.showoptiondialog(parent, "choose shape type", null, 0, joptionpane.question_message                 , null, new string[] {"lines only", "bounded shapes", "all shapes"}, null);         if (choice == 0)             return lines_only;         else if (choice == 1)             return bounded_shapes;         else if (choice == 2)             return all_shapes;          return none;      }      private static int chooseboundedshape(component parent) {         int choice = joptionpane.showoptiondialog(parent, "choose shape type", null, 0, joptionpane.question_message                 , null, new string[] {"rectangle", "oval"}, null);         if (choice == 0)             return rectangle;         else if (choice == 1)             return oval;          return none;     }      private static int chooseanyshape(component parent) {         int choice = joptionpane.showoptiondialog(parent, "choose shape type", null, 0, joptionpane.question_message                 , null, new string[] {"rectangle", "oval", "line"}, null);         if (choice == 0)             return rectangle;         else if (choice == 1)             return oval;         else if (choice == 2)             return line;          return none;     }   } 

let's not talk if solution practice, solve problem.

i noticed that, implement 2 listener classes. 1) mouselistener 2) controlslistener. listener removal done in mouselistener implementation, not in controlslistener.

in picturepanel though use controlslistener never mouselistener, never removes itself, mentioned above.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -