Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- public class Menu extends JFrame implements Runnable {
- private BufferedImage tlo;
- private BufferedImage tlo1;
- private BufferedImage tlo2;
- private BufferedImage tlo3;
- private BufferedImage tlo4;
- private int swiatla;
- private ArrayList<Auto> auta = new ArrayList<Auto>();
- private ArrayList<Tramwaj> tramwaje = new ArrayList<Tramwaj>();
- private ArrayList<Tramwaj> tramwaje2 = new ArrayList<Tramwaj>();
- private ArrayList<Miejsce> zajete = new ArrayList<Miejsce>();
- protected Graphics bufferGraphics;
- protected Image offscreen;
- Swiatlo s1;
- Swiatlo s2;
- Swiatlo s3;
- Swiatlo s4;
- public Menu() {
- KeySpy keySpy = new KeySpy();
- this.addKeyListener(keySpy);
- try {
- tlo1 = ImageIO.read(new File("res/okopowa1.png"));
- } catch (IOException e) {
- }
- try {
- tlo2 = ImageIO.read(new File("res/okopowa2.png"));
- } catch (IOException e) {
- }
- try {
- tlo3 = ImageIO.read(new File("res/okopowa3.png"));
- } catch (IOException e) {
- }
- try {
- tlo4 = ImageIO.read(new File("res/okopowa4.png"));
- } catch (IOException e) {
- }
- swiatla = 1;
- s1 = new Swiatlo(this);
- s2 = new Swiatlo(this);
- s3 = new Swiatlo(this);
- s4 = new Swiatlo(this);
- s1.swiatloZielone();
- s2.swiatloCzerwone();
- s3.swiatloCzerwone();
- s4.swiatloCzerwone();
- this.setTitle("Skrzyzowanie Okopowej i Towarowej");
- this.setResizable(false);
- this.setSize(1000, 1000);
- Auto a0 = new Auto(this, 4, 100);
- Auto a1 = new Auto(this, 5, 100);
- Auto a2 = new Auto(this, 6, 100);
- Auto a3 = new Auto(this, 7, 100);
- Auto a4 = new Auto(this, 8, 100);
- Auto a5 = new Auto(this, 9, 100);
- Auto a6 = new Auto(this, 10, 120);
- Auto a7 = new Auto(this, 11, 140);
- Auto a8 = new Auto(this, 12, 80);
- Auto a9 = new Auto(this, 13, 100);
- Auto a10 = new Auto(this, 14, 100);
- Auto a11 = new Auto(this, 15, 100);
- Tramwaj t0 = new Tramwaj(this, 0, 100, 1);
- Tramwaj t1 = new Tramwaj(this, 1, 100, 1);
- Tramwaj t2 = new Tramwaj(this, 2, 100, 2);
- Tramwaj t3 = new Tramwaj(this, 3, 100, 2);
- auta.add(a1);
- auta.add(a2);
- auta.add(a3);
- auta.add(a0);
- auta.add(a4);
- auta.add(a5);
- auta.add(a6);
- auta.add(a7);
- auta.add(a8);
- auta.add(a9);
- auta.add(a10);
- auta.add(a11);
- tramwaje.add(t0);
- tramwaje.add(t1);
- tramwaje2.add(t2);
- tramwaje2.add(t3);
- a0.start();
- a1.start();
- a2.start();
- a3.start();
- a4.start();
- a5.start();
- a6.start();
- a7.start();
- a8.start();
- a9.start();
- a10.start();
- t0.start();
- t1.start();
- t2.start();
- t3.start();
- }
- public void run() {
- while (true) {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- }
- swiatla = swiatla + 1;
- if(swiatla == 5){
- swiatla = 1;
- }
- repaint();
- wybierzWariantSwiatel();
- }
- }
- public synchronized int dodajNoweZajeteMiejsce(Miejsce m) {
- int s = zajete.size();
- if (s < 0) s = 0;
- zajete.add(s, m);
- return s;
- }
- public synchronized void usunZajeteMiejsce(int dx, int dy) {
- int s = -1;
- for (int i = 0; i < zajete.size(); i++)
- if (zajete.get(i).getX() == dx && zajete.get(i).getY() == dy)
- s = i;
- if (s >= 0)
- zajete.remove(s);
- }
- public synchronized boolean sprawdzCzyMiejsceJestZajete(int dx, int dy) {
- for (Miejsce m : zajete)
- if (m.getX() == dx && m.getY() == dy)
- return true;
- return false;
- }
- class KeySpy implements KeyListener {
- public void keyPressed(KeyEvent arg0) {
- }
- public void keyReleased(KeyEvent arg0) {
- switch (arg0.getKeyChar()) {
- case ' ':
- swiatla = swiatla + 1;
- if(swiatla == 5){
- swiatla = 1;
- }
- break;
- }
- wybierzWariantSwiatel();
- }
- public void keyTyped(KeyEvent arg0) {
- }
- }
- public Swiatlo getLight(int tory) {
- switch (tory) {
- case 0:
- return s2;
- case 1:
- return s2;
- case 2:
- return s1;
- case 3:
- return s1;
- case 4:
- return s2;
- case 5:
- return s3;
- case 6:
- return s2;
- case 7:
- return s3;
- case 8:
- return s1;
- case 9:
- return s4;
- case 10:
- return s1;
- case 11:
- return s4;
- default:
- return s1;
- }
- }
- public void wybierzWariantSwiatel() {
- switch (swiatla) {
- case 1:
- zmienSwiatla(1);
- break;
- case 2:
- zmienSwiatla(2);
- break;
- case 3:
- zmienSwiatla(3);
- break;
- case 4:
- zmienSwiatla(4);
- break;
- }
- }
- public void zmienSwiatla(int opcjaSwiatel) {
- if (opcjaSwiatel == 1) {
- s1.swiatloZielone();
- s2.swiatloCzerwone();
- s3.swiatloCzerwone();
- s4.swiatloCzerwone();
- } else if (opcjaSwiatel == 2) {
- s1.swiatloCzerwone();
- s2.swiatloZielone();
- s3.swiatloCzerwone();
- s4.swiatloCzerwone();
- } else if (opcjaSwiatel == 3) {
- s1.swiatloCzerwone();
- s2.swiatloCzerwone();
- s3.swiatloZielone();
- s4.swiatloCzerwone();
- }
- else if (opcjaSwiatel == 4) {
- s1.swiatloCzerwone();
- s2.swiatloCzerwone();
- s3.swiatloCzerwone();
- s4.swiatloZielone();
- }
- }
- public void paint(Graphics g) {
- if (bufferGraphics == null) {
- offscreen = createImage(getWidth(), getHeight());
- bufferGraphics = offscreen.getGraphics();
- }
- paintComponent(bufferGraphics);
- g.drawImage(offscreen, 0, 0, null);
- }
- public void update(Graphics g) {
- paint(g);
- }
- public void paintComponent(Graphics g) {
- {
- switch (swiatla) {
- case 1:
- tlo = tlo1;
- break;
- case 2:
- tlo = tlo2;
- break;
- case 3:
- tlo = tlo3;
- break;
- case 4:
- tlo = tlo4;
- break;
- }
- g.drawImage(tlo, 0, 0, 1000, 1000, this);
- Graphics2D g2 = (Graphics2D) g;
- for (Auto a : auta)
- g2.drawImage(a.getImage(), a.getX(), a.getY(), 25, 25, this);
- for (Tramwaj t : tramwaje)
- g2.drawImage(t.getImage(), t.getX(), t.getY(), 25, 50, this);
- for (Tramwaj t : tramwaje2)
- g2.drawImage(t.getImage(), t.getX(), t.getY(), 50, 25, this);
- g2.setColor(Color.BLACK);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement