Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. package wisielec;
  2.  
  3. import java.io.IOException;
  4. import java.util.Random;
  5. import javax.microedition.io.Connector;
  6. import javax.microedition.io.file.FileConnection;
  7. import javax.microedition.lcdui.Command;
  8. import javax.microedition.lcdui.CommandListener;
  9. import javax.microedition.lcdui.Display;
  10. import javax.microedition.lcdui.Displayable;
  11. import javax.microedition.lcdui.Form;
  12. import javax.microedition.lcdui.Image;
  13. import javax.microedition.lcdui.ImageItem;
  14. import javax.microedition.lcdui.StringItem;
  15. import javax.microedition.lcdui.TextBox;
  16. import javax.microedition.lcdui.TextField;
  17. import javax.microedition.midlet.*;
  18.  
  19. /**
  20.  * @author Let
  21.  */
  22. public class Midlet extends MIDlet implements CommandListener {
  23.  
  24.     private Form okno;
  25.     private Command cmdKoniec;
  26.     private Command cmdNext;
  27.     static private TextField tf;
  28.     private String tmp ;
  29.     private StringItem slowo;
  30.     private StringItem proby;
  31.     static int proba = 0;
  32.     String []images ={"/0.png","/0.png","/1.png","/2.png","/3.png","/4.png","/5.png","/6.png","/7.png","/8.png","/9.png","/10.png"};
  33.  
  34.     private ImageItem imageItem;
  35.     private Image image;
  36.    
  37.     Random rand = new Random();
  38.     static int tab [] ;
  39.     String [] slowa = {"Komputer","Java","Aplikacje"};
  40.      
  41.     String checkWorld(String word) throws IOException{
  42.         String hideword = "";
  43.         boolean powodzenie = false;
  44.  
  45.         int length = word.length();
  46.         for (int i =0;i<length;i++){
  47.             if(tf.getString().toUpperCase().equals(String.valueOf(word.charAt(i)).toUpperCase())){
  48.                 tab[i]=1;
  49.                 powodzenie = true;
  50.             }                
  51.             if(tab[i]==1){
  52.                 hideword += " "+String.valueOf(word.charAt(i));
  53.             }else{
  54.             hideword += " _";
  55.             }
  56.         }      
  57.         if(!powodzenie){
  58.             proba++;            
  59.            image = Image.createImage(Image.createImage(images[proba]),0,0,150,150,0);
  60.           // image = Image.createImage(images[proba]);
  61.            imageItem.setImage(image);  
  62.         }
  63.         return hideword;
  64.     }
  65.    
  66.     public Midlet() throws IOException{
  67.        
  68.         image = Image.createImage(Image.createImage(images[0]),0,0,150,150,0);
  69.         imageItem = new ImageItem(null, image,ImageItem.LAYOUT_CENTER,"");
  70.         tmp = slowa[rand.nextInt(2)];
  71.         tab = new int [tmp.length()];
  72.         tf= new TextField ("Podaj litere :",null,1,TextField.ANY);
  73.         slowo = new StringItem("Szukane słowo: ",checkWorld(tmp));
  74.         proby = new StringItem("Próba nr:",String.valueOf(proba));
  75.         okno = new Form ("Wisielec");
  76.         Display ekran = Display.getDisplay(this);
  77.         ekran.setCurrent(okno);
  78.         cmdKoniec = new Command("Koniec",Command.EXIT,0);
  79.         cmdNext = new Command("Następna próba",Command.ITEM,0);
  80.              
  81.     }
  82.  
  83.    
  84.     public void startApp() {
  85.         for (int i =0;i<tmp.length();i++){
  86.             tab[i] = 0;
  87.         }
  88.         Display ekran = Display.getDisplay(this);
  89.         ekran.setCurrent(okno);
  90.         okno.addCommand(cmdKoniec);
  91.         okno.addCommand(cmdNext);
  92.         okno.setCommandListener(this);
  93.         okno.append(slowo);
  94.         okno.append(tf);
  95.         okno.append(proby);    
  96.         okno.append(imageItem);
  97.     }
  98.    
  99.     public void pauseApp() {
  100.     }
  101.    
  102.     public void destroyApp(boolean unconditional) {
  103.     }
  104.    
  105.     public void commandAction(Command cmd, Displayable s){
  106.         if(cmd == cmdNext){
  107.             proby.setText(String.valueOf(proba));
  108.             try {
  109.                 slowo.setText(checkWorld(tmp));
  110.             } catch (IOException ex) {
  111.                 ex.printStackTrace();
  112.             }
  113.              
  114.         }
  115.         if(cmd ==cmdKoniec){
  116.             notifyDestroyed();
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement