Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.rburgos.mastermindtestlayout;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.ArrayList;
- import javax.swing.*;
- import javax.swing.border.*;
- @SuppressWarnings("serial")
- public class MainLayout extends JFrame
- {
- private JPanel mainPanel, topPanel, pegsLeftPanel, hintsRightPanel, bottomPanel;
- private JMenuBar menuBar;
- private JMenu gameMenu, helpMenu;
- private JMenuItem easyModeItem, advModeItem, instrItem, aboutItem;
- private String[] colors = {"#eccc75", "#72688c", "#f6a01a", "#116348",
- "#3b7e98", "#d53533", "#a36526", "#679317", "#ed9f9f", "#1be2b4"};
- private ColorPeg peg1, peg2, peg3, peg4, peg5, peg6, peg7, peg8, peg9, peg10;
- private ColorPeg tempColorPeg;
- private DummyPeg tempDummyPeg;
- private GridBagConstraints dummyPegsConstraints;
- private int col = 0, row = 0;
- private ArrayList<ArrayList<? super JComponent>> initPegsArray = new ArrayList<>();
- private ArrayList<ArrayList<? super JComponent>> guessPegsAray = new ArrayList<>();
- private ArrayList<? super JComponent> answerPegsArray = new ArrayList<>();
- public static void main(String[] args)
- {
- try
- {
- UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
- }
- catch (Throwable e)
- {
- e.printStackTrace();
- }
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- try
- {
- MainLayout frame = new MainLayout();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public MainLayout()
- {
- // Window
- setResizable(false);
- setTitle("MasterMind");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 500, 700);
- createMenu();
- // Main panel:
- // Divided into "top" and "bottom" by topPanel and bottomPanel
- mainPanel = new JPanel();
- mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(mainPanel);
- GridBagLayout gbl_contentPane = new GridBagLayout();
- gbl_contentPane.columnWidths = new int[]{0};
- gbl_contentPane.rowHeights = new int[]{591, 0, 0};
- gbl_contentPane.columnWeights = new double[]{1.0};
- gbl_contentPane.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
- mainPanel.setLayout(gbl_contentPane);
- topPanel = new JPanel();
- GridBagConstraints gbc_topPanel = new GridBagConstraints();
- gbc_topPanel.insets = new Insets(0, 0, 5, 0);
- gbc_topPanel.fill = GridBagConstraints.BOTH;
- gbc_topPanel.gridx = 0;
- gbc_topPanel.gridy = 0;
- mainPanel.add(topPanel, gbc_topPanel);
- GridBagLayout gbl_topPanel = new GridBagLayout();
- gbl_topPanel.columnWidths = new int[]{0, 0, 0};
- gbl_topPanel.rowHeights = new int[]{0, 0};
- gbl_topPanel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
- gbl_topPanel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
- topPanel.setLayout(gbl_topPanel);
- pegsLeftPanel = new JPanel();
- pegsLeftPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
- GridBagConstraints gbc_pegsLeftPanel = new GridBagConstraints();
- gbc_pegsLeftPanel.anchor = GridBagConstraints.NORTH;
- gbc_pegsLeftPanel.fill = GridBagConstraints.BOTH;
- gbc_pegsLeftPanel.gridx = 0;
- gbc_pegsLeftPanel.gridy = 0;
- topPanel.add(pegsLeftPanel, gbc_pegsLeftPanel);
- GridBagLayout gbl_pegsLeftPanel = new GridBagLayout();
- gbl_pegsLeftPanel.columnWidths = new int[] {0};
- gbl_pegsLeftPanel.rowHeights = new int[] {30};
- gbl_pegsLeftPanel.columnWeights = new double[]{0.0};
- gbl_pegsLeftPanel.rowWeights = new double[]{0.0};
- pegsLeftPanel.setLayout(gbl_pegsLeftPanel);
- dummyPegsConstraints = new GridBagConstraints();
- dummyPegsConstraints.insets = new Insets(2, 3, 2, 3);
- createDummyPegs();
- hintsRightPanel = new JPanel();
- hintsRightPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
- GridBagConstraints gbc_hintsRightPanel = new GridBagConstraints();
- gbc_hintsRightPanel.fill = GridBagConstraints.BOTH;
- gbc_hintsRightPanel.gridx = 1;
- gbc_hintsRightPanel.gridy = 0;
- topPanel.add(hintsRightPanel, gbc_hintsRightPanel);
- bottomPanel = new JPanel();
- bottomPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
- GridBagConstraints gbc_bottomPanel = new GridBagConstraints();
- gbc_bottomPanel.fill = GridBagConstraints.BOTH;
- gbc_bottomPanel.gridx = 0;
- gbc_bottomPanel.gridy = 1;
- mainPanel.add(bottomPanel, gbc_bottomPanel);
- peg1 = new ColorPeg(Color.decode(colors[0]));
- peg2 = new ColorPeg(Color.decode(colors[1]));
- peg3 = new ColorPeg(Color.decode(colors[2]));
- peg4 = new ColorPeg(Color.decode(colors[3]));
- peg5 = new ColorPeg(Color.decode(colors[4]));
- peg6 = new ColorPeg(Color.decode(colors[5]));
- peg7 = new ColorPeg(Color.decode(colors[6]));
- peg8 = new ColorPeg(Color.decode(colors[7]));
- peg9 = new ColorPeg(Color.decode(colors[8]));
- peg10 = new ColorPeg(Color.decode(colors[9]));
- peg1.addMouseListener(new PegListener());
- peg2.addMouseListener(new PegListener());
- peg3.addMouseListener(new PegListener());
- peg4.addMouseListener(new PegListener());
- peg5.addMouseListener(new PegListener());
- peg6.addMouseListener(new PegListener());
- peg7.addMouseListener(new PegListener());
- peg8.addMouseListener(new PegListener());
- peg9.addMouseListener(new PegListener());
- peg10.addMouseListener(new PegListener());
- bottomPanel.setLayout(new GridLayout(0, 10, 0, 0));
- bottomPanel.add(peg1);
- bottomPanel.add(peg2);
- bottomPanel.add(peg3);
- bottomPanel.add(peg4);
- bottomPanel.add(peg5);
- bottomPanel.add(peg6);
- bottomPanel.add(peg7);
- bottomPanel.add(peg8);
- bottomPanel.add(peg9);
- bottomPanel.add(peg10);
- }
- private void createMenu()
- {
- menuBar = new JMenuBar();
- menuBar.setBorderPainted(false);
- setJMenuBar(menuBar);
- gameMenu = new JMenu("Game");
- menuBar.add(gameMenu);
- easyModeItem = new JMenuItem("Easy Mode");
- gameMenu.add(easyModeItem);
- advModeItem = new JMenuItem("Advanced Mode");
- gameMenu.add(advModeItem);
- helpMenu = new JMenu("Help");
- menuBar.add(helpMenu);
- instrItem = new JMenuItem("Instruction");
- helpMenu.add(instrItem);
- aboutItem = new JMenuItem("About...");
- helpMenu.add(aboutItem);
- }
- public void createDummyPegs()
- {
- for (int i = 0; i < 13; i++)
- {
- initPegsArray.add(new ArrayList<>());
- dummyPegsConstraints.gridy = i;
- for (int j = 0; j < 5; j++)
- {
- dummyPegsConstraints.gridx = j;
- if (i == 0)
- {
- tempColorPeg = new ColorPeg(Color.DARK_GRAY);
- initPegsArray.get(i).add(j, tempColorPeg);
- pegsLeftPanel.add((JComponent) initPegsArray.get(i).get(j), dummyPegsConstraints);
- }
- else
- {
- tempDummyPeg = new DummyPeg();
- initPegsArray.get(i).add(j, tempDummyPeg);
- pegsLeftPanel.add((JComponent) initPegsArray.get(i).get(j), dummyPegsConstraints);
- }
- }
- }
- }
- public void updatePegs()
- {
- pegsLeftPanel.removeAll();
- for (int i = 0; i < 13; i++)
- {
- dummyPegsConstraints.gridy = i;
- for (int j = 0; j < 5; j++)
- {
- dummyPegsConstraints.gridx = j;
- pegsLeftPanel.add((JComponent) initPegsArray.get(i).get(j), dummyPegsConstraints);
- pegsLeftPanel.repaint();
- }
- }
- System.out.println(initPegsArray.get(1).get(0));
- System.out.println(initPegsArray.get(0).get(0));
- }
- class PegListener extends MouseAdapter
- {
- @Override
- public void mouseClicked(MouseEvent e)
- {
- super.mouseClicked(e);
- if (e.getComponent().equals(peg1))
- {
- tempColorPeg = new ColorPeg(peg1.getColor());
- System.out.println("peg1 clicked");
- System.out.println("tempColorPeg: " + tempColorPeg.getColor());
- initPegsArray.get(1).remove(0);
- initPegsArray.get(1).add(0, tempColorPeg);
- System.out.println(initPegsArray.get(0).get(0));
- }
- else if (e.getComponent().equals(peg2))
- {
- tempColorPeg = new ColorPeg(peg2.getColor());
- System.out.println("peg2 clicked");
- System.out.println("tempColorPeg: " + tempColorPeg.getColor());
- initPegsArray.get(1).remove(1);
- initPegsArray.get(1).add(1, tempColorPeg);
- }
- else if (e.getComponent().equals(peg3))
- {
- System.out.println("peg3 clicked");
- }
- else if (e.getComponent().equals(peg4))
- {
- System.out.println("peg4 clicked");
- }
- else if (e.getComponent().equals(peg5))
- {
- System.out.println("peg5 clicked");
- }
- else if (e.getComponent().equals(peg6))
- {
- System.out.println("peg6 clicked");
- }
- else if (e.getComponent().equals(peg7))
- {
- System.out.println("peg7 clicked");
- }
- else if (e.getComponent().equals(peg8))
- {
- System.out.println("peg8 clicked");
- }
- else if (e.getComponent().equals(peg9))
- {
- System.out.println("peg9 clicked");
- }
- else if (e.getComponent().equals(peg10))
- {
- System.out.println("peg10 clicked");
- }
- updatePegs();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement