Advertisement
Guest User

Hangman

a guest
Apr 29th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.62 KB | None | 0 0
  1.  
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import javax.swing.JComponent;
  9. import javax.swing.Timer;
  10. import java.io.BufferedReader;
  11. import java.io.BufferedWriter;
  12. import java.io.FileReader;
  13. import java.io.FileWriter;
  14. import java.io.IOException;
  15. import java.util.Random;
  16. import java.util.concurrent.DelayQueue;
  17. import javax.swing.JOptionPane;
  18.  
  19. /*
  20.  * To change this license header, choose License Headers in Project Properties.
  21.  * To change this template file, choose Tools | Templates
  22.  * and open the template in the editor.
  23.  */
  24.  
  25. /**
  26.  *
  27.  * @author Dušan
  28.  */
  29. public class obesenec extends JComponent implements KeyListener{
  30.     //Timer t;
  31.     int stav = -1;
  32.     int stlacena_klavesa;
  33.     boolean dobry_klaves;
  34.     String dobre_klavesy ="";
  35.     boolean zly_klaves;
  36.     char pismeno;
  37.     boolean uhadol = false;
  38.     String slovo;
  39.     public obesenec(){
  40.         setBounds(1,1,500,500);
  41.        // t = new Timer(1000, (ActionListener) this);
  42.        // t.start();
  43.         addKeyListener(this);
  44.         setFocusable(true);
  45.         citaj();//prva vec je, ze si nacita prve slovo a potom ide dalej.
  46.     }
  47.     @Override
  48.     public void paintComponent(Graphics g) { //vykreslenie . dalej sa riesi cez switch
  49.         super.paintComponent(g);
  50.         g.setColor(Color.DARK_GRAY);
  51.         g.fillRect(10, 200, 100, 7);
  52.         g.fillRect(55, 45, 7, 155);
  53.         g.fillRect(55, 45, 100, 7);
  54.         g.drawLine(55+7, 55+7, 70, 45+7);
  55.         g.drawLine(55+7,56+7,71,45+7);
  56.         g.drawLine(55+7, 57+7, 72, 45+7);
  57.         g.drawLine(55+7, 58+7, 73, 45+7);
  58.         g.drawLine(55+7, 59+7, 74, 45+7);
  59.         g.drawLine(55+7, 60+7, 75, 45+7);
  60.         g.drawLine(55+7, 61+7, 76, 45+7);
  61.         g.drawLine(55+7, 62+7, 77, 45+7);
  62.         switch(stav){
  63.             case 0:
  64.                 g.fillRect(140, 45, 4, 30);
  65.                 repaint();
  66.                 break;
  67.             case 1:
  68.                 g.fillRect(140, 45, 4, 30);
  69.                 g.drawOval(140-12, 75, 26, 26);
  70.                 repaint();
  71.                 break;
  72.             case 2:
  73.                 g.fillRect(140, 45, 4, 30);
  74.                 g.drawOval(140-12, 75, 26, 26);
  75.                 g.drawLine(142, 75+26, 142,101+55 );
  76.                 repaint();
  77.                 break;
  78.             case 3:
  79.                 g.fillRect(140, 45, 4, 30);
  80.                 g.drawOval(140-12, 75, 26, 26);
  81.                 g.drawLine(142, 75+26, 142,101+55 );
  82.                 g.drawLine(142, 119, 160, 101);
  83.                 repaint();
  84.                 break;
  85.             case 4:
  86.                 g.fillRect(140, 45, 4, 30);
  87.                 g.drawOval(140-12, 75, 26, 26);
  88.                 g.drawLine(142, 75+26, 142,101+55 );
  89.                 g.drawLine(142, 119, 160, 101);
  90.                 g.drawLine(142, 119, 142-18, 101);
  91.                 repaint();
  92.                 break;
  93.             case 5:
  94.                 g.fillRect(140, 45, 4, 30);
  95.                 g.drawOval(140-12, 75, 26, 26);
  96.                 g.drawLine(142, 75+26, 142,101+55 );
  97.                 g.drawLine(142, 119, 160, 101);
  98.                 g.drawLine(142, 119, 142-18, 101);
  99.                 g.drawLine(142, 156, 142+23, 156+15);
  100.                 repaint();
  101.                 break;
  102.             case 6:
  103.                 g.fillRect(140, 45, 4, 30);
  104.                 g.drawOval(140-12, 75, 26, 26);
  105.                 g.drawLine(142, 75+26, 142,101+55 );
  106.                 g.drawLine(142, 119, 160, 101);
  107.                 g.drawLine(142, 119, 142-18, 101);
  108.                 g.drawLine(142, 156, 142+23, 156+15);
  109.                 g.drawLine(142, 156, 142-23, 156+15);
  110.                 repaint();
  111.                 break;
  112.         }  
  113.     }
  114.     /*int skore(int skore){
  115.         return skore;
  116.     }*/
  117.     public void porovnaj (){ //porovnáva pismeno so slovom
  118.         // hladaj v slovo pismeno. co znamena
  119.         if(slovo.indexOf(pismeno)!=-1){ // podmienka na kontrolu, ci sa nachadza pismeno v slove
  120.             System.out.println("ano!!"); // vypisovanie, ci sa nachadza pismeno v slove
  121.             dobre_klavesy += pismeno;   // priradenie dobrych klaves
  122.         }
  123.         else {  // ak sa neneachadza pismeno v slove
  124.             System.out.println("nie"); //vypis
  125.             stav++;     // zvysenie stavu -  prida sa hlava, ruka , noha atd...
  126.             repaint();// prekreslenie. do istoty
  127.             }
  128.         if (stav==6) {  // podmienka ak sa vykreslil cely panacik, tak pouzivatel prehral
  129.             JOptionPane.showMessageDialog(this, "Prehral si!"); //vypis
  130.             stav = -1;  //stav sa nahodi na -1 pretoze case 0 uz je ze stratil pokus
  131.             repaint();  // prekreslenie do istoty
  132.         }
  133.         System.out.println("dobre klavesy su "+dobre_klavesy);// vypis pre mna, ktore su dobre klavesy. vo finalnej verzii to nebude.
  134.         //if(dobre_klavesy.equals(slovo)){
  135.           //  uhadol = true;
  136.           //  JOptionPane.showMessageDialog(this, "Uhádol si slovo !");
  137.         //}
  138.         int pocet = dobre_klavesy.length();
  139.         int cislovanie = 0;
  140.         // tu potrebujem dat podmienku, aby  sa zistovalo, ci dobre_klavesy==slovo.
  141.         // problem je v tom, ze ono potrebuje dat true aj ked je napriklad AUTO==AOUT pretoze je to to iste, len v inom poradi.
  142.             JOptionPane.showMessageDialog(this, "Uhádol si slovo !"); //vypis
  143.            
  144.            
  145.            
  146.        
  147.     }
  148.    
  149.     public void citaj(){    //citanie zo suboru. prve slovo je AUTO    
  150.         try{
  151.             BufferedReader subor = new BufferedReader
  152.         (new FileReader("slova.txt"));
  153.             StringBuilder sb = new StringBuilder();
  154.             String riadok = subor.readLine();
  155.             slovo = riadok;
  156.             while(riadok != null && uhadol == true){
  157.                 sb.append(riadok);
  158.                 sb.append(System.lineSeparator());
  159.                 riadok = subor.readLine();
  160.             }
  161.             String vsetko = sb.toString();
  162.             System.out.println(riadok);
  163.     }
  164.         catch(IOException e){
  165.             e.printStackTrace();
  166.         }
  167.     }
  168.     @Override
  169.     public void keyTyped(KeyEvent e) {
  170.     }
  171.     @Override
  172.     public void keyPressed(KeyEvent e) {
  173.     }
  174.     @Override
  175.     public void keyReleased(KeyEvent e) { //zisti aky je releasnuty klaves a priradi mi ho do premennej pismeno.
  176.         stlacena_klavesa = e.getKeyCode();
  177.         pismeno = (char)stlacena_klavesa;
  178.         System.out.println("Klavesa "+ pismeno);// vypise mi ho aby som si to vedel odkontrolovat
  179.         porovnaj(); // potom zacne porovnavat
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement