Advertisement
ljukk

semafor

May 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. import java.io.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseListener;
  7. import java.awt.event.MouseEvent;
  8.  
  9. public class Semafor extends JPanel {
  10.    
  11.     Timer timer;
  12.     private int seconds = 1;
  13.     private int stav;
  14.     private boolean left_broken = false;
  15.    
  16.     public Semafor () {  
  17.         setBounds(125,50,100,200);
  18.         timer = new Timer(1000, new ActionListener() {
  19.                 @Override
  20.                 public void actionPerformed(ActionEvent e) {
  21.                     if (seconds % 4 == 1) {
  22.                         stav = 1;
  23.                     } else if (seconds % 4 == 2 || seconds % 4 == 0){
  24.                         stav = 2;
  25.                     } else if (seconds % 4 == 3){
  26.                         stav = 3;
  27.                     }
  28.                     System.out.println("Time in seconds : " + seconds);
  29.                     repaint();
  30.                     seconds++;
  31.                    
  32.                 }
  33.             });
  34.         timer.start();      
  35.     }
  36.    
  37.     public void mousePressed(MouseEvent e) {
  38.        System.out.println("Mouse pressed; # of clicks: ");
  39.     }
  40.    
  41.     @Override
  42.     public void paintComponent(Graphics g) {
  43.         super.paintComponent(g);
  44.         g.setColor(Color.black);
  45.         g.fillRect(0, 0, 100, 200);
  46.         if (stav == 1 || stav == 0){
  47.             g.setColor(Color.red);
  48.             g.fillOval(30, 20, 40, 40);
  49.             g.setColor(Color.white);
  50.             g.fillOval(30, 80, 40, 40);
  51.             g.setColor(Color.white);
  52.             g.fillOval(30, 140, 40, 40);
  53.         } else if (stav == 2) {
  54.             g.setColor(Color.white);
  55.             g.fillOval(30, 20, 40, 40);
  56.             g.setColor(Color.orange);
  57.             g.fillOval(30, 80, 40, 40);
  58.             g.setColor(Color.white);
  59.             g.fillOval(30, 140, 40, 40);  
  60.         } else if (stav == 3){
  61.             g.setColor(Color.white);
  62.             g.fillOval(30, 20, 40, 40);
  63.             g.setColor(Color.white);
  64.             g.fillOval(30, 80, 40, 40);
  65.             g.setColor(Color.green);
  66.             g.fillOval(30, 140, 40, 40);  
  67.         }
  68.        
  69.     }
  70.    
  71.    
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement