aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src
diff options
context:
space:
mode:
authorCamil Staps2015-05-26 10:12:10 +0200
committerCamil Staps2015-05-26 10:12:10 +0200
commitcbb41f8025105e455993db2dee52d4234aed0aeb (patch)
tree031de0aa9ae8f089a9c382d5a397538c7997a29b /Week14 Route 66/src
parentWeek13 tarball (diff)
Start w14
Diffstat (limited to 'Week14 Route 66/src')
-rw-r--r--Week14 Route 66/src/OO14route66/Car.java97
-rw-r--r--Week14 Route 66/src/OO14route66/Controller.java88
-rw-r--r--Week14 Route 66/src/OO14route66/Direction.java55
-rw-r--r--Week14 Route 66/src/OO14route66/KeyHandler.java39
-rw-r--r--Week14 Route 66/src/OO14route66/Model.java60
-rw-r--r--Week14 Route 66/src/OO14route66/Regelaar.java15
-rw-r--r--Week14 Route 66/src/OO14route66/RoadView.java87
-rw-r--r--Week14 Route 66/src/OO14route66/Route66.java42
-rw-r--r--Week14 Route 66/src/OO14route66/TableView.java65
-rw-r--r--Week14 Route 66/src/com/camilstaps/route66/Week14Route66.java19
10 files changed, 567 insertions, 0 deletions
diff --git a/Week14 Route 66/src/OO14route66/Car.java b/Week14 Route 66/src/OO14route66/Car.java
new file mode 100644
index 0000000..4448a27
--- /dev/null
+++ b/Week14 Route 66/src/OO14route66/Car.java
@@ -0,0 +1,97 @@
+package OO14route66;
+
+import java.awt.Color;
+import java.awt.Graphics;
+import java.util.Random;
+
+/**
+ * OO1route66 initial class
+ * @author Pieter Koopman
+ *
+ * Class for an individual car
+ */
+public class Car
+{
+ public static final int
+ CARWIDTH = 20,
+ CARLENGTH = 40,
+ MINCARSPACE = 5,
+ MINCARSPEED = 2,
+ MAXCARSPEED = 4;
+ public static final Color[] carColors={Color.red, Color.blue, Color.black, Color.gray, Color.green,
+ Color.pink, Color.orange, Color.magenta, Color.lightGray};
+
+ private final int speed, number; // speed and car-number
+ private final Direction direction; // driving direction, used by paint
+ private int location; // current location on the road
+ private final Color color; // color of this car
+ private final Random random; // here to ensure that every car gets a new
+
+ /**
+ * The constructor
+ * @param number of the car
+ */
+ public Car(int number) {
+ this.number = number;
+ random = new Random();
+ speed = MINCARSPEED + random.nextInt(MAXCARSPEED - MINCARSPEED + 1);
+ direction = Direction.intToDirection(number);
+ location = RoadView.WINDOWSIZE - 2 - (number/Model.DIRECTIONS) * (CARLENGTH + MINCARSPACE);
+ color = carColors[number % carColors.length];
+ }
+
+ /**
+ * move this car one step
+ */
+ public void step() {
+ location = (location + speed) % RoadView.WINDOWSIZE;
+ }
+
+ /**
+ * paint this car
+ * @param g
+ */
+ public void paint(Graphics g) {
+ int x, y, w, h;
+ switch (direction) {
+ case North:
+ x = RoadView.WINDOWSIZE / 2 + 1;
+ y = RoadView.WINDOWSIZE - location;
+ w = Car.CARWIDTH;
+ h = Car.CARLENGTH;
+ break;
+ case East:
+ x = location - Car.CARLENGTH;
+ y = RoadView.WINDOWSIZE / 2 + 1;
+ w = Car.CARLENGTH;
+ h = Car.CARWIDTH;
+ break;
+ case South:
+ x = RoadView.WINDOWSIZE / 2 - Car.CARWIDTH - 1;
+ y = location - Car.CARLENGTH;
+ w = Car.CARWIDTH;
+ h = Car.CARLENGTH;
+ break;
+ case West:
+ x = RoadView.WINDOWSIZE - location - 1;
+ y = RoadView.WINDOWSIZE / 2 - Car.CARWIDTH - 1;
+ w = Car.CARLENGTH;
+ h = Car.CARWIDTH;
+ break;
+ default:
+ x = y = w = h = 10;
+ }
+ g.setColor(color);
+ g.fillRect(x, y, w, h);
+ g.setColor(Color.WHITE);
+ g.drawString(String.valueOf(number), x + w / 6, y + h / 2 + 4);
+ }
+
+ /**
+ * yield the current location of this car
+ * @return
+ */
+ public int getLocation() {
+ return location;
+ }
+}
diff --git a/Week14 Route 66/src/OO14route66/Controller.java b/Week14 Route 66/src/OO14route66/Controller.java
new file mode 100644
index 0000000..c1542b9
--- /dev/null
+++ b/Week14 Route 66/src/OO14route66/Controller.java
@@ -0,0 +1,88 @@
+package OO14route66;
+
+import java.util.Random;
+
+/**
+ * OO1route66 initial class
+ * @author Pieter Koopman
+ *
+ * The initial controller runs as a single thread
+ */
+public class Controller
+{
+ private int delay = 200; // average sleep time
+ private final Model model; // the model
+ private final Random random; // a random generator
+ private boolean run = true; // car can run in simulation
+
+ /**
+ * The constructor of the controller
+ * @param model holds the cars
+ */
+ public Controller(Model model) {
+ this.model = model;
+ random = new Random();
+ }
+
+ /**
+ * the run method from Thread
+ * forever:
+ * move all cars
+ * sleep some time
+ */
+ public void run() {
+ while (true) {
+ stepAllCars();
+ pause();
+ }
+ }
+
+ /**
+ * wait some pseudo random time
+ */
+ private void pause() {
+ try { // sleep can throw an exception
+ Thread.sleep(delay / 2 + random.nextInt(delay));
+ }
+ catch (InterruptedException e) { // catch the exception thrown by sleep
+ System.out.println("An exception in Controller: " + e);
+ }
+ }
+
+ /**
+ * make one step with all cars and repaint views.
+ */
+ public void stepAllCars() {
+ if (run) {
+ for (int c = 0; c < Model.NUMBEROFCARS; c += 1) {
+ model.getCar(c).step();
+ }
+ }
+ model.update(); // update only after all cars have stepped
+ }
+ /**
+ * stop all cars by setting boolean run to false
+ */
+ public void stopCars() {
+ run = false;
+ }
+
+ /**
+ * start all cars by setting boolean run to true
+ */
+ public void resumeCars() {
+ run = true;
+ }
+
+ public int getDelay() {
+ return delay;
+ }
+
+ /**
+ * set delay between maximum and minimum bounds
+ * @param d
+ */
+ public void setDelay(int d) {
+ delay = Math.max(50, Math.min (2000, d));
+ }
+}
diff --git a/Week14 Route 66/src/OO14route66/Direction.java b/Week14 Route 66/src/OO14route66/Direction.java
new file mode 100644
index 0000000..b8c1636
--- /dev/null
+++ b/Week14 Route 66/src/OO14route66/Direction.java
@@ -0,0 +1,55 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package OO14route66;
+
+/**
+ * OO1route66 initial class
+ * @author Pieter Koopman
+ *
+ * Enumeration class for driving directions
+ */
+public enum Direction
+{
+ North, East, South, West;
+
+ /**
+ * convert integer to direction
+ * if number of directions is 2 only East and West are used,
+ * otherwise all 4 directions are used
+ * @param i the integer
+ * @return the direction
+ */
+ public static Direction intToDirection(int i) {
+ if (Model.DIRECTIONS == 2) {
+ switch (i % 2) {
+ case 0: return East;
+ default: return West;
+ }
+ } else {
+ switch (i % 4) {
+ case 0: return North;
+ case 1: return East;
+ case 2: return South;
+ default: return West;
+ }
+ }
+ }
+
+ /**
+ * override standard toString
+ * @return string representation of this value
+ */
+ @Override
+ public String toString() {
+ switch (this) {
+ case North: return "North";
+ case East: return "East";
+ case South: return "South";
+ case West: return "West";
+ default: return "Unknown direction";
+ }
+ }
+}
diff --git a/Week14 Route 66/src/OO14route66/KeyHandler.java b/Week14 Route 66/src/OO14route66/KeyHandler.java
new file mode 100644
index 0000000..d446d48
--- /dev/null
+++ b/Week14 Route 66/src/OO14route66/KeyHandler.java
@@ -0,0 +1,39 @@
+package OO14route66;
+
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+
+/**
+ * Keyboard handler of views for controller.
+ * Pressing keys changes the controller.
+ * @author pieterkoopman
+ */
+public class KeyHandler extends KeyAdapter {
+
+ private Controller controller;
+
+ public KeyHandler (Controller c) {
+ controller = c;
+ }
+
+ /**
+ * on key down 'q' stop program
+ * on key down 's' stop the cars
+ * on key down ' ' all cars one step
+ * on key down '<' decrease delay
+ * on key down '>' increase delay
+ * on any other key activate the cars
+ * @param e the key event
+ */
+ @Override
+ public void keyPressed(KeyEvent e) {
+ switch (e.getKeyChar()) {
+ case 'q': System.exit(0);
+ case 's': controller.stopCars(); break;
+ case ' ': controller.stepAllCars(); break;
+ case '<': controller.setDelay(controller.getDelay() - 50); break;
+ case '>': controller.setDelay(controller.getDelay() + 50); break;
+ default : controller.resumeCars();
+ }
+ }
+}
diff --git a/Week14 Route 66/src/OO14route66/Model.java b/Week14 Route 66/src/OO14route66/Model.java
new file mode 100644
index 0000000..10c413f
--- /dev/null
+++ b/Week14 Route 66/src/OO14route66/Model.java
@@ -0,0 +1,60 @@
+package OO14route66;
+
+import java.util.ArrayList;
+import java.util.Observable;
+import javax.swing.JFrame;
+
+/**
+ * OO1route66 initial class
+ * @author Pieter Koopman
+ *
+ * The class model holds all cars in the simulation
+ */
+public class Model extends Observable
+{
+ private final Car [] cars;
+ public static final int
+// DIRECTIONS = 4, // for a crossing
+ DIRECTIONS = 2, // for a single road
+ NUMBEROFCARS = 5 * DIRECTIONS; // total number of cars in system
+
+ private final ArrayList<JFrame> views;
+
+
+ /**
+ * Constructor: create all cars
+ */
+ public Model() {
+ views = new ArrayList<JFrame>();
+ cars = new Car [NUMBEROFCARS];
+ for (int c = 0; c < NUMBEROFCARS; c += 1) {
+ cars[c] = new Car(c);
+ }
+ }
+
+ /**
+ * add the view to this model. It will be repainted upon an update
+ * @param view
+ */
+ public void addView(JFrame view) {
+ views.add(view);
+ }
+
+ /**
+ * get a car from the model
+ * @param i numbers of required car
+ * @return the car itself (not a copy)
+ */
+ public Car getCar(int i) {
+ return cars[i];
+ }
+
+ /**
+ * repaint all views
+ */
+ public void update() {
+ for (JFrame view: views) {
+ view.repaint();
+ }
+ }
+}
diff --git a/Week14 Route 66/src/OO14route66/Regelaar.java b/Week14 Route 66/src/OO14route66/Regelaar.java
new file mode 100644
index 0000000..1fc336f
--- /dev/null
+++ b/Week14 Route 66/src/OO14route66/Regelaar.java
@@ -0,0 +1,15 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package OO14route66;
+
+/**
+ *
+ * @author pieterkoopman
+ */
+class Regelaar {
+
+}
diff --git a/Week14 Route 66/src/OO14route66/RoadView.java b/Week14 Route 66/src/OO14route66/RoadView.java
new file mode 100644
index 0000000..1d53269
--- /dev/null
+++ b/Week14 Route 66/src/OO14route66/RoadView.java
@@ -0,0 +1,87 @@
+package OO14route66;
+
+import java.awt.Color;
+import java.awt.Graphics;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+
+/**
+ * OO13route66 animation
+ * @author Pieter Koopman
+ *
+ * Yields a graphical view on all cars
+ */
+public class RoadView extends JFrame
+{
+ JPanel panel; // the panel to draw the cars
+ public static final int WINDOWSIZE = 600; // the window size
+ private final Model model; // the model knows the position of cars
+
+ /**
+ * A subclass of JPanel to draw the cars.
+ * This is a bit of a hack to ensure that this panel is painted correctly.
+ */
+ private class CarPanel extends JPanel {
+ /**
+ * Constructor: just call the constructor of the base class
+ */
+ public CarPanel() {
+ super();
+ }
+ /**
+ * paint the roads and cars
+ * @param g Graphics to paint on
+ */
+ @Override
+ public void paint (Graphics g) {
+ paintRoad(g);
+ paintCars(g);
+ }
+ }
+ /**
+ * The constructor of RoadView
+ * @param model
+ */
+ RoadView(Model model) {
+ super("Route66 traffic simulator");
+ this.model = model;
+ setSize(WINDOWSIZE, WINDOWSIZE);
+ setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
+ panel = new CarPanel();
+ add(panel);
+ }
+ /**
+ * Paint the view of the model graphically
+ * @param g
+ */
+ @Override
+ public void paint(Graphics g) {
+ super.paint(g);
+ panel.repaint();
+ }
+ /**
+ * Paint all cars
+ * @param g graphics to draw on
+ */
+ private void paintCars (Graphics g) {
+ for (int i = 0; i < Model.NUMBEROFCARS; i += 1) {
+ model.getCar(i).paint(g);
+ }
+ }
+
+ /**
+ * Paint the roads
+ * @param g graphics to draw on
+ */
+ private void paintRoad (Graphics g) {
+ final int left = (WINDOWSIZE / 2) - Car.CARWIDTH - 4;
+ final int width = 2 * Car.CARWIDTH + 8;
+ g.setColor(Color.white); // background
+ g.fillRect(0, 0, WINDOWSIZE, WINDOWSIZE);
+ g.setColor(Color.darkGray); // streets
+ g.fillRect(0, left, WINDOWSIZE, width);
+ if (Model.DIRECTIONS > 2) { // paint a crossing if there are 4 directions
+ g.fillRect(left, 0, width, WINDOWSIZE);
+ }
+ }
+}
diff --git a/Week14 Route 66/src/OO14route66/Route66.java b/Week14 Route 66/src/OO14route66/Route66.java
new file mode 100644
index 0000000..f6b3f1f
--- /dev/null
+++ b/Week14 Route 66/src/OO14route66/Route66.java
@@ -0,0 +1,42 @@
+package OO14route66;
+
+/**
+ * OO1route66 initial class
+ * @author Pieter Koopman
+ * Route66 class constructs model, view and controller
+ */
+public class Route66
+{
+ Controller controller;
+ /**
+ * the main method for OO13route66
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ Route66 r66 = new Route66();
+ }
+
+ /**
+ * the main constructor:
+ * - creates model, controller and views
+ */
+ public Route66() {
+ Model model = new Model();
+
+ RoadView rview = new RoadView(model);
+ TableView tview = new TableView(model);
+ model.addView(tview);
+ model.addView(rview);
+
+ controller = new Controller(model);
+
+ KeyHandler keyHandler = new KeyHandler(controller);
+ rview.addKeyListener(keyHandler);
+ tview.addKeyListener(keyHandler);
+
+ tview.setVisible(true);
+ rview.setVisible(true);
+
+ controller.run();
+ }
+}
diff --git a/Week14 Route 66/src/OO14route66/TableView.java b/Week14 Route 66/src/OO14route66/TableView.java
new file mode 100644
index 0000000..9f2546f
--- /dev/null
+++ b/Week14 Route 66/src/OO14route66/TableView.java
@@ -0,0 +1,65 @@
+package OO14route66;
+
+import java.awt.Graphics;
+import java.awt.GridLayout;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+
+/**
+ * OO14route66 initial class
+ * @author Pieter Koopman
+ */
+public class TableView extends JFrame
+{
+ JPanel panel;
+ public static final int WINDOWWITDH = 100 * Model.DIRECTIONS, WINDOWHEIGTH = 200;
+ JLabel [] textLabels;
+ private final Model model; // for the positions of the cars
+
+ /**
+ * The constructor of TableView
+ * @param model containing the position of the cars to display
+ * makes a panel with a gridLayout and a JLabel for each car in this panel
+ */
+ TableView(Model model) {
+ super("Route66 table view");
+ this.model = model;
+ setSize(WINDOWWITDH, WINDOWHEIGTH);
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ panel = new JPanel();
+ panel.setLayout(new GridLayout(Model.NUMBEROFCARS / Model.DIRECTIONS + 1, Model.DIRECTIONS));
+
+ for (int i = 0; i < Model.DIRECTIONS; i += 1) { // make table headings
+ panel.add(new JLabel(Direction.intToDirection(i).toString()));
+ }
+
+ textLabels = new JLabel[Model.NUMBEROFCARS]; // make car info
+ for (int c = 0; c < Model.NUMBEROFCARS; c += 1) {
+ textLabels[c] = new JLabel(label(c));
+ panel.add(textLabels[c]);
+ }
+ add(panel);
+ }
+
+ /**
+ * update the JLabel for each car
+ * @param g
+ */
+ @Override
+ public void paint(Graphics g) {
+ super.paint(g);
+ for (int c = 0; c < Model.NUMBEROFCARS; c += 1) {
+ textLabels[c].setText(label(c));
+ }
+ }
+
+ /**
+ * create a label for the given car number
+ * @param c: car number
+ * @return string representing the car information
+ */
+ private String label(int c) {
+ return c + ": " + model.getCar(c).getLocation();
+ }
+}
diff --git a/Week14 Route 66/src/com/camilstaps/route66/Week14Route66.java b/Week14 Route 66/src/com/camilstaps/route66/Week14Route66.java
new file mode 100644
index 0000000..30b7bd2
--- /dev/null
+++ b/Week14 Route 66/src/com/camilstaps/route66/Week14Route66.java
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2015 Camil Staps
+ */
+package com.camilstaps.route66;
+
+/**
+ *
+ * @author camilstaps
+ */
+public class Week14Route66 {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ // TODO code application logic here
+ }
+
+}