Initial commit

This commit is contained in:
github-classroom[bot]
2022-03-31 09:49:56 +00:00
commit 5a1de0b8e7
83 changed files with 17003 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
/*
* Gradle build configuration for specific lab module / exercise
* Default declarations can be found in the lab main build configuration (../../gradle.build)
* Declarations in this file extend or override the default values.
*/
// the Java plugin is added by default in the main lab configuration
plugins {
// Apply the application plugin to add support for building a CLI application.
id 'application'
}
description = 'Lab04 Bridge'
dependencies {
}
// Configuration for Application plugin
application {
// Define the main class for the application.
mainClass = 'ch.zhaw.prog2.bridge.Main'
}
@@ -0,0 +1,82 @@
package ch.zhaw.prog2.bridge;
import java.awt.*;
public class Car implements Runnable {
public static final int REDCAR = 0;
public static final int BLUECAR = 1;
private final int bridgeY = 95;
private final int bridgeXLeft = 210;
private final int bridgeXLeft2 = 290;
private final int bridgeXMid = 410;
private final int bridgeXRight2 = 530;
private final int bridgeXRight = 610;
private final int totalWidth = 900;
private final int initX[] = {-80, totalWidth};
private final int initY[] = {135, 55};
private final int outLeft = -200;
private final int outRight = totalWidth + 200;
int cartype;
int xpos, ypos;
Car inFront;
Image image;
TrafficController controller;
public Car(int cartype, Car inFront, Image image, TrafficController controller) {
this.cartype = cartype;
this.inFront = inFront;
this.image = image;
this.controller = controller;
if (cartype == REDCAR) {
xpos = inFront == null ? outRight : Math.min(initX[cartype], inFront.getX()-90);
} else {
xpos = inFront == null ? outLeft : Math.max(initX[cartype], inFront.getX()+90);
}
ypos = initY[cartype];
}
public void move() {
int xposOld = xpos;
if (cartype==REDCAR) {
if (inFront.getX() - xpos > 100) {
xpos += 4;
if (xpos >= bridgeXLeft && xposOld < bridgeXLeft) controller.enterLeft();
else if (xpos > bridgeXLeft && xpos < bridgeXMid) { if (ypos > bridgeY) ypos -= 2; }
else if (xpos >= bridgeXRight2 && xpos < bridgeXRight) { if (ypos < initY[REDCAR]) ypos += 2; }
else if (xpos >= bridgeXRight && xposOld < bridgeXRight) controller.leaveRight();
}
} else {
if (xpos-inFront.getX() > 100) {
xpos -= 4;
if (xpos <= bridgeXRight && xposOld > bridgeXRight) controller.enterRight();
else if (xpos < bridgeXRight && xpos > bridgeXMid) { if (ypos < bridgeY) ypos += 2; }
else if (xpos <= bridgeXLeft2 && xpos > bridgeXLeft) { if(ypos > initY[BLUECAR]) ypos -= 2; }
else if (xpos <= bridgeXLeft && xposOld > bridgeXLeft) controller.leaveLeft();
}
}
}
public void run() {
boolean outOfSight = cartype == REDCAR ? xpos > totalWidth : xpos < -80;
while (!outOfSight) {
move();
outOfSight = cartype == REDCAR ? xpos > totalWidth : xpos < -80;
try {
Thread.sleep(30);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
xpos = cartype == REDCAR ? outRight: outLeft;
}
public int getX() { return xpos; }
public void draw(Graphics g) {
g.drawImage(image, xpos, ypos, null);
}
}
@@ -0,0 +1,25 @@
package ch.zhaw.prog2.bridge;
import javax.swing.*;
import java.awt.*;
public class CarWindow extends JFrame {
public CarWindow(CarWorld world) {
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add("Center",world);
JButton addLeft = new JButton("Add Left");
JButton addRight = new JButton("Add Right");
addLeft.addActionListener((e) -> world.addCar(Car.REDCAR));
addRight.addActionListener((e) -> world.addCar(Car.BLUECAR));
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(addLeft);
p1.add(addRight);
c.add("South",p1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
@@ -0,0 +1,66 @@
package ch.zhaw.prog2.bridge;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
class CarWorld extends JPanel {
Image bridge;
Image redCar;
Image blueCar;
TrafficController controller;
ArrayList<Car> blueCars = new ArrayList<>();
ArrayList<Car> redCars = new ArrayList<>();
public CarWorld(TrafficController controller) {
this.controller = controller;
MediaTracker mt = new MediaTracker(this);
Toolkit toolkit = Toolkit.getDefaultToolkit();
redCar = toolkit.getImage(getClass().getResource("redcar.gif"));
mt.addImage(redCar, 0);
blueCar = toolkit.getImage(getClass().getResource("bluecar.gif"));
mt.addImage(blueCar, 1);
bridge = toolkit.getImage(getClass().getResource("bridge1.gif"));
mt.addImage(bridge, 2);
try {
mt.waitForID(0);
mt.waitForID(1);
mt.waitForID(2);
} catch (java.lang.InterruptedException e) {
System.out.println("Couldn't load one of the images");
}
redCars.add( new Car(Car.REDCAR,null,redCar,null) );
blueCars.add( new Car(Car.BLUECAR,null,blueCar,null) );
setPreferredSize( new Dimension(bridge.getWidth(null), bridge.getHeight(null)) );
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(bridge,0,0,this);
for (Car c : redCars) c.draw(g);
for (Car c : blueCars) c.draw(g);
}
public void addCar(final int cartype) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Car car;
if (cartype==Car.REDCAR) {
car = new Car(cartype,redCars.get(redCars.size()-1),redCar,controller);
redCars.add(car);
} else {
car = new Car(cartype,blueCars.get(blueCars.size()-1),blueCar,controller);
blueCars.add(car);
}
new Thread(car).start();
}
});
}
}
@@ -0,0 +1,31 @@
package ch.zhaw.prog2.bridge;
public class Main {
private static void nap(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] a) {
final TrafficController controller = new TrafficController();
final CarWorld world = new CarWorld(controller);
final CarWindow win = new CarWindow(world);
win.pack();
win.setVisible(true);
new Thread(new Runnable() {
public void run() {
while (true) {
nap(25);
win.repaint();
}
}
}).start();
}
}
@@ -0,0 +1,19 @@
package ch.zhaw.prog2.bridge;
/*
* Controls the traffic passing the bridge
*/
public class TrafficController {
/* Called when a car wants to enter the bridge form the left side */
public void enterLeft() {}
/* Called when a wants to enter the bridge form the right side */
public void enterRight() {}
/* Called when the car leaves the bridge on the left side */
public void leaveLeft() {}
/* Called when the car leaves the bridge on the right side */
public void leaveRight() {}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB