swing - Sliding Puzzle Java Shuffle issue -
i trying shuffle pieces 15 piece sliding puzzle when click shuffle button. unfortunately, don't know how "grab" jpanel buttons because dynamic , don't "passed" action listener: here's loop creates & adds buttons:
pos = new int[][] { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15} }; centerpanel = new jpanel(); centerpanel.setlayout(new gridlayout(4, 5, 0, 0)); add(box.createrigidarea(new dimension(0, 5)), borderlayout.north); add(centerpanel, borderlayout.center); int counter = 0; ( int = 0; < 4; i++) { ( int j = 0; j < 4; j++) { if ( j == 3 && == 3) { label = new jlabel(""); centerpanel.add(label); // empty label } else { counter++; button = new jbutton(); button.settext(string.valueof(counter)); button.addactionlistener(this); centerpanel.add(button); } } }
then add shuffle button:
southpanel = new jpanel(new flowlayout()); jbutton shufflebutton = new jbutton("shuffle"); southpanel.add(shufflebutton); shufflebutton.addactionlistener(this);
then shuffle code i have 4 random moves have far (and know awful , don't know better). moves label pushes buttons down in order :(
public void actionperformed(actionevent e) { //get empty label location int labelx = label.getx(); int labely = label.gety(); int labelposx = labelx / sizelabel.width; int labelposy = labely / sizelabel.height; int labelindex = pos[labelposx][labelposy]; // button location buttonx = label.getx() - size.width; buttony = label.gety(); // button position buttonposx = buttonx / size.width; buttonposy = buttony / size.height; buttonindex = pos[buttonposy][buttonposx]; // if label can moved if ((labely == buttony && (labelx - buttonx) == size.width )) { system.out.println("label moves left"); labelindex = buttonindex + 1; centerpanel.add(label,buttonindex); // here need "grab button" , put // in label index ??? labely = label.gety(); labelx = label.getx(); }
any appreciated!! tried using component getcomponent(int) don't know how implement it. think perfect...
rely more on maintaining kind of reference jlabel
s trying maintain information labels carry.
for example, place jlabel
s list
, shuffle list, removing labels , adding them again.
import java.awt.borderlayout; import java.awt.dimension; import java.awt.eventqueue; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.arraylist; import java.util.collections; import java.util.list; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class shuffle { public static void main(string[] args) { new shuffle(); } public shuffle() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } final puzzelpane puzzelpane = new puzzelpane(); jbutton shuffle = new jbutton("shuffel"); shuffle.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { puzzelpane.shuffle(); } }); jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(puzzelpane); frame.add(shuffle, borderlayout.south); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class puzzelpane extends jpanel { private list<jlabel> labels; public puzzelpane() { setlayout(new gridlayout(3, 3)); labels = new arraylist<>(9); (int index = 0; index < 9; index++) { jlabel label = new jlabel(string.valueof(index)); label.sethorizontalalignment(jlabel.center); labels.add(label); } shuffle(); } @override public dimension getpreferredsize() { return new dimension(200, 200); } public void shuffle() { removeall(); collections.shuffle(labels); (jlabel label : labels) { add(label); } revalidate(); } } }
you use arrays same idea...
Comments
Post a Comment