1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java - 15 Puzzle

  1. Nov 17, 2011 #1
    1. The problem statement, all variables and given/known data
    My class is required to write the Java source code for a 15 Puzzle program as an end of the year assignment. http://en.wikipedia.org/wiki/15_puzzle" [Broken] is a link to what the 15 Puzzle is, for those who aren't familiar with its name.

    The assignment is split into two parts; setting up the visual part of the puzzle and setting up the actions/events of the puzzle.

    Currently, I'm in the first half of the puzzle. The second half will be assigned at a later date, and I will most likely end up having to post again for help :P

    2. Relevant equations
    This project requires "for" loops, and I was also told that "if" statements aren't allowed.

    How much these restrictions will inhibit anyone's ability to find a helpful solution, I don't know. I myself am not familiar with "if" statements since my professor has yet to cover that part of the course.

    3. The attempt at a solution
    All current code:

    NOTE: The first two sections of code was code that was given to us already to start us off; these aren't to be altered. The last section of code is what I have been writing.

    Code (Text):

    package code;

    public class Driver {
        public static void main(String[] args) {
            new FifteenPuzzle("images/butterflySmall.png");
        }
    }
     
    Code (Text):

    package support;

    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;

    import javax.imageio.ImageIO;

    public class HelpfulImageMethods {
       
       
        /**
         * @param filePath
         * @return The BufferedImage object created from the data in the file
         *         located at filePath; if no image data can be loaded, null
         *         is returned.
         */
        public static BufferedImage loadImage(String filePath) {
            BufferedImage img = null;
            try {
                img = ImageIO.read(new File(filePath));
            }
            catch (IOException e) {
                System.err.println("I could not load the file \'"+filePath+"'.  Sorry.");
            }
            return img;
        }

        /**
         * @param img
         * @param sx
         * @param sy
         * @param imageWidth
         * @param imageHeight
         * @return a BufferedImage object which is cut out from the BufferedImage
         *         object 'img'.  The returned image is the sub-image of 'img' whose
         *         upper-left corner is at (sx,sy) and whose width is imageWidth,
         *         and whose height is imageHeight.
         */
        public static BufferedImage createSubImage(BufferedImage img, int sx, int sy, int imageWidth,
                int imageHeight) {
            BufferedImage subImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
            Graphics g = subImage.getGraphics();
            int dx = 0;
            int dy = 0;
            g.drawImage(img,dx, dy, dx+imageWidth, dy+imageHeight,
                            sx, sy, sx+imageWidth, sy+imageHeight,
                            null);
            g.dispose();
            return subImage;
        }
    }
     

    This last section of code is the code that the student is supposed to write himself/herself, ie. the problem lies in this section.

    Code (Text):

    package code;

    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import support.HelpfulImageMethods;


    public class FifteenPuzzle {           
       
        private JFrame _window;
        private BufferedImage _wholeImage;
       
        public FifteenPuzzle(String filePath) {
            _wholeImage = HelpfulImageMethods.loadImage(filePath);
            _window = new JFrame("15 Puzzle");
           
            Container p = _window.getContentPane();
            p.setLayout(null);
            p.setPreferredSize(new Dimension(300,300));
           
            this.subImageAdder();
           
            _window.pack();
            _window.setVisible(true);
            _window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
       
        public List<BufferedImage> subImageCreator(){
            List<BufferedImage> list = new LinkedList<BufferedImage>();
            int subImageWidth = _wholeImage.getWidth()/4;
            int subImageHeight = _wholeImage.getHeight()/4;
           
            for (int col = 0; col < 4; col = col+1){
                BufferedImage rowImages = HelpfulImageMethods.createSubImage(_wholeImage, 0, col*subImageHeight, subImageWidth, subImageHeight);
                list.add(rowImages);
            }
           
            for (int row = 0; row < 4; row = row+1){
                BufferedImage columnImages = HelpfulImageMethods.createSubImage(_wholeImage, row*subImageWidth, 0, subImageWidth, subImageHeight);
                list.add(columnImages);
            }
           
            return list;
        }
       
        public void subImageAdder(){
            Iterator<BufferedImage> i = this.subImageCreator().iterator();
            int subImageWidth = _wholeImage.getWidth()/4;
            int subImageHeight = _wholeImage.getHeight()/4;
           
            for (int col = 0; col < 4; col = col+1){
                ImageIcon icon = new ImageIcon(i.next());
                JLabel label = new JLabel(icon);
                label.setBounds(2, col*(2+subImageHeight), subImageWidth, subImageHeight);
                _window.add(label);
            }
           
            for (int row = 0; row < 4; row = row+1){
                ImageIcon icon = new ImageIcon(i.next());
                JLabel label = new JLabel(icon);
                label.setBounds(row*(2+subImageWidth), 2, subImageWidth, subImageHeight);
                _window.add(label);
            }
        }
       
    }
     
    Here is a snapshot of what my current code results in:

    15Puzzle.jpg

    So as you can see, the code's not doing what I need it to just yet. This is where I have been stumped for some time.

    Also, we need to have each sub-image of the whole image 2 pixels apart from neighbouring sub-images. This explains the small spaces in-between the sub-images in the picture, except for some reason the spaces don't appear around the edges of the first (top-left most) sub-image.

    Any advice or help would be greatly appreciated.
     
    Last edited by a moderator: May 5, 2017
  2. jcsd
  3. Nov 30, 2011 #2

    Mark44

    Staff: Mentor

    In your code, you have two separate for loops, one for rows and one for columns, in your subimageCreator and subimageAdder methods.

    The way I would approach this is to use an inner for loop nested inside an outer for loop, like this:
    Code (Text):

    for(int row = 0; row < 4; row++)
    {
      for(int col = 0; col < 4; col++)
      {
        // create subimage for window at position (row, col)
        // add subimage at position (row, col)
      }
    }
     
  4. Dec 3, 2011 #3
    Yes, thank you. That's what I ended up doing for the first part. Now comes the real tricky, second part of the assignment.

    For this next part we have to write the code so that the following things occur:

    1.) On a click of a button, the puzzle starts a new game*. This entails getting the picture, "slicing it up", shuffling it, and then putting it all onto the game board (JPanel/JFrame).

    *If the button is clicked continuously, the puzzle must keep shuffling/scrambling the pieces so that the puzzle keeps resetting, starting a new game.

    2.) After that's done, we have to set it up so that any tile adjacent/next-to the empty/blank tile will switch spots with the empty space, on clicking that tile.


    So far I've completed part of 1.), my only problem being that if I keep clicking the button, the same set of shuffled tiles show. In other words, if I keep clicking the "Start New Game" button the puzzle just stays on the same arrangement of tiles as if I had only clicked on the button once.


    Here's the code that I've written so far:

    "Driver" Class - Starts the game.

    Code (Text):
    package code;

    public class Driver {
        public static void main(String[] args) {
            new Game("images/butterflySmall.png");
        }
    }
     


    "FifteenPuzzle" Class - From the first part of the assignment, has a method that "slices up" the provided image into tiles and another method that adds the tiles onto a JPanel.

    Code (Text):
    package code;

    import java.awt.image.BufferedImage;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import support.HelpfulImageMethods;

    public class FifteenPuzzle {           
       
        private JPanel _puzzlePanel;
        private BufferedImage _wholeImage;
       
        public FifteenPuzzle(String filePath, JPanel puzzlePanel) {
            _wholeImage = HelpfulImageMethods.loadImage(filePath);
            _puzzlePanel = puzzlePanel;
            _puzzlePanel.setLayout(null);
        }
       
        public List<BufferedImage> subImageCreator() {
            List<BufferedImage> list = new LinkedList<BufferedImage>();
            int subImageWidth = _wholeImage.getWidth()/4;
            int subImageHeight = _wholeImage.getHeight()/4;
           
            for (int col = 0; col < 4; col = col+1){
                for (int row = 0; row < 4; row = row + 1) {
                    BufferedImage subImages = HelpfulImageMethods.createSubImage(_wholeImage, row*subImageWidth, col*subImageHeight, subImageWidth, subImageHeight);
                    list.add(subImages);
                }
            }
           
            return list;
        }
       
        public void subImageAdder(Iterator<BufferedImage> i) {
            int subImageWidth = _wholeImage.getWidth()/4;
            int subImageHeight = _wholeImage.getHeight()/4;
           
            for (int col = 0; col < 4; col = col+1){
                for (int row = 0; row < 4; row = row+1) {
                    ImageIcon icon = new ImageIcon(i.next());
                    JLabel label = new JLabel(icon);
                    label.setBounds(2+(row*(2+subImageWidth)) , 2+(col*(2+subImageHeight)) , subImageWidth, subImageHeight);
                    _puzzlePanel.add(label);
                }
            }
        }
       
        public int getWidth() {
            return _wholeImage.getWidth();
        }
       
        public int getHeight() {
            return _wholeImage.getHeight();
        }
       
    }
     


    "Game" Class - Instantiates and sets up graphical components of the game. (JFrame, JPanels, JButtons, ActionListeners, etc.)

    Code (Text):
    package code;

    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class Game {
       
        public Game(String filePath) {
            JFrame window = new JFrame("15 Puzzle");
            JPanel puzzlePanel = new JPanel();
            FifteenPuzzle puzzle = new FifteenPuzzle(filePath, puzzlePanel);
            JPanel buttonPanel = new JPanel();
           
            JButton startButton = new JButton("Start New Game");
            JButton quitButton = new JButton("Quit");
           
            window.getContentPane().setLayout(new BoxLayout(window.getContentPane(), BoxLayout.Y_AXIS));

            StartActionListener sal = new StartActionListener(puzzle, puzzlePanel, window, buttonPanel);
            QuitActionListener qal = new QuitActionListener();
           
            startButton.addActionListener(sal);
            quitButton.addActionListener(qal);
           
            buttonPanel.add(startButton);
            buttonPanel.add(quitButton);
           
            window.add(buttonPanel);
            window.add(puzzlePanel);
           
            window.setVisible(true);
            window.pack();
           
            puzzlePanel.setVisible(false);
        }
    }
     


    "StartActionListener" Class - ActionListener for "Start New Game" button. *(I think this is where my "resetting problem" might lie.)*

    Code (Text):
    package code;

    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class StartActionListener implements ActionListener {

        private FifteenPuzzle _puzzle;
        private JPanel _puzzlePanel;
        private JFrame _window;
       
        public StartActionListener(FifteenPuzzle puzzle, JPanel puzzlePanel, JFrame window, JPanel buttonPanel) {
            _puzzle = puzzle;
            _puzzlePanel = puzzlePanel;
            _window = window;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            List<BufferedImage> list = _puzzle.subImageCreator();
            Iterator<BufferedImage> i = list.iterator();
           
            list.remove(list.size()-1);
            Collections.shuffle(list);
           
            _puzzle.subImageAdder(i);
            _puzzlePanel.setVisible(true);
            _puzzlePanel.setPreferredSize(new Dimension(_puzzle.getWidth(),_puzzle.getHeight()));
           
            _window.pack();
        }
    }
     


    "QuitActionListener" Class - ActionListener for the "Quit" button.

    Code (Text):
    package code;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class QuitActionListener implements ActionListener {

        public QuitActionListener() {
           
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }

    }
     


    I hope that lays out the requirements of the assignment clearly.

    Any help would greatly be appreciated! Thanks!
     
  5. Dec 4, 2011 #4

    Mark44

    Staff: Mentor

    I would suggest that you get familiar with a debugger so you can test your theory that your problem is in your StartActionListener class.
     
  6. Dec 8, 2011 #5
    Okay I've got the tiles moving the way I need them to. Now I just have to figure out how to get the game to keep switching up the order of the tile arrangement each time I click the "Start New Game" button.

    Here's all of my edited code:

    Game: All the GUI stuff, now only instantiates buttonPanel instead of both buttonPanel and puzzlePanel. Also, no FifteenPuzzle object is created on instantiation.

    Code (Text):
    package code;

    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class Game {
       
        public Game(String filePath) {
            JFrame window = new JFrame("15 Puzzle");
            JPanel buttonPanel = new JPanel();
           
            JButton startButton = new JButton("Start New Game");
            JButton quitButton = new JButton("Quit");
           
            StartActionListener sal = new StartActionListener(window, filePath);
            QuitActionListener qal = new QuitActionListener();
           
            window.getContentPane().setLayout(new BoxLayout(window.getContentPane(), BoxLayout.Y_AXIS));
           
            startButton.addActionListener(sal);
            quitButton.addActionListener(qal);
           
            buttonPanel.add(startButton);
            buttonPanel.add(quitButton);
           
            window.add(buttonPanel);
           
            window.setVisible(true);
            window.pack();
        }
    }


    StartActionListener: Now creates and handles its own FifteenPuzzle object.

    Code (Text):
    package code;

    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class StartActionListener implements ActionListener {

        private FifteenPuzzle _puzzle;
        private JFrame _window;
        private JPanel _puzzlePanel;
       
        public StartActionListener(JFrame window, String filePath) {
            FifteenPuzzle puzzle = new FifteenPuzzle(filePath);
            _puzzle = puzzle;
            _window = window;
        }
       
        @Override
        public void actionPerformed(ActionEvent e) {
            JPanel puzzlePanel = new JPanel();
            _puzzlePanel = puzzlePanel;
            _puzzle.setPanel(puzzlePanel);
            puzzlePanel.setLayout(null);

            _puzzle.subImageCreator();
            _puzzle.subImageAdder();
           
            BlankSpaceHolder bsh = new BlankSpaceHolder();
            int subImageWidth = _puzzle.getWidth()/4;
            int subImageHeight = _puzzle.getHeight()/4;
            bsh.setX(3*(subImageWidth+2));
            bsh.setY(3*(subImageHeight+2));
           
            TileListener tl = new TileListener(_puzzlePanel, bsh, _puzzle);
            _puzzlePanel.addMouseListener(tl);
           
            _puzzlePanel.setPreferredSize(new Dimension(_puzzle.getWidth(), _puzzle.getHeight()));
           
            _window.add(_puzzlePanel);
            _window.pack();
           
        }
    }


    FifteenPuzzle:

    Code (Text):
    package code;

    import java.awt.image.BufferedImage;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;

    import support.HelpfulImageMethods;

    public class FifteenPuzzle {           
       
        private BufferedImage _wholeImage;
        private JPanel _puzzlePanel;
       
        public FifteenPuzzle(String filePath) {
            _wholeImage = HelpfulImageMethods.loadImage(filePath);
        }
       
        public List<BufferedImage> subImageCreator() {
            List<BufferedImage> list = new LinkedList<BufferedImage>();
            int subImageWidth = _wholeImage.getWidth()/4;
            int subImageHeight = _wholeImage.getHeight()/4;
           
            for (int col = 0; col < 4; col++){
                for (int row = 0; row < 4; row++) {
                    BufferedImage image = HelpfulImageMethods.createSubImage(_wholeImage, row*subImageWidth, col*subImageHeight, subImageWidth, subImageHeight);
                    list.add(image);
                }
            }
           
            list.remove(list.size() - 1);
            Collections.shuffle(list);
            return list;
        }
       
        public void subImageAdder() {
            Iterator<BufferedImage> i = this.subImageCreator().iterator();
            int subImageWidth = _wholeImage.getWidth()/4;
            int subImageHeight = _wholeImage.getHeight()/4;
           
            for (int col = 0; col < 4; col++){
                for (int row = 0; row < 4; row++) {
                    if (i.hasNext()) {
                        ImageIcon icon = new ImageIcon(i.next());
                        Tile tile = new Tile(icon);
                        tile.setBounds(row*(2+subImageWidth), col*(2+subImageHeight), subImageWidth, subImageHeight);
                        _puzzlePanel.add(tile);
                    }
                }
            }
        }
       
        public void setPanel(JPanel panel) {
            _puzzlePanel = panel;
        }
       
        public JPanel getPanel() {
            return _puzzlePanel;
        }
       
        public int getWidth() {
            int imageWidth = _wholeImage.getWidth();
            return imageWidth;
        }
       
        public int getHeight() {
            int imageHeight = _wholeImage.getHeight();
            return imageHeight;
        }
    }
     


    BlankSpaceHolder: Holds the x and y int values that locate the blank space.

    Code (Text):
    package code;

    public class BlankSpaceHolder {

        private int _x;
        private int _y;
       
        public void setX(int x) {
            _x = x;
        }
       
        public void setY(int y) {
            _y = y;
        }
       
        public int getX() {
            return _x;
        }
       
        public int getY() {
            return _y;
        }
    }
     


    Tile: Extends JLabel class, thus handling its own image piece of the BufferedImage file.

    Code (Text):
    package code;

    import javax.swing.ImageIcon;
    import javax.swing.JLabel;

    public class Tile extends JLabel {

        private static final long serialVersionUID = 1L;

        public Tile(ImageIcon icon) {
            super(icon);
        }

    }
     


    TileListener: Handles the movement of the tiles. There shouldn't be any problems in this code, but just including it as well in case you're curious :P

    Code (Text):
    package code;

    import java.awt.Component;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;

    import javax.swing.JPanel;

    public class TileListener implements MouseListener {
       
        private JPanel _panel;
        private BlankSpaceHolder _bsh;
        private FifteenPuzzle _puzzle;
       
        public TileListener (JPanel panel, BlankSpaceHolder bsh, FifteenPuzzle puzzle) {
            _panel = panel;
            _bsh = bsh;
            _puzzle = puzzle;
        }
       
        @Override
        public void mouseClicked(MouseEvent e) {
            Point pointClicked = e.getPoint();
            Component tile = _panel.getComponentAt(pointClicked);
            Point tilePoint = tile.getLocation();
           
            int x = tilePoint.x;
            int y = tilePoint.y;
           
            int bx = _bsh.getX();
            int by = _bsh.getY();
           
            int xCup;
            int yCup;
           
            int subImageWidth = _puzzle.getWidth()/4;
            int subImageHeight = _puzzle.getHeight()/4;
           
            if ((x - bx == subImageWidth + 2) && (y - by == 0)) {
                xCup = x;
                tile.setLocation(bx,by);
                _bsh.setX(xCup);
            }
           
            else if ((bx - x == subImageWidth + 2) && (y - by == 0)) {
                xCup = x;
                tile.setLocation(bx, by);
                _bsh.setX(xCup);
            }
           
            else if ((y - by == subImageHeight + 2) && (x - bx == 0)) {
                yCup = y;
                tile.setLocation(bx, by);
                _bsh.setY(yCup);
            }
           
            else if ((by - y == subImageHeight + 2) && (x - bx == 0)) {
                yCup = y;
                tile.setLocation(bx, by);
                _bsh.setY(yCup);
            }
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
           
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
           
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
           
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
           
        }
    }
     
     
  7. Dec 9, 2011 #6

    Mark44

    Staff: Mentor

    I think this should be pretty easy. As I understand your code, you read image pieces from a file and store them in a linked list. This happens in one method on the FifteenPuzzle class, subImageCreator. Before this method returns, it calls a static method on the Collections class, shuffle, to shuffle the items in the linked list. I believe you could use this to create a rearranged puzzle when someone clicks Start New Game. Hope that helps!
     
  8. Dec 9, 2011 #7
    But wouldn't that just shuffle and rearrange all the pieces? I need to have the Start Button do that and have all the pieces put onto the JFrame with the ButtonPanel still showing.
     
  9. Dec 9, 2011 #8

    Mark44

    Staff: Mentor

    Maybe I misunderstood, but shuffling and rearranging when the user wants to play another round was what I thought you wanted to do.

    What exactly is the problem you're trying to solve? What you're asking isn't clear to me.

    BTW, it looks like you have done a very nice job on this programming problem.
     
  10. Dec 9, 2011 #9
    I actually JUST got the assignment finished today, almost right after I posted that :smile:

    Initially the problem was that repeatedly clicking the "Start New Game" button would just keep adding JPanels to the JFrame, without first removing the old/initial puzzlePanel.

    I solved this problem by revising my Game, StartActionListener, and FifteenPuzzle classes.

    First I added a new method to the FifteenPuzzle class, called setPanel(JPanel panel): (Re)Assigns the JPanel instance variable that the FifteenPuzzle holds (_puzzlePanel).

    Code (Text):

    public void setPanel(JPanel panel) {
    [INDENT]_puzzlePanel = panel;[/INDENT]
    }
     
    Then I modified the Game class so that:

    1.) It instantiates a "dummy" JPanel that the StartActionListener class could take in as an argument.

    and

    2.) It doesn't instantiate a new FifteenPuzzle object.

    Code (Text):
    package code;

    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.util.Collections;
    import java.util.List;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class StartActionListener implements ActionListener {

        private JFrame _window;
        private String _filePath;
        private JPanel _puzzlePanel;
       
        public StartActionListener(JFrame window, String filePath, JPanel panel) {
            _window = window;
            _puzzlePanel = panel;
            _filePath = filePath;
        }
       
        @Override
        public void actionPerformed(ActionEvent e) {
            _window.remove(_puzzlePanel);
            JPanel puzzlePanel = new JPanel();
            _puzzlePanel = puzzlePanel;
           
            FifteenPuzzle puzzle = new FifteenPuzzle(_filePath);
            puzzle.setPanel(_puzzlePanel);
            _puzzlePanel.setLayout(null);
           
            List<BufferedImage> list = puzzle.subImageCreator();
            list.remove(list.size() - 1);
            Collections.shuffle(list);
            puzzle.subImageAdder(list);
           
            BlankSpaceHolder bsh = new BlankSpaceHolder();
            int subImageWidth = puzzle.getWidth()/4;
            int subImageHeight = puzzle.getHeight()/4;
            bsh.setX(3*(subImageWidth+2));
            bsh.setY(3*(subImageHeight+2));
           
            TileListener tl = new TileListener(_puzzlePanel, bsh, puzzle);
            _puzzlePanel.addMouseListener(tl);
           
            _puzzlePanel.setPreferredSize(new Dimension(puzzle.getWidth(), puzzle.getHeight()));
           
            _window.add(_puzzlePanel);
            _window.pack();
           
        }
    }
    Thus by clicking the "Start New Game" button the window would remove the "dummy" puzzlePanel, that was instantiated in the Game class, from the window object and replace it with a new JPanel that's instantiated upon click.

    Clicked repeatedly, the StartActionListener would just remove from the window the object whose reference is assigned to the _puzzlePanel instance variable, ie. the last added JPanel. (Since the last added JPanel's reference is assigned to the StartActionListener's _puzzlePanel instance variable, clicking the button multiple times will just keep reassigning _puzzlePanel to new JPanel instances.)

    I hope that explains my method :P

    Thanks! :smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java - 15 Puzzle
  1. String in java (Replies: 7)

  2. Java program (Replies: 3)

  3. Java prob. (Replies: 1)

  4. HeapSort Java (Replies: 3)

Loading...