0% found this document useful (0 votes)
4 views9 pages

Jogo da Velha com Swing

The document outlines the implementation of a Tic-Tac-Toe game using Java Swing, featuring an initial screen and a game interface. The initial screen allows users to start the game, while the game interface manages player turns, displays player scores, and checks for win conditions. The code includes event handling for button interactions and visual updates based on player actions.

Uploaded by

leticiamilan.dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Jogo da Velha com Swing

The document outlines the implementation of a Tic-Tac-Toe game using Java Swing, featuring an initial screen and a game interface. The initial screen allows users to start the game, while the game interface manages player turns, displays player scores, and checks for win conditions. The code includes event handling for button interactions and visual updates based on player actions.

Uploaded by

leticiamilan.dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Jogo da Velha com Swing

Tela Inicial

Tela do Jogo
Código Tela Inicial
package interfacesGUI;

import java.awt.Color;

public class TelaInicial extends javax.swing.JFrame {

Color c1 = new Color(255,255,255);

Color c2 = new Color(115,180,110);

public TelaInicial() {

initComponents();

jBtnIniciar.setBackground(c1);

@SuppressWarnings("unchecked")

………

private void jBtnIniciarMouseEntered(java.awt.event.MouseEvent evt) {

// TODO add your handling code here:

jBtnIniciar.setBackground(c2);

private void jBtnIniciarMouseExited(java.awt.event.MouseEvent evt) {

// TODO add your handling code here:

jBtnIniciar.setBackground(c1);

private void jBtnIniciarActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

JogoDaVelha jv1 = new JogoDaVelha();

this.setVisible(false);

jv1.setVisible(true);

/**

* @param args the command line arguments

*/

public static void main(String args[]) {


/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(TelaInicial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(TelaInicial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(TelaInicial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(TelaInicial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new TelaInicial().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton jBtnIniciar;

private javax.swing.JLabel jLabel1;

private javax.swing.JPanel jPanel1;

// End of variables declaration

}
Tela do Jogo
package interfacesGUI;

import java.awt.Color;

import javax.swing.JButton;

public class JogoDaVelha extends javax.swing.JFrame {

int res[][] = new int[4][4];

boolean vez = true;

int player1 = 0, player2 = 0, contagem = 0, total = 0;

public JogoDaVelha() {

initComponents();

@SuppressWarnings("unchecked")

………

private void jBtn21ActionPerformed(java.awt.event.ActionEvent evt) {

vez(jBtn21, 2, 1);

private void jBtn22ActionPerformed(java.awt.event.ActionEvent evt) {

vez(jBtn22, 2, 2);

private void jBtn11ActionPerformed(java.awt.event.ActionEvent evt) {

vez(jBtn11, 1, 1);

private void jBtn12ActionPerformed(java.awt.event.ActionEvent evt) {

vez(jBtn12, 1, 2);

private void jBtn13ActionPerformed(java.awt.event.ActionEvent evt) {

vez(jBtn13, 1, 3);

private void jBtn23ActionPerformed(java.awt.event.ActionEvent evt) {

vez(jBtn23, 2, 3);

}
private void jBtn31ActionPerformed(java.awt.event.ActionEvent evt) {

vez(jBtn31, 3, 1);

private void jBtn32ActionPerformed(java.awt.event.ActionEvent evt) {

vez(jBtn32, 3, 2);

private void jBtn33ActionPerformed(java.awt.event.ActionEvent evt) {

vez(jBtn33, 3, 3);

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(JogoDaVelha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(JogoDaVelha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(JogoDaVelha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(JogoDaVelha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new JogoDaVelha().setVisible(true);
}

});

public void vez(JButton btn, int linha, int coluna) {

if (this.vez && this.res[linha][coluna] == 0) { //true = vez do X

btn.setBackground(Color.BLACK);

btn.setForeground(Color.WHITE);

btn.setText("X");

this.res[linha][coluna] = 1;

this.contagem++;

} else if (!this.vez && this.res[linha][coluna] == 0) { //false = vez do O

btn.setBackground(Color.GRAY);

btn.setForeground(Color.BLACK);

btn.setText("O");

this.res[linha][coluna] = 100;

this.contagem++;

this.vez = !this.vez;

verificar();

public void verificar() {

int somaDP = 0, somaDS = 0;

for (int i = 1; i <= 3; i++) {

somaDP += this.res[i][i];

somaDS += this.res[i][4 - i];

int somaL = 0, somaC = 0;

for (int j = 1; j <= 3; j++) {

somaL += this.res[i][j];

somaC += this.res[j][i];

if (somaL == 3 || somaC == 3) {

this.player1++;

limpar();

} else if (somaL == 300 || somaC == 300) {


this.player2++;

limpar();

if (somaDP == 3 || somaDS == 3) {

this.player1++;

limpar();

} else if (somaDP == 300 || somaDS == 300) {

this.player2++;

limpar();

if (this.contagem == 9 && this.res[1][1] != 0) { //Deu velha

limpar();

public void limpar() {

this.total++;

if(this.total == 10){ //10 partidas

jLbPlayer1.setText("0");

jLbPlayer2.setText("0");

this.total = 0;

}else{

jLbPlayer1.setText(String.valueOf(this.player1));

jLbPlayer2.setText(String.valueOf(this.player2));

jProgressBar1.setValue(this.total);

this.vez = true; //o jogador X deverá sempre ser o primeiro

this.contagem = 0;

for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= 3; j++) {

this.res[i][j] = 0;

}
jBtn11.setBackground(Color.WHITE);

jBtn11.setText("");

jBtn12.setBackground(Color.WHITE);

jBtn12.setText("");

jBtn13.setBackground(Color.WHITE);

jBtn13.setText("");

jBtn21.setBackground(Color.WHITE);

jBtn21.setText("");

jBtn22.setBackground(Color.WHITE);

jBtn22.setText("");

jBtn23.setBackground(Color.WHITE);

jBtn23.setText("");

jBtn31.setBackground(Color.WHITE);

jBtn31.setText("");

jBtn32.setBackground(Color.WHITE);

jBtn32.setText("");

jBtn33.setBackground(Color.WHITE);

jBtn33.setText("");

// Variables declaration - do not modify

private javax.swing.JButton jBtn11;

private javax.swing.JButton jBtn12;

private javax.swing.JButton jBtn13;

private javax.swing.JButton jBtn21;

private javax.swing.JButton jBtn22;

private javax.swing.JButton jBtn23;

private javax.swing.JButton jBtn31;

private javax.swing.JButton jBtn32;

private javax.swing.JButton jBtn33;


private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLbPlayer1;

private javax.swing.JLabel jLbPlayer2;

private javax.swing.JLabel jLbPlayer3;

private javax.swing.JPanel jPanel1;

private javax.swing.JPanel jPanel2;

private javax.swing.JPanel jPanel3;

private javax.swing.JProgressBar jProgressBar1;

// End of variables declaration

You might also like