This is my code for the game of mastermind, the button i added screwed up the grids so now they are a wierd diagonal pattern. also, i need some way to check for a winner with the button i named "winCheck". Can somebody help me come up with a block of code that will enable me to do this, and where i should put it
package mastermind;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
/**
*
* @author student
*/
public class Game {
private JFrame window;
private JPanel[] square;
private JPanel mPanel;
private JLabel[] marker;
private JLabel[] indicator;
private JButton winCheck;
private static Random rand;
private final int NUM_ROWS = 11;
private final int NUM_COLS = 5;
private JMenuBar menuBar;
private JMenu optionMenu, rulesMenu, aboutMenu;
private JMenuItem[] optionMenuItem;
private JMenuItem[] rulesMenuItem;
private JMenuItem aboutMenuItem;
public Game() {
window = new JFrame("Mastermind");
window.setSize(400,600);
window.setBackground(Color.BLACK);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(NUM_ROWS, NUM_COLS,2,3));
winCheck = new JButton("Check");
square = new JPanel[NUM_ROWS*NUM_COLS];
marker = new JLabel[NUM_ROWS*NUM_COLS];
mPanel = new JPanel();
mPanel.add(winCheck);
for(int i=0; i<square.length; i++) {
square[i] = new JPanel();
square[i].setName("" + i);
square[i].addMouseListener(new ClickListener());
square[i].setBackground(Color.WHITE);
square[i].setLayout(new BorderLayout());
square[i].add(mPanel,BorderLayout.PAGE_END);
square[i].add(winCheck, BorderLayout.WEST);
if(i%5!=4) {
marker[i] = new JLabel();
square[i].add(marker[i]);
marker[i].setOpaque(true);
}
}
menuBar = new JMenuBar();
optionMenu = new JMenu("Options");
rulesMenu = new JMenu("Rules");
aboutMenu = new JMenu("About");
optionMenuItem = new JMenuItem[3];
rulesMenuItem = new JMenuItem[2];
aboutMenuItem = new JMenuItem("About Mastermind");
for(int i=0; i<optionMenuItem.length; i++)
optionMenuItem[i] = new JMenuItem();
optionMenuItem[0].setText("Undo move");
optionMenuItem[1].setText("Restart");
optionMenuItem[2].setText("Exit");
rulesMenuItem[0] = new JMenuItem("text here");
rulesMenuItem[1] = new JMenuItem("text here");
aboutMenu.add(aboutMenuItem);
rulesMenu.add(rulesMenuItem[0]);
rulesMenu.add(rulesMenuItem[1]);
for(int i=0; i<optionMenuItem.length; i++)
optionMenu.add(optionMenuItem[i]);
menuBar.add(optionMenu);
menuBar.add(rulesMenu);
menuBar.add(aboutMenu);
for(JPanel p:square)
window.add(p);
window.setJMenuBar(menuBar);
window.setVisible(true);
// marker[49].getLayout();
//square[54].setLayout(new GridLayout(2,2,2,3));
square[49].setLayout(new GridLayout(2,2,2,3));
square[44].setLayout(new GridLayout(2,2,2,3));
square[39].setLayout(new GridLayout(2,2,2,3));
square[34].setLayout(new GridLayout(2,2,2,3));
square[29].setLayout(new GridLayout(2,2,2,3));
square[24].setLayout(new GridLayout(2,2,2,3));
square[19].setLayout(new GridLayout(2,2,2,3));
square[14].setLayout(new GridLayout(2,2,2,3));
square[9].setLayout(new GridLayout(2,2,2,3));
square[4].setLayout(new GridLayout(2,2,2,3));
indicator = new JLabel[4*NUM_ROWS];
for(int i=0; i<indicator.length; i++) {
indicator[i] = new JLabel();
indicator[i].setBackground(Color.LIGHT_GRAY);
indicator[i].setOpaque(true);
}
for(int i=4; i<square.length; i=i+5) {
for(int j=4; j>0; j--)
square[i].add(indicator[i-j-i/5]);
}
window.add(mPanel);
}
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
}
}
private class ClickListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent me) {
int loc = Integer.parseInt(((JPanel)(me.getSource())).getName());
if(((JPanel)(me.getSource())).getBackground().equals(Color.WHITE)) {
((JPanel)(me.getSource())).setBackground(Color.RED);
}
else if(((JPanel)(me.getSource())).getBackground().equals(Color.RED)) {
((JPanel)(me.getSource())).setBackground(Color.BLUE);
}
else if(((JPanel)(me.getSource())).getBackground().equals(Color.BLUE)) {
((JPanel)(me.getSource())).setBackground(Color.YELLOW);
}
else if(((JPanel)(me.getSource())).getBackground().equals(Color.YELLOW)) {
((JPanel)(me.getSource())).setBackground(Color.GREEN);
}
else if(((JPanel)(me.getSource())).getBackground().equals(Color.GREEN)) {
((JPanel)(me.getSource())).setBackground(Color.RED);
}
}
@Override
public void mousePressed(MouseEvent me) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(MouseEvent me) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(MouseEvent me) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(MouseEvent me) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
}
Aucun commentaire:
Enregistrer un commentaire