java - Programmatically swap two JPanels in JFrame -


i trying accomplish above functionality, , having little success. using gridlayout 2 columns , 2 rows show user puzzle-like game, there 4 (200x200 pixel) jpanels (3 colored, 1 default bgcolor) fill whole contentpane. clicking on colored panel resolves in assessment if panel next gray panel. if so, should swap. have accomplished every step last one, swap. here's code:

public class mainclass extends jframe { //generated private static final long serialversionuid = 4710628273679698058l;  private specialpanel redpanel; private specialpanel greenpanel; private specialpanel yellowpanel; private specialpanel graypanel;  public mainclass() {     super("puzzle");      initpanels();      setsize(410, 410);     setlayout(new gridlayout(2, 2));      this.add(redpanel);     this.add(greenpanel);     this.add(graypanel);     this.add(yellowpanel); } private void initpanels() {     redpanel = new specialpanel();     greenpanel = new specialpanel();     yellowpanel = new specialpanel();     graypanel = new specialpanel();      graypanel.setgreypanel(true);      redpanel.setbackground(color.red);     greenpanel.setbackground(color.green);     yellowpanel.setbackground(color.yellow);     graypanel.setbackground(this.getbackground());      redpanel.setsize(200, 200);     greenpanel.setsize(200, 200);     yellowpanel.setsize(200, 200);     graypanel.setsize(200, 200);      mouseadapter ma = new mouseadapter() {          @override         public void mouseclicked(mouseevent arg0) {             super.mouseclicked(arg0);              specialpanel sp = (specialpanel) arg0.getcomponent();              if (sp.getisgray()) {                 //do nothing             } else if (checkifneighbourtogray(sp, graypanel)) {                 //swap them                 system.out.println("swap notification");                 swap(sp, graypanel);                                 //update ui             } else {                 //again, nothing clicked panel diagonal gray panel             }         }          private boolean checkifneighbourtogray(specialpanel sp, specialpanel graypanel) {              point startpointsp = sp.getlocation();             double x = startpointsp.getx();             double y = startpointsp.gety();              double width = sp.getwidth();             double height = sp.getheight();              point graypoint = graypanel.getlocation();             double xg = graypanel.getx();             double yg = graypanel.gety();              double widthg = graypanel.getwidth();             double heightg = graypanel.getheight();              //gray panel right of clicked 1             if (x + width == xg && y + height == yg + heightg) {                 return true;                                 }             //gray panel left of clicked 1             else if (x - width == xg && y + height == yg + heightg) {                 return true;             }             //gray panel above of clicked 1             else if (x == xg && x + width == xg + widthg) {                 return true;             }             //gray panel under of clicked 1             else if (xg == x && yg + widthg == x + width) {                 return true;             }              return false;         }          private void swap(specialpanel sp, specialpanel graypanel) {             //swap logic         }      };     redpanel.addmouselistener(ma);     greenpanel.addmouselistener(ma);     yellowpanel.addmouselistener(ma);     graypanel.addmouselistener(ma);  } public static void main(string[] args) {     mainclass mc = new mainclass();     mc.setvisible(true);  }  } 

the class specialpanel extending jpanel new boolean property isgraypanel set false + getter , setter. note: being done in "i have basic swing skills" manner, although have learned more in meantime java swing, limited basic swing functionality, should keep way. therefore question how swap 2 panels next each other, including ui update?

  • there wouldn't need move jpanels container, otherwise woud need use bunch of quite useless code remove 2 jpanels from contianer 2 indexes layout container swaping indexes,

  • (if color only) swap setbackground(color.xxx) betweens 2 jpanels

  • puzzle-like game images, puzzle or minesweaper about, (not clear ...)

    1. put images icon/imageicons jlabel instead of jpanel , on mouse events switch (seticon()) icons betweens jlabels, load icons local variables

    2. (easiest of ways) jtogglebutton , icon, there logic similair jlabel, showing icon on setpressedicon()


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -