jdalbey

GUIskeleton

Jan 12th, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.73 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.table.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6.  
  7. /**  Skeleton GUI for grid-based games.
  8.  *   GUI has a menu, a status area, and a 2d playing area.
  9.  *   The GUI will display the game and handle mouse clicks, dispatching
  10.  *   them to the appropriate button or cell of the board.
  11. * @author JD
  12. * @version 2014.1.12
  13. */
  14. public class GUIskeleton extends JFrame
  15. {
  16.  
  17.     /* Main components of the GUI */
  18.     private Object[][] myBoard;
  19.     private String[] columns = {"","","","","","","","","","",};
  20.     private JLabel myStatus=null;
  21.     private JMenuBar menuBar;
  22.     private JTable table;
  23.     private int clickCount;
  24.     /* Square dimensions */
  25.     private int TileWidth = 60;
  26.     private int TileHeight = 30;
  27.    
  28.  
  29.     /** Create a GUI.
  30.      * Will use the System Look and Feel when possible.
  31.      */
  32.     public GUIskeleton()
  33.     {
  34.         super();
  35.         myBoard = new Integer[10][10];
  36.         try
  37.         {
  38.             UIManager.setLookAndFeel(
  39.                 UIManager.getSystemLookAndFeelClassName());
  40.         }
  41.         catch (Exception ex)
  42.         {
  43.             System.err.println(ex);
  44.         }
  45.     }
  46.  
  47.  
  48.     /** Place all the Swing widgets in the frame of this GUI.
  49.      * @post the GUI is visible.  
  50.      */
  51.     public void layoutGUI()
  52.     {
  53.         loadImages();
  54.         newGame();
  55.         table = new JTable(myBoard,columns);
  56.        
  57.         TableColumn column = null;
  58.         if (myBoard != null)
  59.         {
  60.             // Set the dimensions for each column in the board to match the image */
  61.             for (int i = 0; i < 10; i++)
  62.             {
  63.                 column = table.getColumnModel().getColumn(i);
  64.                 column.setMaxWidth(TileWidth);
  65.                 column.setMinWidth(TileWidth);
  66.             }
  67.         }
  68.         table.setDefaultEditor(Object.class, null);  // remove editor makes table not editable
  69.  
  70.         // Define the layout manager that will control order of components
  71.         getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
  72.        
  73.         // Add a menubar
  74.         menuBar = new javax.swing.JMenuBar();
  75.         JMenu mnuGame = new JMenu("Game");
  76.         menuBar.add(mnuGame);
  77.         JMenuItem mnuNew = new JMenuItem("New");
  78.         mnuNew.setMnemonic('N');
  79.         mnuNew.setAccelerator(
  80.                 KeyStroke.getKeyStroke('N', ActionEvent.ALT_MASK));
  81.         mnuNew.addActionListener(new ActionListener()
  82.         {
  83.             public void actionPerformed(ActionEvent e)
  84.             {
  85.                 newGame();
  86.                 repaint();                
  87.             }
  88.         });
  89.         mnuGame.add(mnuNew);
  90.        
  91.         JMenuItem mnuQuit = new JMenuItem("Quit");
  92.         mnuQuit.setMnemonic('Q');
  93.         mnuQuit.setAccelerator(
  94.                 KeyStroke.getKeyStroke('Q', ActionEvent.ALT_MASK));
  95.         mnuQuit.addActionListener(new ActionListener()
  96.         {
  97.             public void actionPerformed(ActionEvent e)
  98.             {
  99.                 dispose();
  100.             }
  101.         });
  102.         mnuGame.add(mnuQuit);
  103.         setJMenuBar(menuBar);        
  104.        
  105.         // Create a panel for the status information
  106.         JPanel statusPane = new JPanel();
  107.         myStatus = new JLabel("###");
  108.         statusPane.add(myStatus);
  109.         statusPane.setAlignmentX(Component.CENTER_ALIGNMENT);
  110.         getContentPane().add(statusPane);
  111.  
  112.         // Define the characteristics of the table that shows the game board        
  113.         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  114.         table.setCellSelectionEnabled(false);
  115.         table.setRowHeight(TileHeight);
  116.  
  117.         table.setOpaque(false);
  118.         table.setShowGrid(false);
  119.  
  120.         table.setAlignmentX(Component.CENTER_ALIGNMENT);
  121.         getContentPane().add(table);
  122.  
  123.         // Define the mouse listener that will handle player's clicks.
  124.         table.addMouseListener(new MouseAdapter()
  125.         {
  126.             public void mouseReleased(MouseEvent ev)
  127.             {
  128.                 int col = table.getSelectedColumn();
  129.                 int row = table.getSelectedRow();
  130.                 // Is it a right mouse click?
  131.                 if (SwingUtilities.isRightMouseButton(ev))
  132.                 {
  133.                     row = (int) (ev.getPoint().getY()/TileHeight);
  134.                     col = (int) (ev.getPoint().getX()/TileWidth);
  135.                     // Turn the value negative
  136.                     myBoard[row][col] = -(Integer) myBoard[row][col];
  137.                 }
  138.                 else
  139.                 {
  140.                     // left mouse clicks turn the value to zero
  141.                     myBoard[row][col] = 0;
  142.                     clickCount++;
  143.                     myStatus.setText("" + clickCount);
  144.                 }
  145.                 repaint();
  146.             }
  147.         }
  148.         );
  149.         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  150.    
  151.  
  152.     } // end layout
  153.  
  154.    
  155.     protected void loadImages()
  156.     {
  157.         // Any images will be loaded here
  158.     }
  159.  
  160.     /** Start a new game by putting new values in the board */
  161.     private void newGame()
  162.     {
  163.         Integer cellvalue = new Integer((int)(Math.random()*10));
  164.         for (int i=0;i<10;i++)
  165.         {
  166.             for (int j=0;j<10;j++)
  167.             {
  168.                 myBoard[i][j] = cellvalue;
  169.             }
  170.         }
  171.         clickCount = 0;
  172.     }
  173.    
  174.     // Local main to launch the GUI
  175.     public static void main(String[] args)
  176.     {
  177.         // Create the GUI
  178.         GUIskeleton frame = new GUIskeleton();
  179.  
  180.         frame.layoutGUI();   // do the layout of widgets
  181.                
  182.         // Make the GUI visible and available for user interaction
  183.         frame.pack();
  184.         frame.setVisible(true);
  185.     }
  186. }  // end class
Advertisement
Add Comment
Please, Sign In to add comment