0% found this document useful (0 votes)
3 views

Jogo da Forca com Java Swing

The document describes a Java Swing implementation of the Hangman game, created by Prof. Dr. Kleython Lacerda. It includes the main class 'JogoPrincipal' which initializes the game, manages word selection, and handles user interactions through buttons for letter guesses. The game tracks errors, updates the display, and provides feedback to the player upon winning or losing.

Uploaded by

leticiamilan.dev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Jogo da Forca com Java Swing

The document describes a Java Swing implementation of the Hangman game, created by Prof. Dr. Kleython Lacerda. It includes the main class 'JogoPrincipal' which initializes the game, manages word selection, and handles user interactions through buttons for letter guesses. The game tracks errors, updates the display, and provides feedback to the player upon winning or losing.

Uploaded by

leticiamilan.dev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Jogo da Forca com Java Swing

Prof. Dr. Kleython Lacerda

package interfacesGUI;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
*
* @author Kleython Lacerda
*/
public class JogoPrincipal extends javax.swing.JFrame {

private ImageIcon f0 = new ImageIcon(getClass().getResource("/imagens/forca0.png"));


private ImageIcon f1 = new ImageIcon(getClass().getResource("/imagens/forca1.png"));
private ImageIcon f2 = new ImageIcon(getClass().getResource("/imagens/forca2.png"));
private ImageIcon f3 = new ImageIcon(getClass().getResource("/imagens/forca3.png"));
private ImageIcon f4 = new ImageIcon(getClass().getResource("/imagens/forca4.png"));
private ImageIcon f5 = new ImageIcon(getClass().getResource("/imagens/forca5.png"));
private ImageIcon f6 = new ImageIcon(getClass().getResource("/imagens/forca6.png"));
private ArrayList<String> palavras = new ArrayList();
private String palavra;
private String[] resultado;

private int erro, contagem, pontuacao = 0;

public JogoPrincipal() {
initComponents();

inicializar();
}

public void palavras() {


palavras.add("KLEYTHON");
palavras.add("MESA");
palavras.add("PORTEIRO");
palavras.add("MALETA");
palavras.add("CHAVE");
palavras.add("PORTA");
palavras.add("LIVRO");
palavras.add("CARRO");
palavras.add("RUA");
palavras.add("ARCO");
palavras.add("MONTANHA");
palavras.add("SOL");
palavras.add("LUZ");
palavras.add("CACHORRO");
palavras.add("GATO");
palavras.add("HOSPITAL");
palavras.add("ESCRITORIO");
palavras.add("TIGRE");
palavras.add("BOLA");
palavras.add("CUPCAKE");
palavras.add("FLORES");
palavras.add("TELEVISOR");
palavras.add("CAVALO");
palavras.add("LAMPADA");
palavras.add("PAREDE");

Collections.shuffle(palavras);

public void botoesPadrao() {


ArrayList<JButton> botoes = new ArrayList();

botoes.add(jBtnA);
botoes.add(jBtnB);
botoes.add(jBtnC);
botoes.add(jBtnD);
botoes.add(jBtnE);
botoes.add(jBtnF);
botoes.add(jBtnG);
botoes.add(jBtnH);
botoes.add(jBtnI);
botoes.add(jBtnJ);
botoes.add(jBtnK);
botoes.add(jBtnL);
botoes.add(jBtnM);
botoes.add(jBtnN);
botoes.add(jBtnO);
botoes.add(jBtnP);
botoes.add(jBtnQ);
botoes.add(jBtnR);
botoes.add(jBtnS);
botoes.add(jBtnT);
botoes.add(jBtnU);
botoes.add(jBtnV);
botoes.add(jBtnW);
botoes.add(jBtnX);
botoes.add(jBtnY);
botoes.add(jBtnZ);

for (JButton botao : botoes) {


botao.setBackground(Color.WHITE);
botao.setForeground(new Color(0, 51, 51));
botao.setEnabled(true);
}
}

private void inicializar() {


palavras();
botoesPadrao();

this.erro = 0;
this.contagem = 0;
jLbPontuacao.setText(String.valueOf(this.pontuacao));

this.palavra = this.palavras.get(0);
this.resultado = new String[palavra.length()];
this.palavras.remove(0);

jLbForca.setIcon(f0);
jLbPalavra.setText("");

for (int i = 0; i < this.palavra.length(); i++) {


resultado[i] = " ___ ";
jLbPalavra.setText(jLbPalavra.getText() + resultado[i]);
}

private void verificar(JButton b) {


boolean contem = false;

b.setEnabled(false);
jLbPalavra.setText("");

for (int i = 0; i < palavra.length(); i++) {


if (b.getText().charAt(0) == palavra.charAt(i)) {
resultado[i] = " " + b.getText().charAt(0) + " ";
contem = true;
this.contagem++;
}
jLbPalavra.setText(jLbPalavra.getText() + resultado[i]);
}

if (contem) {//acertou a letra


b.setForeground(Color.BLUE);
} else { //Errou a letra
b.setForeground(Color.red);
this.erro++;
switch (this.erro) {
case 1:
jLbForca.setIcon(f1);
break;
case 2:
jLbForca.setIcon(f2);
break;
case 3:
jLbForca.setIcon(f3);
break;
case 4:
jLbForca.setIcon(f4);
break;
case 5:
jLbForca.setIcon(f5);
break;
case 6:
jLbForca.setIcon(f6);
break;
default:
jLbForca.setIcon(f0);
break;
}
}

if (this.contagem == this.palavra.length()) { //Acertou a palavra


JOptionPane.showMessageDialog(null, "Você acertou a palavra!", "Fim da rodada",
JOptionPane.INFORMATION_MESSAGE);
this.pontuacao ++;
inicializar();
}
if (this.erro == 6) { //Errou a palavra
JOptionPane.showMessageDialog(null, "Você errou a palavra! A resposta correta é: " + this.palavra, "Fim da
rodada", JOptionPane.INFORMATION_MESSAGE);
inicializar();
}
}
public static void main(String args[]) {
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(JogoPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JogoPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JogoPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JogoPrincipal.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 JogoPrincipal().setVisible(true);
}
});
}

@SuppressWarnings("unchecked")

………..

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


verificar(jBtnA);
}
private void jBtnCActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnC);
}
private void jBtnSActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnS);
}
private void jBtnRActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnR);
}
private void jBtnOActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnO);
}
private void jBtnUActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnU);
}
private void jBtnYActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnY);
}
private void jBtnBActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnB);
}
private void jBtnDActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnD);
}
private void jBtnEActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnE);
}
private void jBtnFActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnF);
}
private void jBtnGActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnG);
}
private void jBtnHActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnH);
}
private void jBtnIActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnI);
}
private void jBtnJActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnJ);
}
private void jBtnKActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnK);
}
private void jBtnLActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnL);
}
private void jBtnMActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnM);
}
private void jBtnNActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnN);
}
private void jBtnPActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnP);
}
private void jBtnQActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnQ);
}
private void jBtnTActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnT);
}
private void jBtnVActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnV);
}
private void jBtnWActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnW);
}
private void jBtnXActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnX);
}
private void jBtnZActionPerformed(java.awt.event.ActionEvent evt) {
verificar(jBtnZ);
}
// Variables declaration - do not modify
private javax.swing.JButton jBtnA;
private javax.swing.JButton jBtnB;
private javax.swing.JButton jBtnC;
private javax.swing.JButton jBtnD;
private javax.swing.JButton jBtnE;
private javax.swing.JButton jBtnF;
private javax.swing.JButton jBtnG;
private javax.swing.JButton jBtnH;
private javax.swing.JButton jBtnI;
private javax.swing.JButton jBtnJ;
private javax.swing.JButton jBtnK;
private javax.swing.JButton jBtnL;
private javax.swing.JButton jBtnM;
private javax.swing.JButton jBtnN;
private javax.swing.JButton jBtnO;
private javax.swing.JButton jBtnP;
private javax.swing.JButton jBtnQ;
private javax.swing.JButton jBtnR;
private javax.swing.JButton jBtnS;
private javax.swing.JButton jBtnT;
private javax.swing.JButton jBtnU;
private javax.swing.JButton jBtnV;
private javax.swing.JButton jBtnW;
private javax.swing.JButton jBtnX;
private javax.swing.JButton jBtnY;
private javax.swing.JButton jBtnZ;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLbByKleythonLacerda;
private javax.swing.JLabel jLbForca;
private javax.swing.JLabel jLbPalavra;
private javax.swing.JLabel jLbPontuacao;
private javax.swing.JLabel jLbTituloJogo;
private javax.swing.JLabel jLbTituloPavra;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanelTeclas;
// End of variables declaration
}

You might also like