title: sliding puzzle code
body: <p>Our group project is to create a sliding puzzle game using Java Eclipse. We have the code to create a 3x3 grid with the same picture(its supposed to be one picture but split into 9 pieces but well learn that later on). My part is to create a mouse listener that clicks the selected 'tile' and then displays a message testing to see if the selected tile was clicked correctly. (Example: if user clicks tile 1, message displays "tile 1 is clicked") I think I will have to create individual mouse listeners for each tile. Any suggestions? </p>
  
  <p>Heres the code:</p>
  
  <pre><code>   import javax.swing.*;
     import javax.swing.event.*;
     import java.awt.*;
     import java.awt.event.*;
  
     public class Puzzle 
     {
  // Initialize the Frame for the Puzzle
  private JFrame frame = new JFrame("Puzzle");
  private JPanel puzzlePanel = new JPanel( null );
  private JLabel status;
  
  
  // Itemize Menu
  private JMenuBar menu = new JMenuBar();
  private JMenu menuFile = new JMenu("File");
  private JMenuItem menuFileNew = new JMenuItem("New Game");
  private JMenuItem menuFileQuit = new JMenuItem("Quit");
  private JMenu menuHelp = new JMenu("Help");
  private JMenuItem menuHelpAbout = new JMenuItem("About");
  
  // Variables
  // Easy(3) Medium(4) Hard(5)
  private int dimm = 3;
  private int spacing = 5;
  private int tileDimm = 96;
  
  // Constructor
  public Puzzle() 
  {
      status = new JLabel("Default");
      frame.add(status,BorderLayout.SOUTH);
  
  
      // Build Menu
      frame.setJMenuBar(menu);
      menuFile.add(menuFileNew);
      menuFile.add(menuFileQuit);
      menuHelp.add(menuHelpAbout);
      menu.add(menuFile);
      menu.add(menuHelp);
  
      // Panel
      puzzlePanel.setPreferredSize(new Dimension((dimm*tileDimm)+(spacing*(dimm+1)),(dimm*tileDimm)+(spacing*(dimm+1))) );
      frame.add(puzzlePanel);
  
      // Puzzle Logic
      PuzzleLogic puzzleLogic = new PuzzleLogic(dimm);
  
  
  
      Tile[][] tiles = puzzleLogic.createTileArray();
      puzzleLogic.shuffleTiles(tiles);
      for(int i = 0; i &lt; dimm; i++) 
      {
          for(int ii = 0; ii &lt; dimm; ii++) 
          {
              puzzlePanel.add(tiles[i][ii].getLabel());
              int x = i*tileDimm+(spacing*(i+1));
              int y = ii*tileDimm+(spacing*(ii+1));
              tiles[i][ii].getLabel().setBounds(x,y,tileDimm,tileDimm);
  
              System.out.print(tiles[i][ii].getContent() + " ");
          }
      }
  
      HandlerClass handler = new HandlerClass();
      puzzlePanel.addMouseListener(handler);
  }
  
  // create listener - inner class
  class HandlerClass implements MouseListener
  {
  
      public void mouseClicked(MouseEvent e)
      {
  
          status.setText(String.format("Tile 1 is clicked"));
      }
  
      @Override
      public void mouseEntered(MouseEvent e) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void mouseExited(MouseEvent e) 
      {
          status.setText(String.format(" "));
  
      }
  
      @Override
      public void mousePressed(MouseEvent e) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void mouseReleased(MouseEvent e) {
          // TODO Auto-generated method stub  
      }
  
  }
  
  public void launch() 
  {
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack(); //Adjusts panel to components for display
      frame.setVisible(true);
      frame.setResizable(false);
  }
  
  // Calls Everything to Action
  public static void main(String[] args) 
  {
      Puzzle puzzle = new Puzzle();
      puzzle.launch();
  }
  </code></pre>
  
  <p>}</p>