martaczaska

Timer_co_tu_się_odwala

Jan 4th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 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.     int zero = 1;
  33.     public int time = 0;
  34.  
  35.  
  36.     public Poziomy(){
  37.         setLayout(null);
  38.  
  39.         File zdj_lvl = new File("zdjecia/tlo_menu.png");
  40.         try{ tlo_lvl = ImageIO.read(zdj_lvl); }
  41.         catch(IOException e){ System.err.println("Blad odczytu obrazków"); }
  42.  
  43.         łatwy = new JButton("łatwy");
  44.         sredni = new JButton("średni");
  45.         trudny = new JButton("trudny");
  46.  
  47.         łatwy.addActionListener(this);
  48.         sredni.addActionListener(this);
  49.         trudny.addActionListener(this);
  50.  
  51.         łatwy.setBounds(320, 100, 300, 80 );
  52.         sredni.setBounds(320, 300, 300, 80);
  53.         trudny.setBounds(320, 500, 300, 80);
  54.  
  55.         add(łatwy);
  56.         add(sredni);
  57.         add(trudny);
  58.  
  59.     }
  60.  
  61.     public void actionPerformed(ActionEvent f){
  62.  
  63.         Object poziom = f.getSource();
  64.         if(poziom == łatwy){
  65.             czas = 30;
  66.             timer.schedule(odlicz, 30000);
  67.  
  68.             System.out.println("tutaj!");
  69.                                             //odliczanie(czas);
  70.             while(czas != 0){
  71.                 //wyswietlanie();
  72.             }
  73.         }
  74.  
  75.         else if(poziom == sredni){
  76.             czas = 20;
  77.             timer.schedule(odlicz, 20000);
  78.                                             //odliczanie(czas);
  79.             while(czas != 0){
  80.                 //wyswietlanie();
  81.             }
  82.         }
  83.  
  84.         else if(poziom == trudny){
  85.             czas = 4; //10
  86.             timer.schedule(odlicz, 0, 1000);
  87.  
  88.                 Okno noweOkno_2 = new Okno(1024,768);
  89.                 CardLayout CL_2 =(CardLayout)(noweOkno_2.karty.getLayout());
  90.                 CL_2.show(noweOkno_2.karty, "etap_1");
  91.  
  92.             //if(zero == 0){
  93.             if(time == 4){
  94.                 timer.cancel();                                          //TUTAJ CHYBA NIE NADĄŻA ZROBIĆ timer.cancel(); bo w terminalu widzę, że pojawia się na chwilę "Jestem już na 3 etapie!", ale potem znów zaczyna liczyć "Timer ran..."
  95.                 Okno noweOkno_3 = new Okno(1024,768);
  96.                 CardLayout CL_3 =(CardLayout)(noweOkno_3.karty.getLayout());
  97.                 CL_3.show(noweOkno_3.karty, "etap_3");
  98.             }
  99.  
  100.         }
  101.     }
  102.  
  103.     public class odliczanie extends TimerTask{
  104.         //public int time = 0;
  105.  
  106.         public void run() {
  107.             ++time;
  108.             System.out.println("Timer ran" + ++time);
  109.         }
  110.     }
  111.  
  112.     public void paintComponent(Graphics graphic) {
  113.         Graphics2D g2d = (Graphics2D) graphic;
  114.         g2d.drawImage(tlo_lvl, 0, 0, this);
  115.     }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment