From cbb41f8025105e455993db2dee52d4234aed0aeb Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 26 May 2015 10:12:10 +0200 Subject: Start w14 --- .gitignore | 4 +- Week14 Route 66/Assignment (in Dutch).pdf | Bin 0 -> 119872 bytes Week14 Route 66/build.xml | 73 + Week14 Route 66/manifest.mf | 3 + Week14 Route 66/nbproject/build-impl.xml | 1413 ++++++++++++++++++++ Week14 Route 66/nbproject/genfiles.properties | 8 + Week14 Route 66/nbproject/project.properties | 73 + Week14 Route 66/nbproject/project.xml | 15 + Week14 Route 66/src/OO14route66/Car.java | 97 ++ Week14 Route 66/src/OO14route66/Controller.java | 88 ++ Week14 Route 66/src/OO14route66/Direction.java | 55 + Week14 Route 66/src/OO14route66/KeyHandler.java | 39 + Week14 Route 66/src/OO14route66/Model.java | 60 + Week14 Route 66/src/OO14route66/Regelaar.java | 15 + Week14 Route 66/src/OO14route66/RoadView.java | 87 ++ Week14 Route 66/src/OO14route66/Route66.java | 42 + Week14 Route 66/src/OO14route66/TableView.java | 65 + .../src/com/camilstaps/route66/Week14Route66.java | 19 + 18 files changed, 2155 insertions(+), 1 deletion(-) create mode 100644 Week14 Route 66/Assignment (in Dutch).pdf create mode 100644 Week14 Route 66/build.xml create mode 100644 Week14 Route 66/manifest.mf create mode 100644 Week14 Route 66/nbproject/build-impl.xml create mode 100644 Week14 Route 66/nbproject/genfiles.properties create mode 100644 Week14 Route 66/nbproject/project.properties create mode 100644 Week14 Route 66/nbproject/project.xml create mode 100644 Week14 Route 66/src/OO14route66/Car.java create mode 100644 Week14 Route 66/src/OO14route66/Controller.java create mode 100644 Week14 Route 66/src/OO14route66/Direction.java create mode 100644 Week14 Route 66/src/OO14route66/KeyHandler.java create mode 100644 Week14 Route 66/src/OO14route66/Model.java create mode 100644 Week14 Route 66/src/OO14route66/Regelaar.java create mode 100644 Week14 Route 66/src/OO14route66/RoadView.java create mode 100644 Week14 Route 66/src/OO14route66/Route66.java create mode 100644 Week14 Route 66/src/OO14route66/TableView.java create mode 100644 Week14 Route 66/src/com/camilstaps/route66/Week14Route66.java diff --git a/.gitignore b/.gitignore index 098f52f..94654cb 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,6 @@ /Week13 Bar/nbproject/private/ /Week13 Bar/build/ /Week13 Die/build/ -/Week13 Die/dist/ \ No newline at end of file +/Week13 Die/dist/ +/Week14 Route 66/nbproject/private/ +/Week14 Route 66/build/ \ No newline at end of file diff --git a/Week14 Route 66/Assignment (in Dutch).pdf b/Week14 Route 66/Assignment (in Dutch).pdf new file mode 100644 index 0000000..fff252a Binary files /dev/null and b/Week14 Route 66/Assignment (in Dutch).pdf differ diff --git a/Week14 Route 66/build.xml b/Week14 Route 66/build.xml new file mode 100644 index 0000000..0c97930 --- /dev/null +++ b/Week14 Route 66/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project Week14 Route 66. + + + diff --git a/Week14 Route 66/manifest.mf b/Week14 Route 66/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/Week14 Route 66/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/Week14 Route 66/nbproject/build-impl.xml b/Week14 Route 66/nbproject/build-impl.xml new file mode 100644 index 0000000..b024607 --- /dev/null +++ b/Week14 Route 66/nbproject/build-impl.xml @@ -0,0 +1,1413 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Week14 Route 66/nbproject/genfiles.properties b/Week14 Route 66/nbproject/genfiles.properties new file mode 100644 index 0000000..ed32eae --- /dev/null +++ b/Week14 Route 66/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=59f78847 +build.xml.script.CRC32=54ae1572 +build.xml.stylesheet.CRC32=8064a381@1.75.2.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=59f78847 +nbproject/build-impl.xml.script.CRC32=7ebb0687 +nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 diff --git a/Week14 Route 66/nbproject/project.properties b/Week14 Route 66/nbproject/project.properties new file mode 100644 index 0000000..faae679 --- /dev/null +++ b/Week14 Route 66/nbproject/project.properties @@ -0,0 +1,73 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/Week14_Route_66.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=com.camilstaps.route66.Week14Route66 +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/Week14 Route 66/nbproject/project.xml b/Week14 Route 66/nbproject/project.xml new file mode 100644 index 0000000..292a444 --- /dev/null +++ b/Week14 Route 66/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + Week14 Route 66 + + + + + + + + + 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 views; + + + /** + * Constructor: create all cars + */ + public Model() { + views = new ArrayList(); + 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 + } + +} -- cgit v1.2.3