1. package com.rburgos.mastermindtestlayout;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.ArrayList;
  6.  
  7. import javax.swing.*;
  8. import javax.swing.border.*;
  9.  
  10. @SuppressWarnings("serial")
  11. public class MainLayout extends JFrame
  12. {
  13.     private JPanel mainPanel, topPanel, pegsLeftPanel, hintsRightPanel, bottomPanel;
  14.     private JMenuBar menuBar;
  15.     private JMenu gameMenu, helpMenu;
  16.     private JMenuItem easyModeItem, advModeItem, instrItem, aboutItem;
  17.    
  18.     private String[] colors = {"#eccc75", "#72688c", "#f6a01a", "#116348",
  19.             "#3b7e98", "#d53533", "#a36526", "#679317", "#ed9f9f", "#1be2b4"};
  20.     private ColorPeg peg1, peg2, peg3, peg4, peg5, peg6, peg7, peg8, peg9, peg10;
  21.     private ColorPeg tempColorPeg;
  22.     private DummyPeg tempDummyPeg;
  23.     private GridBagConstraints dummyPegsConstraints;
  24.    
  25.     private int col = 0, row = 0;
  26.     private ArrayList<ArrayList<? super JComponent>> initPegsArray = new ArrayList<>();
  27.     private ArrayList<ArrayList<? super JComponent>> guessPegsAray = new ArrayList<>();
  28.     private ArrayList<? super JComponent> answerPegsArray = new ArrayList<>();
  29.  
  30.     public static void main(String[] args)
  31.     {
  32.         try
  33.         {
  34.             UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
  35.         }
  36.         catch (Throwable e)
  37.         {
  38.             e.printStackTrace();
  39.         }
  40.        
  41.         EventQueue.invokeLater(new Runnable()
  42.         {
  43.             public void run()
  44.             {
  45.                 try
  46.                 {
  47.                     MainLayout frame = new MainLayout();
  48.                     frame.setLocationRelativeTo(null);
  49.                     frame.setVisible(true);
  50.                 }
  51.                 catch (Exception e)
  52.                 {
  53.                     e.printStackTrace();
  54.                 }
  55.             }
  56.         });
  57.     }
  58.  
  59.     /**
  60.      * Create the frame.
  61.      */
  62.     public MainLayout()
  63.     {
  64.         // Window
  65.         setResizable(false);
  66.         setTitle("MasterMind");
  67.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  68.         setBounds(100, 100, 500, 700);
  69.        
  70.         createMenu();
  71.        
  72.         // Main panel:
  73.         // Divided into "top" and "bottom" by topPanel and bottomPanel
  74.         mainPanel = new JPanel();
  75.         mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  76.         setContentPane(mainPanel);
  77.         GridBagLayout gbl_contentPane = new GridBagLayout();
  78.         gbl_contentPane.columnWidths = new int[]{0};
  79.         gbl_contentPane.rowHeights = new int[]{591, 0, 0};
  80.         gbl_contentPane.columnWeights = new double[]{1.0};
  81.         gbl_contentPane.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
  82.         mainPanel.setLayout(gbl_contentPane);
  83.        
  84.         topPanel = new JPanel();
  85.         GridBagConstraints gbc_topPanel = new GridBagConstraints();
  86.         gbc_topPanel.insets = new Insets(0, 0, 5, 0);
  87.         gbc_topPanel.fill = GridBagConstraints.BOTH;
  88.         gbc_topPanel.gridx = 0;
  89.         gbc_topPanel.gridy = 0;
  90.         mainPanel.add(topPanel, gbc_topPanel);
  91.         GridBagLayout gbl_topPanel = new GridBagLayout();
  92.         gbl_topPanel.columnWidths = new int[]{0, 0, 0};
  93.         gbl_topPanel.rowHeights = new int[]{0, 0};
  94.         gbl_topPanel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
  95.         gbl_topPanel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
  96.         topPanel.setLayout(gbl_topPanel);
  97.        
  98.         pegsLeftPanel = new JPanel();
  99.         pegsLeftPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
  100.         GridBagConstraints gbc_pegsLeftPanel = new GridBagConstraints();
  101.         gbc_pegsLeftPanel.anchor = GridBagConstraints.NORTH;
  102.         gbc_pegsLeftPanel.fill = GridBagConstraints.BOTH;
  103.         gbc_pegsLeftPanel.gridx = 0;
  104.         gbc_pegsLeftPanel.gridy = 0;
  105.         topPanel.add(pegsLeftPanel, gbc_pegsLeftPanel);
  106.         GridBagLayout gbl_pegsLeftPanel = new GridBagLayout();
  107.         gbl_pegsLeftPanel.columnWidths = new int[] {0};
  108.         gbl_pegsLeftPanel.rowHeights = new int[] {30};
  109.         gbl_pegsLeftPanel.columnWeights = new double[]{0.0};
  110.         gbl_pegsLeftPanel.rowWeights = new double[]{0.0};
  111.         pegsLeftPanel.setLayout(gbl_pegsLeftPanel);
  112.        
  113.         dummyPegsConstraints = new GridBagConstraints();
  114.         dummyPegsConstraints.insets = new Insets(2, 3, 2, 3);
  115.                
  116.         createDummyPegs();
  117.        
  118.         hintsRightPanel = new JPanel();
  119.         hintsRightPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
  120.         GridBagConstraints gbc_hintsRightPanel = new GridBagConstraints();
  121.         gbc_hintsRightPanel.fill = GridBagConstraints.BOTH;
  122.         gbc_hintsRightPanel.gridx = 1;
  123.         gbc_hintsRightPanel.gridy = 0;
  124.         topPanel.add(hintsRightPanel, gbc_hintsRightPanel);
  125.        
  126.         bottomPanel = new JPanel();
  127.         bottomPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
  128.         GridBagConstraints gbc_bottomPanel = new GridBagConstraints();
  129.         gbc_bottomPanel.fill = GridBagConstraints.BOTH;
  130.         gbc_bottomPanel.gridx = 0;
  131.         gbc_bottomPanel.gridy = 1;
  132.         mainPanel.add(bottomPanel, gbc_bottomPanel);
  133.        
  134.         peg1 = new ColorPeg(Color.decode(colors[0]));
  135.         peg2 = new ColorPeg(Color.decode(colors[1]));
  136.         peg3 = new ColorPeg(Color.decode(colors[2]));
  137.         peg4 = new ColorPeg(Color.decode(colors[3]));
  138.         peg5 = new ColorPeg(Color.decode(colors[4]));
  139.         peg6 = new ColorPeg(Color.decode(colors[5]));
  140.         peg7 = new ColorPeg(Color.decode(colors[6]));
  141.         peg8 = new ColorPeg(Color.decode(colors[7]));
  142.         peg9 = new ColorPeg(Color.decode(colors[8]));
  143.         peg10 = new ColorPeg(Color.decode(colors[9]));
  144.        
  145.         peg1.addMouseListener(new PegListener());
  146.         peg2.addMouseListener(new PegListener());
  147.         peg3.addMouseListener(new PegListener());
  148.         peg4.addMouseListener(new PegListener());
  149.         peg5.addMouseListener(new PegListener());
  150.         peg6.addMouseListener(new PegListener());
  151.         peg7.addMouseListener(new PegListener());
  152.         peg8.addMouseListener(new PegListener());
  153.         peg9.addMouseListener(new PegListener());
  154.         peg10.addMouseListener(new PegListener());
  155.        
  156.         bottomPanel.setLayout(new GridLayout(0, 10, 0, 0));        
  157.         bottomPanel.add(peg1);
  158.         bottomPanel.add(peg2);
  159.         bottomPanel.add(peg3);
  160.         bottomPanel.add(peg4);
  161.         bottomPanel.add(peg5);
  162.         bottomPanel.add(peg6);
  163.         bottomPanel.add(peg7);
  164.         bottomPanel.add(peg8);
  165.         bottomPanel.add(peg9);
  166.         bottomPanel.add(peg10);
  167.     }
  168.  
  169.     private void createMenu()
  170.     {
  171.         menuBar = new JMenuBar();
  172.         menuBar.setBorderPainted(false);
  173.         setJMenuBar(menuBar);
  174.        
  175.         gameMenu = new JMenu("Game");
  176.         menuBar.add(gameMenu);
  177.        
  178.         easyModeItem = new JMenuItem("Easy Mode");
  179.         gameMenu.add(easyModeItem);
  180.        
  181.         advModeItem = new JMenuItem("Advanced Mode");
  182.         gameMenu.add(advModeItem);
  183.        
  184.         helpMenu = new JMenu("Help");
  185.         menuBar.add(helpMenu);
  186.        
  187.         instrItem = new JMenuItem("Instruction");
  188.         helpMenu.add(instrItem);
  189.        
  190.         aboutItem = new JMenuItem("About...");
  191.         helpMenu.add(aboutItem);
  192.     }
  193.    
  194.     public void createDummyPegs()
  195.     {
  196.         for (int i = 0; i < 13; i++)
  197.         {
  198.             initPegsArray.add(new ArrayList<>());
  199.             dummyPegsConstraints.gridy = i;
  200.            
  201.             for (int j = 0; j < 5; j++)
  202.             {
  203.                 dummyPegsConstraints.gridx = j;
  204.                
  205.                 if (i == 0)
  206.                 {
  207.                     tempColorPeg = new ColorPeg(Color.DARK_GRAY);
  208.                     initPegsArray.get(i).add(j, tempColorPeg);
  209.                     pegsLeftPanel.add((JComponent) initPegsArray.get(i).get(j), dummyPegsConstraints);
  210.                 }
  211.                 else
  212.                 {
  213.                     tempDummyPeg = new DummyPeg();
  214.                     initPegsArray.get(i).add(j, tempDummyPeg);
  215.                     pegsLeftPanel.add((JComponent) initPegsArray.get(i).get(j), dummyPegsConstraints);
  216.                 }
  217.             }
  218.         }
  219.     }
  220.    
  221.     public void updatePegs()
  222.     {
  223.         pegsLeftPanel.removeAll();
  224.         for (int i = 0; i < 13; i++)
  225.         {
  226.             dummyPegsConstraints.gridy = i;
  227.             for (int j = 0; j < 5; j++)
  228.             {
  229.                 dummyPegsConstraints.gridx = j;
  230.                 pegsLeftPanel.add((JComponent) initPegsArray.get(i).get(j), dummyPegsConstraints);
  231.                 pegsLeftPanel.repaint();
  232.             }
  233.         }
  234.  
  235.         System.out.println(initPegsArray.get(1).get(0));
  236.         System.out.println(initPegsArray.get(0).get(0));
  237.     }
  238.    
  239.     class PegListener extends MouseAdapter
  240.     {
  241.         @Override
  242.         public void mouseClicked(MouseEvent e)
  243.         {
  244.             super.mouseClicked(e);
  245.            
  246.             if (e.getComponent().equals(peg1))
  247.             {
  248.                 tempColorPeg = new ColorPeg(peg1.getColor());
  249.                 System.out.println("peg1 clicked");
  250.                 System.out.println("tempColorPeg: " + tempColorPeg.getColor());
  251.                 initPegsArray.get(1).remove(0);
  252.                 initPegsArray.get(1).add(0, tempColorPeg);
  253.                 System.out.println(initPegsArray.get(0).get(0));
  254.             }
  255.             else if (e.getComponent().equals(peg2))
  256.             {
  257.                 tempColorPeg = new ColorPeg(peg2.getColor());
  258.                 System.out.println("peg2 clicked");
  259.                 System.out.println("tempColorPeg: " + tempColorPeg.getColor());
  260.                 initPegsArray.get(1).remove(1);
  261.                 initPegsArray.get(1).add(1, tempColorPeg);
  262.             }
  263.             else if (e.getComponent().equals(peg3))
  264.             {
  265.                 System.out.println("peg3 clicked");
  266.             }
  267.             else if (e.getComponent().equals(peg4))
  268.             {
  269.                 System.out.println("peg4 clicked");
  270.             }
  271.             else if (e.getComponent().equals(peg5))
  272.             {
  273.                 System.out.println("peg5 clicked");
  274.             }
  275.             else if (e.getComponent().equals(peg6))
  276.             {
  277.                 System.out.println("peg6 clicked");
  278.             }
  279.             else if (e.getComponent().equals(peg7))
  280.             {
  281.                 System.out.println("peg7 clicked");
  282.             }
  283.             else if (e.getComponent().equals(peg8))
  284.             {
  285.                 System.out.println("peg8 clicked");
  286.             }
  287.             else if (e.getComponent().equals(peg9))
  288.             {
  289.                 System.out.println("peg9 clicked");
  290.             }
  291.             else if (e.getComponent().equals(peg10))
  292.             {
  293.                 System.out.println("peg10 clicked");
  294.             }
  295.            
  296.             updatePegs();
  297.         }
  298.     }
  299. }