Olá Pessoal, to fazendo aqui um joguinho da forca, mas não to consseguindo chamar métodos de outra classe.
eu entregeui ao professor o projeto, mas ele só contia duas classes, o da tela inicial e o do corpo, ai ele me disse q eu fugi um pouco do q se diz O.O (Orientado a Objeto), ai estou tentando dividir em classes, tipo: Monta_boneco, Sorteia_palavra, Mostra_letra estc; mas não to consseguindo chamar essas classes para a classe q contem o método main, gostaria de uma solução ai de vcs galera, eu sou novo na linguagem e assim como todo mundo, tenho um pouco de dificuldade no início, mas eu vou consseguir nominar com fé em Deus.
o codigo q eu vou postar ta de forma estruturada
Bom pessoal conto com a ajuda de vcs.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import javax.swing.*;
public class Forca extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
String[] listaPalavras =
{"PETROLEO", "PESTANA", "PESTILENTO", "PETELECO", "REBOQUE", "CADAFALSO",
"CADEIRA", "COLA", "REBENTO", "DEFUMADO", "DISCURSO", "ELETRICO",
"ELETRONICA", "ENGRENAGEM", "ESFOMEADO", "FERREIRO", "FERROVIA",
"FERTIL", "FORTALEZA", "FOBIA", "OFICINA", "ORNAMENTO", "PALAVRA",
"PREOCUPACAO", "RADIOLOGIA", "RÁDIO", "GRANJA", "GRANULADO", "INDUZIDO",
"IMBATIVEL", "INDUSTRIA", "NACIONAL", "LABIRINTO", "LOBISOMEM",
"LOCOMOTIVA", "TESOURA", "MASSAGISTA", "MATADOURO", "MOCHILA", "NOROESTE",
"NITROGENIO", "ÔNIBUS", "CASTANHA", "SOFTWARE", "ENGENHARIA",
"NOROESTE", "AUTENTICO", "LINUX", "PROCESSAR", "QUARENTENA", "MEDICINA",
"HOLOCAUSTO", "RÁDIO", "XAROPE", "ZAROLHO", "ZOOLOGICO", "BESOURO",
"EXTASE", "EXTRAVIO", "DUODENO", "ECOLOGISTA", "TURISMO", "TRAFICANTE",
"CONSELHO", "BAIXISTA", "AVESTRUZ", "QUIMICA", "BOTANICA", "RESPECTIVO",
"SAXOFONE", "TABERNA", "OCULTO", "TRIÂNGULO", "ZODIACO", "JUSTAPOSTO",
"HIDRAULICO", "HEXAGONO", "MINEIRO", "FRENETICO", "EXPLOSIVEL", "EXORCISTA"};
private String letra;
String p;
private static int i;
int conta_letras = 0;
int Tentativas_erradas;
int qt;
private JPanel pn_letra1, pn_letra2, pn_letra3, pn_letra4, pn_letra5, pn_letra6, pn_letra7, pn_letra8, pn_letra9, pn_letra10;
private JLabel lb_letra1, lb_letra2, lb_letra3, lb_letra4, lb_letra5, lb_letra6, lb_letra7, lb_letra8, lb_letra9, lb_letra10;
private JLabel lb_num_letras;
private JLabel lb_nletras;
private JLabel lb_forca, lb_cabeca, lb_tronco, lb_bracoe, lb_bracod, lb_pernae, lb_pernad;
public JLabel lb_contador;
private JButton bt_novo_jogo;
private JButton bt_a,bt_b,bt_c,bt_d,bt_e,bt_f,bt_g,bt_h,bt_i,bt_j,bt_k,bt_l,bt_m,
bt_n,bt_o,bt_p,bt_q,bt_r,bt_s,bt_t,bt_u,bt_v,bt_w,bt_x,bt_y,bt_z;
private ImageIcon imagem_forca;
private ImageIcon imagem_cabeca;
private ImageIcon imagem_tronco;
private ImageIcon imagem_bracoe;
private ImageIcon imagem_bracod;
private ImageIcon imagem_pernae;
private ImageIcon imagem_pernad;
public Forca ()
{
Componentes();
Sorteia_palavra(p);
Conta_letras(p);
}
public void Componentes()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Jogo da Forca");
setSize(800, 500);
setLocation(75, 75);
setResizable(true);
getContentPane().setLayout(null);
getContentPane().setBackground(new Color(150, 100, 0));
pn_letra1 = new JPanel();
pn_letra2 = new JPanel();
pn_letra3 = new JPanel();
pn_letra4 = new JPanel();
pn_letra5 = new JPanel();
pn_letra6 = new JPanel();
pn_letra7 = new JPanel();
pn_letra8 = new JPanel();
pn_letra9 = new JPanel();
pn_letra10 = new JPanel();
lb_letra1 = new JLabel();
lb_letra2 = new JLabel();
lb_letra3 = new JLabel();
lb_letra4 = new JLabel();
lb_letra5 = new JLabel();
lb_letra6 = new JLabel();
lb_letra7 = new JLabel();
lb_letra8 = new JLabel();
lb_letra9 = new JLabel();
lb_letra10 = new JLabel();
lb_num_letras = new JLabel("");
lb_nletras = new JLabel("Número de letras");
lb_contador = new JLabel();
imagem_forca = new ImageIcon("C:/Documents and Settings/Administrador/Meus documentos/Jhonatam/Análise de sistemas/Linguagem de programação IV/Jogo da forca/Forca.jpg");
imagem_cabeca = new ImageIcon("C:/Documents and Settings/Administrador/Meus documentos/Jhonatam/Análise de sistemas/Linguagem de programação IV/Jogo da forca/shrek_parte_1.jpg");
imagem_tronco = new ImageIcon("C:/Documents and Settings/Administrador/Meus documentos/Jhonatam/Análise de sistemas/Linguagem de programação IV/Jogo da forca/shrek_parte_2.jpg");
imagem_bracoe = new ImageIcon("C:/Documents and Settings/Administrador/Meus documentos/Jhonatam/Análise de sistemas/Linguagem de programação IV/Jogo da forca/shrek_parte_3.jpg");
imagem_bracod = new ImageIcon("C:/Documents and Settings/Administrador/Meus documentos/Jhonatam/Análise de sistemas/Linguagem de programação IV/Jogo da forca/shrek_parte_4.jpg");
imagem_pernae = new ImageIcon("C:/Documents and Settings/Administrador/Meus documentos/Jhonatam/Análise de sistemas/Linguagem de programação IV/Jogo da forca/shrek_parte_5.jpg");
imagem_pernad = new ImageIcon("C:/Documents and Settings/Administrador/Meus documentos/Jhonatam/Análise de sistemas/Linguagem de programação IV/Jogo da forca/shrek_parte_6.jpg");
lb_forca = new JLabel(imagem_forca);
lb_cabeca = new JLabel(imagem_cabeca);
lb_tronco = new JLabel(imagem_tronco);
lb_bracoe = new JLabel(imagem_bracoe);
lb_bracod = new JLabel(imagem_bracod);
lb_pernae = new JLabel(imagem_pernae);
lb_pernad = new JLabel(imagem_pernad);
bt_novo_jogo = new JButton("Novo");
bt_a = new JButton("A");
bt_b = new JButton("B");
bt_c = new JButton("C");
bt_d = new JButton("D");
bt_e = new JButton("E");
bt_f = new JButton("F");
bt_g = new JButton("G");
bt_h = new JButton("H");
bt_i = new JButton("I");
bt_j = new JButton("J");
bt_k = new JButton("K");
bt_l = new JButton("L");
bt_m = new JButton("M");
bt_n = new JButton("N");
bt_o = new JButton("O");
bt_p = new JButton("P");
bt_q = new JButton("Q");
bt_r = new JButton("R");
bt_s = new JButton("S");
bt_t = new JButton("T");
bt_u = new JButton("U");
bt_v = new JButton("V");
bt_w = new JButton("W");
bt_x = new JButton("X");
bt_y = new JButton("Y");
bt_z = new JButton("Z");
getContentPane().add(pn_letra1);
getContentPane().add(pn_letra2);
getContentPane().add(pn_letra3);
getContentPane().add(pn_letra4);
getContentPane().add(pn_letra5);
getContentPane().add(pn_letra6);
getContentPane().add(pn_letra7);
getContentPane().add(pn_letra8);
getContentPane().add(pn_letra9);
getContentPane().add(pn_letra10);
getContentPane().add(lb_letra1);
getContentPane().add(lb_letra2);
getContentPane().add(lb_letra3);
getContentPane().add(lb_letra4);
getContentPane().add(lb_letra5);
getContentPane().add(lb_letra6);
getContentPane().add(lb_letra7);
getContentPane().add(lb_letra8);
getContentPane().add(lb_letra9);
getContentPane().add(lb_letra10);
getContentPane().add(lb_num_letras);
getContentPane().add(lb_nletras);
getContentPane().add(lb_contador);
getContentPane().add(lb_forca);
getContentPane().add(lb_cabeca);
getContentPane().add(lb_tronco);
getContentPane().add(lb_bracoe);
getContentPane().add(lb_bracod);
getContentPane().add(lb_pernae);
getContentPane().add(lb_pernad);
getContentPane().add(bt_novo_jogo);
getContentPane().add(bt_a);
getContentPane().add(bt_b);
getContentPane().add(bt_c);
getContentPane().add(bt_d);
getContentPane().add(bt_e);
getContentPane().add(bt_f);
getContentPane().add(bt_g);
getContentPane().add(bt_h);
getContentPane().add(bt_i);
getContentPane().add(bt_j);
getContentPane().add(bt_k);
getContentPane().add(bt_l);
getContentPane().add(bt_m);
getContentPane().add(bt_n);
getContentPane().add(bt_o);
getContentPane().add(bt_p);
getContentPane().add(bt_q);
getContentPane().add(bt_r);
getContentPane().add(bt_s);
getContentPane().add(bt_t);
getContentPane().add(bt_u);
getContentPane().add(bt_w);
getContentPane().add(bt_v);
getContentPane().add(bt_x);
getContentPane().add(bt_y);
getContentPane().add(bt_z);
pn_letra1.setBounds(100, 60, 50, 5);
pn_letra2.setBounds(160, 60, 50, 5);
pn_letra3.setBounds(220, 60, 50, 5);
pn_letra4.setBounds(280, 60, 50, 5);
pn_letra5.setBounds(340, 60, 50, 5);
pn_letra6.setBounds(400, 60, 50, 5);
pn_letra7.setBounds(460, 60, 50, 5);
pn_letra8.setBounds(520, 60, 50, 5);
pn_letra9.setBounds(580, 60, 50, 5);
pn_letra10.setBounds(640, 60, 50, 5);
lb_letra1.setBounds(104, 10, 50, 50);
lb_letra2.setBounds(164, 10, 50, 50);
lb_letra3.setBounds(224, 10, 50, 50);
lb_letra4.setBounds(284, 10, 50, 50);
lb_letra5.setBounds(344, 10, 50, 50);
lb_letra6.setBounds(404, 10, 50, 50);
lb_letra7.setBounds(464, 10, 50, 50);
lb_letra8.setBounds(524, 10, 50, 50);
lb_letra9.setBounds(584, 10, 50, 50);
lb_letra10.setBounds(644, 10, 50, 50);
lb_num_letras.setBounds(55,170,100,100);
lb_nletras.setBounds(30,165,100,20);
lb_contador.setBounds(30,200,100,200);
lb_forca.setBounds(1, 10, 800, 400);
lb_cabeca.setBounds(1, 10, 800, 400);
lb_tronco.setBounds(1, 10, 800, 400);
lb_bracoe.setBounds(1, 10, 800, 400);
lb_bracod.setBounds(1, 10, 800, 400);
lb_pernae.setBounds(1, 10, 800, 400);
lb_pernad.setBounds(1, 10, 800, 400);
bt_novo_jogo.setBounds (670, 170, 100, 100);
bt_a.setBounds (100, 360, 46, 30);
bt_b.setBounds (147, 360, 46, 30);
bt_c.setBounds (194, 360, 46, 30);
bt_d.setBounds (241, 360, 46, 30);
bt_e.setBounds (288, 360, 46, 30);
bt_f.setBounds (335, 360, 46, 30);
bt_g.setBounds (382, 360, 46, 30);
bt_h.setBounds (429, 360, 46, 30);
bt_i.setBounds (476, 360, 46, 30);
bt_j.setBounds (523, 360, 46, 30);
bt_k.setBounds (570, 360, 46, 30);
bt_l.setBounds (617, 360, 46, 30);
bt_m.setBounds (664, 360, 46, 30);
bt_n.setBounds (100, 400, 46, 30);
bt_o.setBounds (147, 400, 46, 30);
bt_p.setBounds (194, 400, 46, 30);
bt_q.setBounds (241, 400, 46, 30);
bt_r.setBounds (288, 400, 46, 30);
bt_s.setBounds (335, 400, 46, 30);
bt_t.setBounds (382, 400, 46, 30);
bt_u.setBounds (429, 400, 46, 30);
bt_v.setBounds (476, 400, 46, 30);
bt_w.setBounds (522, 400, 47, 30);
bt_x.setBounds (570, 400, 46, 30);
bt_y.setBounds (617, 400, 46, 30);
bt_z.setBounds (664, 400, 46, 30);
lb_num_letras.setForeground(Color.yellow);
lb_letra1.setForeground(Color.white);
lb_letra2.setForeground(Color.white);
lb_letra3.setForeground(Color.white);
lb_letra4.setForeground(Color.white);
lb_letra5.setForeground(Color.white);
lb_letra6.setForeground(Color.white);
lb_letra7.setForeground(Color.white);
lb_letra8.setForeground(Color.white);
lb_letra9.setForeground(Color.white);
lb_letra10.setForeground(Color.white);
bt_novo_jogo.setBackground(Color.green);
bt_novo_jogo.addActionListener(this);
bt_a.addActionListener(this);
bt_b.addActionListener(this);
bt_c.addActionListener(this);
bt_d.addActionListener(this);
bt_e.addActionListener(this);
bt_f.addActionListener(this);
bt_g.addActionListener(this);
bt_h.addActionListener(this);
bt_i.addActionListener(this);
bt_j.addActionListener(this);
bt_k.addActionListener(this);
bt_l.addActionListener(this);
bt_m.addActionListener(this);
bt_n.addActionListener(this);
bt_o.addActionListener(this);
bt_p.addActionListener(this);
bt_q.addActionListener(this);
bt_r.addActionListener(this);
bt_s.addActionListener(this);
bt_t.addActionListener(this);
bt_u.addActionListener(this);
bt_w.addActionListener(this);
bt_v.addActionListener(this);
bt_x.addActionListener(this);
bt_y.addActionListener(this);
bt_z.addActionListener(this);
pn_letra1.setVisible(true);
pn_letra2.setVisible(true);
pn_letra3.setVisible(true);
pn_letra4.setVisible(true);
pn_letra5.setVisible(true);
pn_letra6.setVisible(true);
pn_letra7.setVisible(true);
pn_letra8.setVisible(true);
pn_letra9.setVisible(true);
pn_letra10.setVisible(true);
lb_cabeca.setVisible(false);
lb_tronco.setVisible(false);
lb_bracoe.setVisible(false);
lb_bracod.setVisible(false);
lb_pernae.setVisible(false);
lb_pernad.setVisible(false);
lb_letra1 .setFont(new Font("Arial",Font.BOLD,50));
lb_letra2 .setFont(new Font("Arial",Font.BOLD,50));
lb_letra3 .setFont(new Font("Arial",Font.BOLD,50));
lb_letra4 .setFont(new Font("Arial",Font.BOLD,50));
lb_letra5 .setFont(new Font("Arial",Font.BOLD,50));
lb_letra6 .setFont(new Font("Arial",Font.BOLD,50));
lb_letra7 .setFont(new Font("Arial",Font.BOLD,50));
lb_letra8 .setFont(new Font("Arial",Font.BOLD,50));
lb_letra9 .setFont(new Font("Arial",Font.BOLD,50));
lb_letra10 .setFont(new Font("Arial",Font.BOLD,50));
lb_num_letras.setFont(new Font("Arial",Font.BOLD,50));
lb_contador .setFont(new Font("Arial",Font.BOLD,12));
}
public static void main (String args[])
{
Forca jogo_da_forca = new Forca();
jogo_da_forca.show(true);
}
public void Novo_jogo()
{
Forca jogo = new Forca();
this.setVisible(false);
jogo.setVisible(true);
}
public void Mostra_letra (String letra,String p)
{
boolean acertou = false;
this.qt = p.length();
for ( i = 0;i < qt; i++)
{
p.charAt(i);
char let = p.charAt(i);
String troca = Character.toString(let);
if (troca.equals(letra))
{
this.conta_letras++;
if (i == 0)
{
acertou = true;
lb_letra1.setText(letra);
}
else if (i == 1)
{
acertou = true;
lb_letra2.setText(letra);
}
else if (i == 2)
{
acertou = true;
lb_letra3.setText(letra);
}
else if (i == 3)
{
acertou = true;
lb_letra4.setText(letra);
}
else if (i == 4)
{
acertou = true;
lb_letra5.setText(letra);
}
else if (i == 5)
{
acertou = true;
lb_letra6.setText(letra);
}
else if (i == 6)
{
acertou = true;
lb_letra7.setText(letra);
}
else if (i == 7)
{
acertou = true;
lb_letra8.setText(letra);
}
else if (i == 8)
{
acertou = true;
lb_letra9.setText(letra);
}
else if (i == 9)
{
acertou = true;
lb_letra10.setText(letra);
}
}
}
if (conta_letras == qt)
{
JOptionPane.showMessageDialog(null,"!!! PARABÉNS !!!\n\nVocê ganhou");
TelaInicial inicio = new TelaInicial();
this.setVisible(false);
inicio.setVisible(true);
}
if (acertou == false)
{
this.Tentativas_erradas ++;
Mostra_boneco();
}
}
public void Conta_letras(String p)
{
qt = p.length();
String n = qt + "";
GregorianCalendar day = new GregorianCalendar();
String time = day.getTime().toString();
lb_contador.setText(time);
lb_num_letras.setText(n);
switch(qt)
{
case 9:
pn_letra10.setVisible(false);
break;
case 8:
pn_letra10.setVisible(false);
pn_letra9 .setVisible(false);
break;
case 7:
pn_letra10.setVisible(false);
pn_letra9 .setVisible(false);
pn_letra8 .setVisible(false);
break;
case 6:
pn_letra10.setVisible(false);
pn_letra9 .setVisible(false);
pn_letra8 .setVisible(false);
pn_letra7 .setVisible(false);
break;
case 5:
pn_letra10.setVisible(false);
pn_letra9 .setVisible(false);
pn_letra8 .setVisible(false);
pn_letra7 .setVisible(false);
pn_letra6 .setVisible(false);
break;
case 4:
pn_letra10.setVisible(false);
pn_letra9 .setVisible(false);
pn_letra8 .setVisible(false);
pn_letra7 .setVisible(false);
pn_letra6 .setVisible(false);
pn_letra5 .setVisible(false);
}
}
public void Mostra_boneco()
{
if(Tentativas_erradas > 0)
lb_forca.setVisible(false);
lb_cabeca.setVisible(true);
if(Tentativas_erradas > 1)
{
lb_cabeca.setVisible(false);
lb_tronco.setVisible(true);
}
if(Tentativas_erradas > 2)
lb_tronco.setVisible(false);
lb_bracoe.setVisible(true);
if(Tentativas_erradas > 3)
lb_bracoe.setVisible(false);
lb_bracod.setVisible(true);
if(Tentativas_erradas > 4)
lb_bracod.setVisible(false);
lb_pernae.setVisible(true);
if(Tentativas_erradas > 5)
{
lb_pernae.setVisible(false);
lb_pernad.setVisible(true);
JOptionPane.showConfirmDialog(null,"VOCÊ PERDEU!\n\n " +
"A palavra certa seria '"+p+"'\n\n" +
"Tente novamente.");
TelaInicial inicio = new TelaInicial();
this.setVisible(false);
inicio.setVisible(true);
}
}
public char[] Sorteia_palavra(String p) {
int indicePalavra;
String palavraSorteada;
indicePalavra = (int) (Math.random() * listaPalavras.length);
palavraSorteada = listaPalavras[indicePalavra];
this.p = palavraSorteada;
return palavraSorteada.toCharArray();
}
public void actionPerformed(ActionEvent acao) {
if(acao.getSource() == bt_novo_jogo)
{
Novo_jogo();
Sorteia_palavra(p);
Conta_letras(p);
}
else if(acao.getSource() == bt_a)
{
bt_a.show(false);
letra = "A";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_b)
{
bt_b.show(false);
letra = "B";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_c)
{
bt_c.show(false);
letra = "C";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_d)
{
bt_d.show(false);
letra = "D";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_e)
{
bt_e.show(false);
letra = "E";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_f)
{
bt_f.show(false);
letra = "F";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_g)
{
bt_g.show(false);
letra = "G";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_h)
{
bt_h.show(false);
letra = "H";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_i)
{
bt_i.show(false);
letra = "I";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_j)
{
bt_j.show(false);
letra = "J";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_k)
{
bt_k.show(false);
letra = "K";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_l)
{
bt_l.show(false);
letra = "L";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_m)
{
bt_m.show(false);
letra = "M";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_n)
{
bt_n.show(false);
letra = "N";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_o)
{
bt_o.show(false);
letra = "O";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_p)
{
bt_p.show(false);
letra = "P";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_q)
{
bt_q.show(false);
letra = "Q";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_r)
{
bt_r.show(false);
letra = "R";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_s)
{
bt_s.show(false);
letra = "S";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_t)
{
bt_t.show(false);
letra = "T";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_u)
{
bt_u.show(false);
letra = "U";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_w)
{
bt_w.show(false);
letra = "W";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_v)
{
bt_v.show(false);
letra = "V";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_x)
{
bt_x.show(false);
letra = "X";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_y)
{
bt_y.show(false);
letra = "Y";
Mostra_letra(letra,p);
}
else if(acao.getSource() == bt_z)
{
bt_z.show(false);
letra = "Z";
Mostra_letra(letra,p);
}
}
}