Biblioteca Java - Blame information for rev 27

Subversion Repositories:
Rev:
Rev Author Line No. Line
27 mihai 1 package ex3_roadtraffic;
2  
3 import java.awt.*;
4  
5 public class Car implements Runnable{
6  
7     public static final int REDCAR  = 0;
8     public static final int BLUECAR = 1;
9  
10     private final static int bridgeY   = 95;
11     private final static int bridgeXLeft = 210;
12     private final static int bridgeXLeft2 = 290;
13     private final static int bridgeXMid = 410;
14     private final static int bridgeXRight2 = 530;
15     private final static int bridgeXRight = 610;
16     private final static int totalWidth = 900;
17     private final static int initX[]  = {-80, totalWidth};
18     private final static int initY[]  = {135, 55};
19     private final static int outLeft = -200;
20     private final static int outRight = totalWidth + 200;
21  
22     int cartype;
23     int xpos, ypos;
24     Car inFront;
25     Image image;
26     TrafficController controller;
27  
28     public Car(int cartype,Car inFront, Image image, TrafficController controller) {
29         this.cartype = cartype;
30         this.inFront = inFront;
31         this.image = image;
32         this.controller  = controller;
33         if (cartype == REDCAR)
34             xpos = inFront==null ? outRight : Math.min(initX[cartype],inFront.getX()-90);
35         else
36             xpos = inFront == null ? outLeft : Math.max(initX[cartype],inFront.getX()+90);
37         ypos = initY[cartype];
38     }
39  
40  
41     public void move() {
42         int xposOld =  xpos;
43         if (cartype==REDCAR) {
44             if (inFront.getX() - xpos > 100) {
45                 xpos += 4;
46                 if (xpos >= bridgeXLeft & xposOld < bridgeXLeft) controller.enterLeft();
47                 else if (xpos > bridgeXLeft && xpos < bridgeXMid) {if (ypos > bridgeY) ypos -= 2;}
48                 else if (xpos >= bridgeXRight2 && xpos < bridgeXRight) {if (ypos < initY[REDCAR]) ypos += 2;}
49                 else if (xpos >= bridgeXRight &&  xposOld < bridgeXRight) controller.leaveRight();
50             }
51         } else {
52             if (xpos-inFront.getX() > 100) {
53                 xpos -= 4;
54                 if (xpos <= bridgeXRight && xposOld > bridgeXRight) controller.enterRight();
55                 else if (xpos < bridgeXRight && xpos > bridgeXMid) {if (ypos < bridgeY) ypos += 2;}
56                 else if (xpos <= bridgeXLeft2 && xpos > bridgeXLeft) {if(ypos > initY[BLUECAR]) ypos -= 2;}
57                 else if (xpos <= bridgeXLeft && xposOld > bridgeXLeft) controller.leaveLeft();
58             }
59         }
60     }
61  
62  
63  
64  
65     public void run() {
66         boolean outOfSight = cartype == REDCAR ? xpos > totalWidth : xpos < -80;
67         while (!outOfSight) {
68             move();
69             outOfSight = cartype == REDCAR ? xpos > totalWidth : xpos < -80;
70             try {
71                 Thread.sleep(30);
72             } catch (InterruptedException e) {}
73             }
74         xpos = cartype == REDCAR ? outRight: outLeft;
75     }
76  
77     public int getX() {return xpos;}
78  
79     public void draw(Graphics g) {
80         g.drawImage(image,xpos,ypos,null);
81     }
82 }