Advertisement
jdalbey

Kaboom Swing GUI skeleton

Jun 17th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.55 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.table.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6. import java.text.*;
  7.  
  8. /**  Skeleton for Kaboom.
  9.  *   GUI has a menu bar, a status area, and a 2d playing area.
  10.  *   The GUI will display the game and handle user interaction.
  11.  * @author J. Dalbey
  12.  * @version 6/15/2012
  13. */
  14. public class Kaboom extends JFrame implements ActionListener
  15. {
  16.     /* Main components of the GUI */
  17.     // DO NOT CHANGE ANY OF THE GUI COMPONENT DECLARATIONS IN THIS SECTION
  18.     private String[] columns = {"", "", "", "", "", "", "", "", "", "", };
  19.     private JTable table;
  20.     private JMenuBar menuBar;
  21.     private JMenu mnuGame;
  22.     private JMenuItem[] mnuItems;    
  23.     private JLabel lblStatus = new JLabel();
  24.     private ImageIcon background;
  25.  
  26.     /* The game board */
  27.     private Object[][] myBoard;
  28.  
  29.     /* Square dimensions in pixels */
  30.     private static final int kTileWidth = 58;
  31.     private static final int kTileHeight = 78;
  32.     /* Number of rows and columns in the square Board */
  33.     private static final int kBoardWidth = 10;
  34.  
  35.    
  36.     /** Create a GUI.
  37.      * Will use the System Look and Feel when possible.
  38.      */
  39.     public Kaboom()
  40.     {
  41.         super();
  42.         try
  43.         {
  44.             UIManager.setLookAndFeel(
  45.                 UIManager.getSystemLookAndFeelClassName());
  46.         }
  47.         catch (Exception ex)
  48.         {
  49.             System.err.println(ex);
  50.         }
  51.  
  52.     }
  53.  
  54.  
  55.     /** Place all the Swing widgets in the frame of this GUI.
  56.      * @post the GUI is visible.  
  57.      */
  58.     public void layoutGUI()
  59.     {
  60.         loadImages();
  61.         table = new JTable(myBoard, columns)
  62.         {
  63.             public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
  64.             {
  65.                 Component c = super.prepareRenderer( renderer, row, column);
  66.                 // We want renderer component to be
  67.                 // transparent so background image is visible
  68.                 if ( c instanceof JComponent)
  69.                     ((JComponent)c).setOpaque(false);
  70.                 return c;
  71.             }
  72.  
  73.             // Override paint so as to show the table background
  74.             public void paint( Graphics g )
  75.             {
  76.                 // paint an image in the table background
  77.                 if (background != null)
  78.                 {
  79.                     g.drawImage( background.getImage(), 0, 0, null, null );
  80.                 }
  81.                 // Now let the paint do its usual work
  82.                 super.paint(g);
  83.             }
  84.            
  85.             // Make the table cells not editable
  86.             public boolean isCellEditable(int row,int column)
  87.             {  
  88.                 return false;  
  89.             }              
  90.         }
  91.         ; // end table def
  92.        
  93.         TableColumn column = null;
  94.         // Does the board exist?
  95.         if (myBoard != null)
  96.         {
  97.             // Set the dimensions for each column in the board to match the image */
  98.             for (int index = 0; index < kBoardWidth; index++)
  99.             {
  100.                 column = table.getColumnModel().getColumn(index);
  101.                 column.setMaxWidth(kTileWidth);
  102.                 column.setMinWidth(kTileWidth);
  103.             }
  104.         }
  105.  
  106.         // Define the layout manager that will control order of components
  107.         getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
  108.        
  109.         // Create the menu options
  110.         layoutMenus();
  111.        
  112.         // Create a panel for the status information
  113.         JPanel statusPane = new JPanel();
  114.         statusPane.add(lblStatus);
  115.         lblStatus.setName("Status");
  116.         statusPane.setAlignmentX(Component.CENTER_ALIGNMENT);
  117.         getContentPane().add(statusPane);
  118.  
  119.         // Define the characteristics of the table that shows the game board        
  120.         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  121.         table.setCellSelectionEnabled(false);
  122.         table.setRowHeight(kTileHeight);
  123.         table.setOpaque(false);
  124.         table.setShowGrid(false);
  125.         table.setAlignmentX(Component.CENTER_ALIGNMENT);
  126.         getContentPane().add(table);
  127.  
  128.         // Define the mouse listener that will handle player's clicks.
  129.         table.addMouseListener(myMouseListener);
  130.        
  131.         // And handle window closing events
  132.         addWindowListener(new WindowAdapter()
  133.         {
  134.             public void windowClosing(WindowEvent e)
  135.             {
  136.                 System.exit(0);
  137.             }
  138.         }
  139.         );
  140.  
  141.     } // end layout
  142.    
  143.     private void layoutMenus()
  144.     {
  145.         // Add a menubar
  146.         menuBar = new javax.swing.JMenuBar();
  147.         mnuGame = new JMenu("Game");    
  148.         menuBar.add(mnuGame);
  149.         mnuItems = new JMenuItem[9];  // allocate space for 9 menu items      
  150.        
  151.         // Create the Restart menu item
  152.         mnuItems[0] = new JMenuItem("Restart");
  153.         mnuItems[0].setAccelerator(KeyStroke.getKeyStroke('R', ActionEvent.ALT_MASK));
  154.         mnuItems[0].addActionListener(this);
  155.         mnuGame.add(mnuItems[0]);
  156.  
  157.         setJMenuBar(menuBar);   // tell the frame which menu bar to use
  158.     }
  159.  
  160.     /* Listener to respond to mouse clicks on the table */
  161.     private MouseAdapter myMouseListener = new MouseAdapter()
  162.     {
  163.         public void mouseReleased(MouseEvent ev)
  164.         {
  165.             int col = table.getSelectedColumn();
  166.             int row = table.getSelectedRow();
  167.             // call methods to handle player's click
  168.             repaint();
  169.         }
  170.     };
  171.    
  172.     protected void loadImages()
  173.     {
  174.         // load background image
  175.         background = new ImageIcon(
  176.                     Toolkit.getDefaultToolkit().getImage(
  177.                         this.getClass().getResource("img/" + "bkgd.jpg")));
  178.         // Load tile images here
  179.     }
  180.  
  181.    
  182.     /** Handle button clicks
  183.      * @param e The result of what was clicked
  184.      */
  185.     public void actionPerformed(ActionEvent e)
  186.     {
  187.         // Does the user want to restart the current game?
  188.         if ("Restart".equals(e.getActionCommand()))
  189.         {
  190.             // call restart method
  191.         }
  192.         repaint();
  193.     }
  194.    
  195.     // Local main to launch the GUI
  196.     public static void main(String[] args)
  197.     {
  198.         // Create the GUI
  199.         Kaboom frame = new Kaboom();
  200.  
  201.         frame.layoutGUI();   // do the layout of widgets
  202.                
  203.         // Make the GUI visible and available for user interaction
  204.         frame.pack();
  205.         frame.setVisible(true);
  206.     }
  207.    
  208. }  // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement