java - How do I register two points in a mouselistener class in JPanel? -


this one's been baffling me, i've board , want able move checker piece 1 place another.. i've set frame grid layout, , each grid cell has new jpanel implements mouselistener.. how can register points start , end can move checker piece? i'm not able save point i'm @ temp changes whenever click point..

here's code:

public class tiles extends jpanel implements mouselistener {  color c2, cc, tmp; boolean iswhite, haschecker, ishighlighted; int i, j; arraylist<point> al = new arraylist<point>(); arraylist<point> temparray = new arraylist<point>(); point start; point temp;  public tiles(color c, point s){       this.setsize(75, 75);      this.setlayout(null);      this.addmouselistener(this);      this.c2 = c;      tmp = c2;      this.start = s;    }  public void setwhite(boolean iswhite){     this.iswhite = iswhite; }  public void haschecker(boolean haschecker){     this.haschecker = haschecker; }  public void paintcomponent(graphics g){     graphics2d g2 = (graphics2d)g;     super.paintcomponent(g2);     //if(isclicked == true)     //  highlightpossiblemoves(start);     drawtile(g2);     if(haschecker == true)         addcheckers(g2);    }  public void drawtile(graphics2d g2){      g2.setcolor(c2);      g2.fillrect(3, 3, 75, 75);  }  public void addcheckers(graphics2d g2){     if(iswhite == true){         g2.setcolor(color.white);     }     else{         g2.setcolor(color.black);     }     ellipse2d.double circle = new ellipse2d.double(13, 11, 50, 50);     g2.fill(circle); }  public boolean highlightpossiblemoves(point start){     al = loa.b.getpossiblemoves(start);     if(!al.isempty()){         for(int = 0; i<al.size(); i++){             loa.jboard[al.get(i).gety()][al.get(i).getx()].c2 = color.green;             loa.jboard[al.get(i).gety()][al.get(i).getx()].repaint();         }         return true;     }     return false; }  public point getthispoint(){     return this.start; }  public boolean ispossible(point p){     if (temparray.contains(p)){         system.out.println("contains");         return true;     }     return false; }      @override public void mouseclicked(mouseevent e) {  }  @override public void mouseentered(mouseevent arg0) {  }  @override public void mouseexited(mouseevent arg0) {  }  @override public void mousepressed(mouseevent e) {  }  @override public void mousereleased(mouseevent e) {         if(ishighlighted = true){             loa.initboard();         }         if(highlightpossiblemoves(getthispoint()) == true){             this.ishighlighted = true;         }                    if(ispossible(getthispoint()))             loa.move(this.temp, getthispoint());         else{             system.out.println("x =" + this.start.getx() + "y =" + this.start.gety());             this.temp = new point(getthispoint().getx(), getthispoint().gety());         }         this.repaint();         this.validate();                         } } 

you need have somewhere in mousepressed(...) method,

if (start != null) {  // second press   // second point pressed   //.... junk    start = null; set start null  } else { // first press    start = ...; } 

Comments

Popular posts from this blog

matlab - How to equate a structure array to structure array -

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -