martaczaska

Etap_1_final2

Jan 5th, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.imageio.ImageIO;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import java.awt.image.BufferedImage;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.Random;
  13. import java.util.Scanner;
  14. import java.util.concurrent.Flow;
  15. import java.util.ArrayList;
  16. import java.awt.List;
  17. import java.awt.event.KeyAdapter;
  18.  
  19. public class Etap_1 extends JPanel{
  20.  
  21.     public static Image[]kolo;
  22.     public String[] litery = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "R", "S", "T", "U", "W", "Y", "Z"};   //(23 ale od 0 do 22)
  23.     static public String wybrana_zol = "";
  24.     public String wybrana_kolor = "";
  25.  
  26.     public BufferedImage image_1;
  27.     public BufferedImage image_2;
  28.     public BufferedImage kolo_1;
  29.     public BufferedImage kolo_2;
  30.     public BufferedImage tlo_1;
  31.  
  32.     static JLabel label_timer = new JLabel();
  33.     static ArrayList<String> lista = new ArrayList<String>();                                 // RISKY SHIT
  34.     static JTextField jt = new JTextField();
  35.  
  36.     static boolean czy_wpisane = false;
  37.  
  38.     static StringBuilder sB = new StringBuilder();//1:57
  39.     static String sklejka ="";
  40.  
  41.  
  42.     public Etap_1(){
  43.         setLayout(null);
  44.  
  45.         Random losowa = new Random();
  46.  
  47.         int polozenie = losowa.nextInt(2);
  48.         int nr_litery_1 = losowa.nextInt(22);
  49.         int nr_litery_2 = losowa.nextInt(22);
  50.  
  51.         wybrana_zol = litery[nr_litery_1];
  52.         wybrana_kolor = litery[nr_litery_2];
  53.  
  54.         File zdjecie_1 = new File("zdjecia/zolte/" + wybrana_zol + ".png");
  55.         File zdjecie_2 = new File("zdjecia/kolorowe/" + wybrana_kolor + ".png");
  56.         File zdjecie_3 = new File("zdjecia/tlo_GRA_1.png");
  57.  
  58.         try{
  59.             image_1 = ImageIO.read(zdjecie_1);
  60.             image_2 = ImageIO.read(zdjecie_2);
  61.             tlo_1 = ImageIO.read(zdjecie_3);
  62.  
  63.         }
  64.         catch(IOException e){
  65.             System.err.println("Blad odczytu obrazków");
  66.         }
  67.  
  68.  
  69.         if(polozenie == 0){
  70.             kolo_1 = image_1;
  71.             kolo_2 = image_2;
  72.         }
  73.         else if(polozenie == 1){
  74.             kolo_1 = image_2;
  75.             kolo_2 = image_1;
  76.         }
  77.  
  78.         //JTextField jt = new JTextField();
  79.         jt.setBounds(320, 650, 370, 70);
  80.         add(jt);
  81.  
  82.         label_timer.setBounds(800, 85, 220, 50);
  83.         label_timer.setFont(new Font("Cambria", Font.BOLD, 35));
  84.         add(label_timer);
  85.  
  86.         jt.addKeyListener(new KeyAdapter(){
  87.             public void keyPressed(KeyEvent pr){
  88.                 if (pr.getKeyCode() == KeyEvent.VK_ENTER) {
  89.                     if (jt.getText().equals(wybrana_zol)) {
  90.                         lista.add(wybrana_zol);
  91.  
  92.                         sB.append(wybrana_zol);
  93.                         sB.append(" ");
  94.  
  95.  
  96.                         czy_wpisane = true;
  97.                         jt.setText("");
  98.  
  99.                         /*jt.setCaretPosition(0);
  100.                         int caretPosition = jt.getCaretPosition();
  101.                         jt.setCaretPosition(caretPosition);*/
  102.  
  103.                         Okno.karty.add((new Etap_1()), "etap_1");
  104.                         CardLayout CL_2 = (CardLayout) (Projekt.noweOkno.karty.getLayout());
  105.                         CL_2.show(Projekt.noweOkno.karty, "etap_1");
  106.                     } else {
  107.                         jt.setText("");
  108.                         //System.out.println("Wpisałeś złą literę");
  109.                     }
  110.  
  111.                 }
  112.             }
  113.         });
  114.  
  115.  
  116.         sklejka = sB.toString();
  117.         System.out.println(sklejka);
  118.     }
  119.  
  120.  
  121.     public void paintComponent(Graphics graphic) {
  122.         Graphics2D g2d = (Graphics2D) graphic;
  123.         g2d.drawImage(tlo_1, 0, 0, this);
  124.         g2d.drawImage(kolo_1, 100, 180, this);
  125.         g2d.drawImage(kolo_2, 600, 180, this);
  126.     }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment