Biblioteca Java - Blame information for rev 27
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
27 | mihai | 1 | package ex3_roadtraffic; |
2 | import java.awt.*; | ||
3 | import java.util.*; | ||
4 | |||
5 | import javax.swing.*; | ||
6 | |||
7 | class CarWorld extends JPanel { | ||
8 | |||
9 | Image bridge; | ||
10 | Image redCar; | ||
11 | Image blueCar; | ||
12 | |||
13 | TrafficController controller; | ||
14 | |||
15 | ArrayList blueCars = new ArrayList(); | ||
16 | ArrayList redCars = new ArrayList(); | ||
17 | |||
18 | public CarWorld() { | ||
19 | controller = new TrafficController(); | ||
20 | MediaTracker mt = new MediaTracker(this); | ||
21 | Toolkit toolkit = Toolkit.getDefaultToolkit(); | ||
22 | |||
23 | redCar = toolkit.getImage("image/redcar.gif"); | ||
24 | mt.addImage(redCar, 0); | ||
25 | blueCar = toolkit.getImage("image/bluecar.gif"); | ||
26 | mt.addImage(blueCar, 1); | ||
27 | bridge = toolkit.getImage("image/bridge1.gif"); | ||
28 | mt.addImage(bridge, 2); | ||
29 | |||
30 | try { | ||
31 | mt.waitForID(0); | ||
32 | mt.waitForID(1); | ||
33 | mt.waitForID(2); | ||
34 | } catch (java.lang.InterruptedException e) { | ||
35 | System.out.println("Couldn't load one of the images"); | ||
36 | } | ||
37 | |||
38 | redCars.add(new Car(Car.REDCAR,null,redCar,null)); | ||
39 | blueCars.add(new Car(Car.BLUECAR,null,blueCar,null)); | ||
40 | setPreferredSize(new Dimension(bridge.getWidth(null),bridge.getHeight(null))); | ||
41 | } | ||
42 | |||
43 | |||
44 | public void paintComponent(Graphics g) { | ||
45 | g.drawImage(bridge,0,0,this); | ||
46 | for (Iterator iter = redCars.iterator(); iter.hasNext();) { | ||
47 | Car element = (Car) iter.next(); | ||
48 | element.draw(g); | ||
49 | } | ||
50 | |||
51 | for (Iterator iter = blueCars.iterator(); iter.hasNext();) { | ||
52 | Car element = (Car) iter.next(); | ||
53 | element.draw(g); | ||
54 | } | ||
55 | |||
56 | } | ||
57 | |||
58 | public void addCar(final int cartype) { | ||
59 | SwingUtilities.invokeLater(new Runnable () { | ||
60 | public void run() { | ||
61 | Car c; | ||
62 | if (cartype==Car.REDCAR) { | ||
63 | c = new Car(cartype,(Car)redCars.get(redCars.size()-1),redCar,controller); | ||
64 | redCars.add(c); | ||
65 | } else { | ||
66 | c = new Car(cartype,(Car)blueCars.get(blueCars.size()-1),blueCar,controller); | ||
67 | blueCars.add(c); | ||
68 | } | ||
69 | new Thread(c).start(); | ||
70 | } | ||
71 | }); | ||
72 | } | ||
73 | |||
74 | } |