import com.sun.awt.AWTUtilities; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.border.EmptyBorder; import org.jvnet.substance.painter.GlassGradientPainter; import org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel; /** * @author Shirin Joshi */ public class TicTacToe extends JFrame implements ActionListener { int turn = 0; int count = 0; public TicTacToe() { initComponents(); initListner(); } public static void main(String a[]) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try { SubstanceBusinessBlueSteelLookAndFeel.setCurrentGradientPainter(new GlassGradientPainter()); UIManager.setLookAndFeel(new SubstanceBusinessBlueSteelLookAndFeel()); } catch (Exception exception) { System.out.println("L&F not set" + exception); } TicTacToe objTicTacToe = new TicTacToe(); objTicTacToe.setDefaultCloseOperation(EXIT_ON_CLOSE); objTicTacToe.setSize(170, 180); objTicTacToe.setResizable(false); objTicTacToe.setVisible(true); // AWTUtilities.setWindowOpacity(objTicTacToe, 0.3F); // AWTUtilities.setWindowOpacity(JOptionPane.getRootFrame(),0.3F); } private void checkWiningCondition() { // Check is Pressed the Button or not // Check First Line Horizontal Line if (!btn_0.isEnabled() && !btn_1.isEnabled() && !btn_2.isEnabled()) { if (btn_0.getText().equalsIgnoreCase(btn_1.getText()) && btn_1.getText().equalsIgnoreCase(btn_2.getText())) { setEnableAll(false); JOptionPane.showMessageDialog(this, "Winner Is 1 : " + btn_0.getText()); setWinnerBold(btn_0, btn_1, btn_2); return; } } // Check 2nd Line Horizontal Line if (!btn_3.isEnabled() && !btn_4.isEnabled() && !btn_5.isEnabled()) { if (btn_3.getText().equalsIgnoreCase(btn_4.getText()) && btn_4.getText().equalsIgnoreCase(btn_5.getText())) { setEnableAll(false); JOptionPane.showMessageDialog(this, "Winner Is 2 : " + btn_3.getText()); setWinnerBold(btn_3, btn_4, btn_5); return; } } // Check 3nd Line Horizontal Line if (!btn_6.isEnabled() && !btn_7.isEnabled() && !btn_8.isEnabled()) { if (btn_6.getText().equalsIgnoreCase(btn_7.getText()) && btn_7.getText().equalsIgnoreCase(btn_8.getText())) { setEnableAll(false); JOptionPane.showMessageDialog(this, "Winner Is 3 : " + btn_6.getText()); setWinnerBold(btn_6, btn_7, btn_8); return; } } // Check First Line Verticle Line if (!btn_0.isEnabled() && !btn_3.isEnabled() && !btn_6.isEnabled()) { if (btn_0.getText().equalsIgnoreCase(btn_3.getText()) && btn_3.getText().equalsIgnoreCase(btn_6.getText())) { setEnableAll(false); JOptionPane.showMessageDialog(this, "Winner Is 4 : " + btn_0.getText()); setWinnerBold(btn_0, btn_3, btn_6); return; } } // Check 2nd Line Verticle Line if (!btn_1.isEnabled() && !btn_4.isEnabled() && !btn_7.isEnabled()) { if (btn_2.getText().equalsIgnoreCase(btn_4.getText()) && btn_4.getText().equalsIgnoreCase(btn_7.getText())) { setEnableAll(false); JOptionPane.showMessageDialog(this, "Winner Is 5 : " + btn_1.getText()); setWinnerBold(btn_1, btn_4, btn_7); return; } } // Check 3nd Line Verticle Line if (!btn_2.isEnabled() && !btn_5.isEnabled() && !btn_8.isEnabled()) { if (btn_2.getText().equalsIgnoreCase(btn_5.getText()) && btn_5.getText().equalsIgnoreCase(btn_8.getText())) { setEnableAll(false); JOptionPane.showMessageDialog(this, "Winner Is 6 : " + btn_2.getText()); setWinnerBold(btn_2, btn_5, btn_8); return; } } // Check Diagnol \ if (!btn_0.isEnabled() && !btn_4.isEnabled() && !btn_8.isEnabled()) { if (btn_0.getText().equalsIgnoreCase(btn_4.getText()) && btn_4.getText().equalsIgnoreCase(btn_8.getText())) { setEnableAll(false); JOptionPane.showMessageDialog(this, "Winner Is 7 : " + btn_0.getText()); setWinnerBold(btn_0, btn_4, btn_8); return; } } // Check Diagnol / if (!btn_2.isEnabled() && !btn_4.isEnabled() && !btn_6.isEnabled()) { if (btn_2.getText().equalsIgnoreCase(btn_4.getText()) && btn_4.getText().equalsIgnoreCase(btn_6.getText())) { JOptionPane.showMessageDialog(this, "Winner Is 8 : " + btn_2.getText()); setEnableAll(false); setWinnerBold(btn_2, btn_4, btn_6); return; } } if (count == 9) { if (!btnReset.isEnabled()) { JOptionPane.showMessageDialog(this, "Draw Match..."); btnReset.setEnabled(true); return; } } } private void clearAll() { btn_0.setText(""); btn_1.setText(""); btn_2.setText(""); btn_3.setText(""); btn_4.setText(""); btn_5.setText(""); btn_6.setText(""); btn_7.setText(""); btn_8.setText(""); turn = 0; count = 0; } private void initComponents() { // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents // Generated using JFormDesigner Evaluation license - Shirin Joshi dialogPane = new JPanel(); contentPanel = new JPanel(); btn_0 = new JButton(); btn_1 = new JButton(); btn_2 = new JButton(); btn_3 = new JButton(); btn_4 = new JButton(); btn_5 = new JButton(); btn_6 = new JButton(); btn_7 = new JButton(); btn_8 = new JButton(); buttonBar = new JPanel(); btnReset = new JButton(); cancelButton = new JButton(); //======== this ======== setTitle("TicTacToe"); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); //======== dialogPane ======== { dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12)); dialogPane.setLayout(new BorderLayout()); //======== contentPanel ======== { contentPanel.setLayout(new GridLayout(3, 3)); contentPanel.add(btn_0); contentPanel.add(btn_1); contentPanel.add(btn_2); contentPanel.add(btn_3); contentPanel.add(btn_4); contentPanel.add(btn_5); contentPanel.add(btn_6); contentPanel.add(btn_7); contentPanel.add(btn_8); } dialogPane.add(contentPanel, BorderLayout.CENTER); //======== buttonBar ======== { buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0)); buttonBar.setLayout(new GridBagLayout()); ((GridBagLayout) buttonBar.getLayout()).columnWidths = new int[]{53, 46, 40}; ((GridBagLayout) buttonBar.getLayout()).columnWeights = new double[]{1.0, 0.0, 0.0}; //---- btnReset ---- btnReset.setText("Reset"); btnReset.setEnabled(false); buttonBar.add(btnReset, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 3), 0, 0)); //---- cancelButton ---- cancelButton.setText("Cancel"); buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); } dialogPane.add(buttonBar, BorderLayout.SOUTH); } contentPane.add(dialogPane, BorderLayout.CENTER); //pack(); setLocationRelativeTo(getOwner()); // JFormDesigner - End of component initialization //GEN-END:initComponents } // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables // Generated using JFormDesigner Evaluation license - Shirin Joshi private JPanel dialogPane; private JPanel contentPanel; private JButton btn_0; private JButton btn_1; private JButton btn_2; private JButton btn_3; private JButton btn_4; private JButton btn_5; private JButton btn_6; private JButton btn_7; private JButton btn_8; private JPanel buttonBar; private JButton btnReset; private JButton cancelButton; public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equalsIgnoreCase("K")) { setEnableAll(true); clearAll(); } else { JButton Clicked = (JButton) e.getSource(); if (Clicked.getText().trim().length() == 0) { Clicked.setEnabled(false); Clicked.setText(turn == 0 ? "0" : "X"); turn = turn == 0 ? 1 : 0; count++; // Check Condition always occured after 4th Click.. if (count > 3) { checkWiningCondition(); } } } } private void initListner() { btn_0.addActionListener(this); btn_1.addActionListener(this); btn_2.addActionListener(this); btn_3.addActionListener(this); btn_4.addActionListener(this); btn_5.addActionListener(this); btn_6.addActionListener(this); btn_7.addActionListener(this); btn_8.addActionListener(this); btnReset.setActionCommand("K"); btnReset.addActionListener(this); } private void setEnableAll(boolean p_isEnable) { btn_0.setEnabled(p_isEnable); btn_1.setEnabled(p_isEnable); btn_2.setEnabled(p_isEnable); btn_3.setEnabled(p_isEnable); btn_4.setEnabled(p_isEnable); btn_5.setEnabled(p_isEnable); btn_6.setEnabled(p_isEnable); btn_7.setEnabled(p_isEnable); btn_8.setEnabled(p_isEnable); if (p_isEnable) { btn_0.setForeground(btnReset.getForeground()); btn_1.setForeground(btnReset.getForeground()); btn_2.setForeground(btnReset.getForeground()); btn_3.setForeground(btnReset.getForeground()); btn_4.setForeground(btnReset.getForeground()); btn_5.setForeground(btnReset.getForeground()); btn_6.setForeground(btnReset.getForeground()); btn_7.setForeground(btnReset.getForeground()); btn_8.setForeground(btnReset.getForeground()); } btnReset.setEnabled(!p_isEnable); } private void setWinnerBold(JButton btn_0, JButton btn_1, JButton btn_2) { btn_0.setForeground(Color.blue); btn_1.setForeground(Color.blue); btn_2.setForeground(Color.blue); } // JFormDesigner - End of variables declaration //GEN-END:variables }
This blog will provide details on some generic and important stuff required for JAVA Technology
Tuesday, April 22, 2008
TIC TAC TOE Version 1 – Standalone Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment