martaczaska

Timer_chyba udany_bezifa

Jan 4th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. package com.company;
  2. import javax.imageio.ImageIO;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.util.Scanner;
  12. import java.util.Random;
  13. import java.util.TimerTask;
  14. import java.util.Timer;
  15.  
  16.  
  17. public class Poziomy extends JPanel implements ActionListener {
  18.     public int czas = 1;
  19.     public BufferedImage tlo_lvl;
  20.  
  21.     public int m = 100;
  22.     public int n = 200;
  23.  
  24.     public int j;
  25.  
  26.     JButton łatwy;
  27.     JButton sredni;
  28.     JButton trudny;
  29.  
  30.     Timer timer = new Timer();
  31.     TimerTask odlicz = new odliczanie();
  32.  
  33.  
  34.     public Poziomy(){
  35.         setLayout(null);
  36.  
  37.         File zdj_lvl = new File("zdjecia/tlo_menu.png");
  38.         try{ tlo_lvl = ImageIO.read(zdj_lvl); }
  39.         catch(IOException e){ System.err.println("Blad odczytu obrazków"); }
  40.  
  41.         łatwy = new JButton("łatwy");
  42.         sredni = new JButton("średni");
  43.         trudny = new JButton("trudny");
  44.  
  45.         łatwy.addActionListener(this);
  46.         sredni.addActionListener(this);
  47.         trudny.addActionListener(this);
  48.  
  49.         łatwy.setBounds(320, 100, 300, 80 );
  50.         sredni.setBounds(320, 300, 300, 80);
  51.         trudny.setBounds(320, 500, 300, 80);
  52.  
  53.         add(łatwy);
  54.         add(sredni);
  55.         add(trudny);
  56.  
  57.     }
  58.  
  59.     public void actionPerformed(ActionEvent f){
  60.  
  61.         Object poziom = f.getSource();
  62.         if(poziom == łatwy){
  63.             czas = 30;
  64.             System.out.println("tutaj!");
  65.                                             //odliczanie(czas);
  66.             while(czas != 0){
  67.                 //wyswietlanie();
  68.             }
  69.         }
  70.         else if(poziom == sredni){
  71.             czas = 20;
  72.                                             //odliczanie(czas);
  73.             while(czas != 0){
  74.                 //wyswietlanie();
  75.             }
  76.         }
  77.         else if(poziom == trudny){
  78.  
  79.             timer.schedule(odlicz, 4000);
  80.  
  81.             //czas = 10;
  82.                                             //odliczanie(czas);
  83.  
  84.             if(czas != 0){
  85.                 Okno noweOkno_2 = new Okno(1024,768);
  86.                 CardLayout CL_2 =(CardLayout)(noweOkno_2.karty.getLayout());
  87.                 CL_2.show(noweOkno_2.karty, "etap_1");
  88.             }
  89.             while(czas == 0){
  90.                 Okno noweOkno_3 = new Okno(1024,768);
  91.                 CardLayout CL_3 =(CardLayout)(noweOkno_3.karty.getLayout());
  92.                 CL_3.show(noweOkno_3.karty, "etap_3");
  93.             }
  94.  
  95.         }
  96.     }
  97.  
  98.     /*private void odliczanie(int czas){
  99.         long czas_teraz = System.currentTimeMillis();
  100.         while(czas >= 0){
  101.             if(System.currentTimeMillis() - czas_teraz > 1000){
  102.                 System.out.println(czas--);
  103.                 czas_teraz= System.currentTimeMillis();
  104.             }
  105.         }
  106.         System.out.println("Bam");
  107.     }*/
  108.  
  109.     public class odliczanie extends TimerTask{
  110.         public int time = 0;
  111.  
  112.         public void run() {
  113.             System.out.println("Timer ran" + ++time);
  114.         }
  115.     }
  116.  
  117.     public void paintComponent(Graphics graphic) {
  118.         Graphics2D g2d = (Graphics2D) graphic;
  119.         g2d.drawImage(tlo_lvl, 0, 0, this);
  120.     }
  121.  
  122.     public static Image wez_zdjecie(String nazwa_pliku)
  123.     {
  124.         return new ImageIcon(nazwa_pliku).getImage();
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment