Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.JComponent;
- import javax.swing.Timer;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Random;
- import java.util.concurrent.DelayQueue;
- import javax.swing.JOptionPane;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Dušan
- */
- public class obesenec extends JComponent implements KeyListener{
- //Timer t;
- int stav = -1;
- int stlacena_klavesa;
- boolean dobry_klaves;
- String dobre_klavesy ="";
- boolean zly_klaves;
- char pismeno;
- boolean uhadol = false;
- String slovo;
- public obesenec(){
- setBounds(1,1,500,500);
- // t = new Timer(1000, (ActionListener) this);
- // t.start();
- addKeyListener(this);
- setFocusable(true);
- citaj();//prva vec je, ze si nacita prve slovo a potom ide dalej.
- }
- @Override
- public void paintComponent(Graphics g) { //vykreslenie . dalej sa riesi cez switch
- super.paintComponent(g);
- g.setColor(Color.DARK_GRAY);
- g.fillRect(10, 200, 100, 7);
- g.fillRect(55, 45, 7, 155);
- g.fillRect(55, 45, 100, 7);
- g.drawLine(55+7, 55+7, 70, 45+7);
- g.drawLine(55+7,56+7,71,45+7);
- g.drawLine(55+7, 57+7, 72, 45+7);
- g.drawLine(55+7, 58+7, 73, 45+7);
- g.drawLine(55+7, 59+7, 74, 45+7);
- g.drawLine(55+7, 60+7, 75, 45+7);
- g.drawLine(55+7, 61+7, 76, 45+7);
- g.drawLine(55+7, 62+7, 77, 45+7);
- switch(stav){
- case 0:
- g.fillRect(140, 45, 4, 30);
- repaint();
- break;
- case 1:
- g.fillRect(140, 45, 4, 30);
- g.drawOval(140-12, 75, 26, 26);
- repaint();
- break;
- case 2:
- g.fillRect(140, 45, 4, 30);
- g.drawOval(140-12, 75, 26, 26);
- g.drawLine(142, 75+26, 142,101+55 );
- repaint();
- break;
- case 3:
- g.fillRect(140, 45, 4, 30);
- g.drawOval(140-12, 75, 26, 26);
- g.drawLine(142, 75+26, 142,101+55 );
- g.drawLine(142, 119, 160, 101);
- repaint();
- break;
- case 4:
- g.fillRect(140, 45, 4, 30);
- g.drawOval(140-12, 75, 26, 26);
- g.drawLine(142, 75+26, 142,101+55 );
- g.drawLine(142, 119, 160, 101);
- g.drawLine(142, 119, 142-18, 101);
- repaint();
- break;
- case 5:
- g.fillRect(140, 45, 4, 30);
- g.drawOval(140-12, 75, 26, 26);
- g.drawLine(142, 75+26, 142,101+55 );
- g.drawLine(142, 119, 160, 101);
- g.drawLine(142, 119, 142-18, 101);
- g.drawLine(142, 156, 142+23, 156+15);
- repaint();
- break;
- case 6:
- g.fillRect(140, 45, 4, 30);
- g.drawOval(140-12, 75, 26, 26);
- g.drawLine(142, 75+26, 142,101+55 );
- g.drawLine(142, 119, 160, 101);
- g.drawLine(142, 119, 142-18, 101);
- g.drawLine(142, 156, 142+23, 156+15);
- g.drawLine(142, 156, 142-23, 156+15);
- repaint();
- break;
- }
- }
- /*int skore(int skore){
- return skore;
- }*/
- public void porovnaj (){ //porovnáva pismeno so slovom
- // hladaj v slovo pismeno. co znamena
- if(slovo.indexOf(pismeno)!=-1){ // podmienka na kontrolu, ci sa nachadza pismeno v slove
- System.out.println("ano!!"); // vypisovanie, ci sa nachadza pismeno v slove
- dobre_klavesy += pismeno; // priradenie dobrych klaves
- }
- else { // ak sa neneachadza pismeno v slove
- System.out.println("nie"); //vypis
- stav++; // zvysenie stavu - prida sa hlava, ruka , noha atd...
- repaint();// prekreslenie. do istoty
- }
- if (stav==6) { // podmienka ak sa vykreslil cely panacik, tak pouzivatel prehral
- JOptionPane.showMessageDialog(this, "Prehral si!"); //vypis
- stav = -1; //stav sa nahodi na -1 pretoze case 0 uz je ze stratil pokus
- repaint(); // prekreslenie do istoty
- }
- System.out.println("dobre klavesy su "+dobre_klavesy);// vypis pre mna, ktore su dobre klavesy. vo finalnej verzii to nebude.
- //if(dobre_klavesy.equals(slovo)){
- // uhadol = true;
- // JOptionPane.showMessageDialog(this, "Uhádol si slovo !");
- //}
- int pocet = dobre_klavesy.length();
- int cislovanie = 0;
- // tu potrebujem dat podmienku, aby sa zistovalo, ci dobre_klavesy==slovo.
- // problem je v tom, ze ono potrebuje dat true aj ked je napriklad AUTO==AOUT pretoze je to to iste, len v inom poradi.
- JOptionPane.showMessageDialog(this, "Uhádol si slovo !"); //vypis
- }
- public void citaj(){ //citanie zo suboru. prve slovo je AUTO
- try{
- BufferedReader subor = new BufferedReader
- (new FileReader("slova.txt"));
- StringBuilder sb = new StringBuilder();
- String riadok = subor.readLine();
- slovo = riadok;
- while(riadok != null && uhadol == true){
- sb.append(riadok);
- sb.append(System.lineSeparator());
- riadok = subor.readLine();
- }
- String vsetko = sb.toString();
- System.out.println(riadok);
- }
- catch(IOException e){
- e.printStackTrace();
- }
- }
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- }
- @Override
- public void keyReleased(KeyEvent e) { //zisti aky je releasnuty klaves a priradi mi ho do premennej pismeno.
- stlacena_klavesa = e.getKeyCode();
- pismeno = (char)stlacena_klavesa;
- System.out.println("Klavesa "+ pismeno);// vypise mi ho aby som si to vedel odkontrolovat
- porovnaj(); // potom zacne porovnavat
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement