Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseEvent;
- public class Semafor extends JPanel {
- Timer timer;
- private int seconds = 1;
- private int stav;
- private boolean left_broken = false;
- public Semafor () {
- setBounds(125,50,100,200);
- timer = new Timer(1000, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (seconds % 4 == 1) {
- stav = 1;
- } else if (seconds % 4 == 2 || seconds % 4 == 0){
- stav = 2;
- } else if (seconds % 4 == 3){
- stav = 3;
- }
- System.out.println("Time in seconds : " + seconds);
- repaint();
- seconds++;
- }
- });
- timer.start();
- }
- public void mousePressed(MouseEvent e) {
- System.out.println("Mouse pressed; # of clicks: ");
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.black);
- g.fillRect(0, 0, 100, 200);
- if (stav == 1 || stav == 0){
- g.setColor(Color.red);
- g.fillOval(30, 20, 40, 40);
- g.setColor(Color.white);
- g.fillOval(30, 80, 40, 40);
- g.setColor(Color.white);
- g.fillOval(30, 140, 40, 40);
- } else if (stav == 2) {
- g.setColor(Color.white);
- g.fillOval(30, 20, 40, 40);
- g.setColor(Color.orange);
- g.fillOval(30, 80, 40, 40);
- g.setColor(Color.white);
- g.fillOval(30, 140, 40, 40);
- } else if (stav == 3){
- g.setColor(Color.white);
- g.fillOval(30, 20, 40, 40);
- g.setColor(Color.white);
- g.fillOval(30, 80, 40, 40);
- g.setColor(Color.green);
- g.fillOval(30, 140, 40, 40);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement