import java.lang.Math.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Ahorcado extends JFrame implements ActionListener, KeyListener, MouseListener{
JComboBox combo1;
JLabel label1, label2, label3;
JTextField cuadro;
JButton enviar;
Container f;
int cont=0;
int tamano;
char letra;
boolean bandera=false;
boolean bandera2=false;
String s="";
char[] palabra_secreta=new char[10];
char[] palabra=new char[10];
private String [][] lista = {{"pera", "pina", "limon", "sandia","jitomate", "manzana", "naranja", "mandarina", "fresa"},
{"gato", "perro", "ardilla", "pez", "pajaro", "lombris", "zorra", "elefante", "leon"}};
String cadena;
public Ahorcado(){
super("Ahorcado");
this.setBounds(0,0,800,500);
f=getContentPane();
f.setLayout(null);
label1 =new JLabel("Categoria");
label2 =new JLabel("");
label3=new JLabel("");
combo1=new JComboBox();
cuadro=new JTextField();
enviar=new JButton("Iniciar");
label1.setBounds(0,0,100,30);
combo1.setBounds(0,50,100,30);
cuadro.setBounds(0,100,200,30);
enviar.setBounds(0,150,100,30);
label2.setBounds(200,20,50,30);
label3.setBounds(100,300,100,100);
combo1.addItem("Frutas");
combo1.addItem("Animales");
//combo1.setModel(new DefaultComboBoxModel(new String[] { "Seleccione una opción....", "Frutas", "Animales"}));
f.add(label1);f.add(combo1);
f.add(cuadro);f.add(enviar);
f.add(label2);f.add(label3);
cuadro.setEditable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
enviar.addActionListener(this);
this.addKeyListener( this );
this.addMouseListener(this);
}
public void actionPerformed(ActionEvent e){
cadena=null;
cont=0;
cuadro.setText("");
label2.setText("");
s="";
repaint();
for(int i=0;i<=palabra.length-1;i++){
palabra[i]=' ';
palabra_secreta[i]=' ';
}
String s2=combo1.getSelectedItem().toString();
int x=(int)(Math.random()*9);
if(s2.equals("Frutas")){
cadena=lista[0][x];
tamano=cadena.length()-1;
for(int i=0;i<=tamano;i++){
palabra_secreta[i]=cadena.charAt(i);
}
for(int i=0;i<=tamano;i++)
s=s+" "+"_";
}
if(s2.equals("Animales")){
cadena=lista[1][x];
tamano=cadena.length()-1;
for(int i=0;i<=tamano;i++){
palabra_secreta[i]=cadena.charAt(i);
}
for(int i=0;i<=tamano;i++)
s=s+" "+"_";
}
for(int i=0;i<=tamano;i++){
palabra[i]=s.charAt(i);
}
cuadro.setText(s);
}
public void compara(char x){
cuadro.setText("");
s="";
if(cont==5){
cuadro.setText("");
s="";
JOptionPane.showMessageDialog(null,"Acabas de perder");
}
for(int j=0;j<=palabra_secreta.length-1;j++){
if(palabra_secreta[j]==x){
palabra[j]= x;
bandera=true;
}
s=s+" "+palabra[j];
}
if(bandera==false){
cont++;
label2.setText(Integer.toString(cont));
}
cuadro.setText(s);
bandera=false;
repaint();
gano();
}
private void gano(){
boolean win=false;
for(int i=0;i<=this.palabra_secreta.length-1;i++){
if(palabra[i]==palabra_secreta[i]){
win=true;
}else{
win=false;
break;
}
}
if(win){
JOptionPane.showMessageDialog(null,"GANASTE CAMPEON");
}
}
public void keyPressed(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
letra=e.getKeyChar();
String l=Character.toString(letra);
label3.setFont(new Font("Arial", 1, 40));
label3.setText(l);
compara(letra);
}
public boolean isFocusable(){
return true;
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){
this.requestFocus();
}
public void mouseEntered(MouseEvent e){
this.requestFocus();
}
public void mouseExited(MouseEvent e){}
public void paint(Graphics g){
super.paint(g);
//x,y,ancho,alto
if(cont==0){
g.drawLine(400,400,400,100);
g.drawLine(400,100,550,100);
g.drawLine(550,150,550,100);
}
if(cont==1){
g.drawLine(400,400,400,100);
g.drawLine(400,100,550,100);
g.drawLine(550,150,550,100);
g.drawOval(500,150,100,100);
}
if(cont==2){
g.drawLine(400,400,400,100);
g.drawLine(400,100,550,100);
g.drawLine(550,150,550,100);
g.drawOval(500,150,100,100);
g.drawLine(550,350,550,250);
}
if(cont==3){
g.drawLine(400,400,400,100);
g.drawLine(400,100,550,100);
g.drawLine(550,150,550,100);
g.drawOval(500,150,100,100);
g.drawLine(650,270,550,250);
g.drawLine(450,270,550,250);
g.drawLine(550,350,550,250);
}
if(cont==4){
g.drawLine(400,400,400,100);
g.drawLine(400,100,550,100);
g.drawLine(550,150,550,100);
g.drawOval(500,150,100,100);
g.drawLine(650,270,550,250);
g.drawLine(450,270,550,250);
g.drawLine(550,350,550,250);
g.drawLine(600,420,550,320);
g.drawLine(500,420,550,320);
}
if(cont==5){
g.drawLine(400,400,400,100);
g.drawLine(400,100,550,100);
g.drawLine(550,150,550,100);
g.drawOval(500,150,100,100);
g.drawLine(650,270,550,250);
g.drawLine(450,270,550,250);
g.drawLine(550,350,550,250);
g.drawLine(600,420,550,320);
g.drawLine(500,420,550,320);
}
}
public static void main (String Args[]){
Ahorcado a=new Ahorcado();
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.