Removed 'MVC template' because it was a bunch of redundant code. Moved 'New MVC' to 'MVC' for simplicity. Added Challenge 2 to the Git tracker. Challenge 2 is basically Challenge 1 all over again, horrible.
- Author
- Maarten Vangeneugden
- Date
- Dec. 18, 2015, 1:03 p.m.
- Hash
- eb353f11d8aaeed9d2af53bb61f2c9468dabf634
- Parent
- 4a4a80b00250b8ae025039fedac1bc2569219e2b
- Modified files
- Challenge 2/AbstractController.java
- Challenge 2/AbstractView.java
- Challenge 2/ChoiceCourse.java
- Challenge 2/Controller.java
- Challenge 2/Course.java
- Challenge 2/DetailedCourseView.java
- Challenge 2/Main.java
- Challenge 2/MainWindow.java
- Challenge 2/MandatoryCourse.java
- Challenge 2/Planner.java
- Challenge 2/SelectionProcedure.java
- Challenge 2/Student.java
- Challenge 2/StudyPath.java
- Challenge 2/StudyPathController.java
- Challenge 2/StudyPathView.java
- Challenge 2/ontwerpkeuzes.txt
- Challenge 2/study.java
- MVC template/AbstractController.java
- MVC template/AbstractView.java
- MVC template/Clock.java
- MVC template/ClockAnalogView.java
- MVC template/ClockController.java
- MVC template/ClockDigitalView.java
- MVC template/ClockModel.java
- MVC template/ClockTools.java
- MVC template/ClockUpdate.java
- MVC template/Controller.java
- MVC template/DigitalClockController.java
- MVC template/View.java
- MVC template/readme.txt
- New MVC/AbstractController.java → MVC/AbstractController.java
- New MVC/AbstractModel.java → MVC/AbstractModel.java
- New MVC/AbstractView.java → MVC/AbstractView.java
- New MVC/Controller.java → MVC/Controller.java
- New MVC/MainWindow.java → MVC/MainWindow.java
Challenge 2/AbstractController.java ¶
51 additions and 0 deletions.
View changes Hide changes
+ |
1 |
import java.util.Observer; // Idem for Observers. |
+ |
2 |
|
+ |
3 |
/** |
+ |
4 |
* Controller - Abstraction layer to represent a controller in an MVC architecture. |
+ |
5 |
* @author Maarten Vangeneugden - 1438256 |
+ |
6 |
*/ |
+ |
7 |
abstract class Controller { |
+ |
8 |
|
+ |
9 |
// Members |
+ |
10 |
private Observable model; |
+ |
11 |
private Observer view; |
+ |
12 |
|
+ |
13 |
// Constructor |
+ |
14 |
public Controller(Observable model, Observer view) { |
+ |
15 |
this.model = model; |
+ |
16 |
this.view = view; |
+ |
17 |
} |
+ |
18 |
|
+ |
19 |
/** |
+ |
20 |
* Sets the model for this controller. |
+ |
21 |
* @param model The model that is to be controlled. |
+ |
22 |
*/ |
+ |
23 |
public void setModel(Observable model) { |
+ |
24 |
this.model = model; |
+ |
25 |
} |
+ |
26 |
|
+ |
27 |
/** |
+ |
28 |
* Returns the model that is being observed. |
+ |
29 |
* @return The model being observed. |
+ |
30 |
*/ |
+ |
31 |
public Observable getModel() { |
+ |
32 |
return model; |
+ |
33 |
} |
+ |
34 |
|
+ |
35 |
/** |
+ |
36 |
* Sets the view for this controller. |
+ |
37 |
* @param view The view for this controller. |
+ |
38 |
*/ |
+ |
39 |
public void setView(Observer view) { |
+ |
40 |
this.view = view; |
+ |
41 |
} |
+ |
42 |
|
+ |
43 |
/** |
+ |
44 |
* Returns the view for this controller. |
+ |
45 |
* @return The view for this controller. |
+ |
46 |
*/ |
+ |
47 |
public Observer getView() { |
+ |
48 |
return view; |
+ |
49 |
} |
+ |
50 |
} |
+ |
51 |
Challenge 2/AbstractView.java ¶
62 additions and 0 deletions.
View changes Hide changes
+ |
1 |
import java.util.Observer; |
+ |
2 |
|
+ |
3 |
/** |
+ |
4 |
* AbstractView - An abstraction layer for a view class in an MVC architecture. |
+ |
5 |
* @author Maarten Vangeneugden - 1438256 |
+ |
6 |
*/ |
+ |
7 |
abstract class AbstractView implements Observer { |
+ |
8 |
|
+ |
9 |
// Members |
+ |
10 |
private Observable model; |
+ |
11 |
private Controller controller; |
+ |
12 |
|
+ |
13 |
// Constructor |
+ |
14 |
public AbstractView(Observable model, Controller controller) { |
+ |
15 |
setModel(model); |
+ |
16 |
setController(controller); |
+ |
17 |
} |
+ |
18 |
|
+ |
19 |
/** |
+ |
20 |
* Sets the model associated with this view. |
+ |
21 |
* @param model The model associated with this view. |
+ |
22 |
*/ |
+ |
23 |
public void setModel(Observable model) { |
+ |
24 |
this.model = model; |
+ |
25 |
} |
+ |
26 |
|
+ |
27 |
/** |
+ |
28 |
* Returns the model associated with this view. |
+ |
29 |
* @return The model associated with this view. |
+ |
30 |
*/ |
+ |
31 |
public Observable getModel() { |
+ |
32 |
return model; |
+ |
33 |
} |
+ |
34 |
|
+ |
35 |
/** |
+ |
36 |
* Sets the controller associated with this view. |
+ |
37 |
* @param controller The controller associated with this view. |
+ |
38 |
*/ |
+ |
39 |
public void setController(Controller controller) { |
+ |
40 |
this.controller = controller; |
+ |
41 |
} |
+ |
42 |
|
+ |
43 |
/** |
+ |
44 |
* Returns the controller associated with this view. |
+ |
45 |
* @return The controller associated with this view. |
+ |
46 |
*/ |
+ |
47 |
public Controller getController() { |
+ |
48 |
return controller; |
+ |
49 |
} |
+ |
50 |
|
+ |
51 |
/** |
+ |
52 |
* This is an abstract method, in that it must be overridden in order to use this class. The method itself is an override of Observer.update(). |
+ |
53 |
* Please note that this function is not supposed to be called because that's something you can do with functions (methods, whatevs); This function is automatically triggered by the Observable class whenever its state is modified. So what is supposed to happen here, is that the subclass of this class takes the trigger class, and asks about its state to update his own stuff. |
+ |
54 |
* @see Observable.update |
+ |
55 |
* @param observable The class that is being observed. This is also the class that triggers this function. It's the observed class screaming it had an update, if you will. It literally sends itself as a parameter. |
+ |
56 |
* @param information An Object containing additional information. This can be any class, so it's up to the subclass to deal with this, or not to deal with it at all. |
+ |
57 |
* @post The View is updated so that it is a correct representation of its associated Model. |
+ |
58 |
*/ |
+ |
59 |
@Override |
+ |
60 |
public abstract void update(Observable observable, Object information); |
+ |
61 |
} |
+ |
62 |
Challenge 2/ChoiceCourse.java ¶
10 additions and 0 deletions.
View changes Hide changes
+ |
1 |
* A course that is not mandatory, therefore offered as a choice to the student. |
+ |
2 |
* Note that this is an "empty" class, since its implementation is completely given by Course. yet again, Course does not explicitely states that it is a choice course. Plus, using ChoiceCourse adds to the open/closed principle. |
+ |
3 |
* @author Maarten Vangeneugden - 1438256 |
+ |
4 |
*/ |
+ |
5 |
class ChoiceCourse extends Course { |
+ |
6 |
public ChoiceCourse(String name, int studyPoints, int semester, int ID, boolean pass) { |
+ |
7 |
super(name, studyPoints, semester, ID, pass); |
+ |
8 |
} |
+ |
9 |
} |
+ |
10 |
Challenge 2/Controller.java ¶
56 additions and 0 deletions.
View changes Hide changes
+ |
1 |
import java.util.Observer; |
+ |
2 |
|
+ |
3 |
/** |
+ |
4 |
* Controller - An abstract controller for an MVC-architecture. |
+ |
5 |
* @author Maarten Vangeneugden - 1438256 |
+ |
6 |
*/ |
+ |
7 |
class Controller { |
+ |
8 |
|
+ |
9 |
// Members |
+ |
10 |
private Observable model; |
+ |
11 |
private Observer view; |
+ |
12 |
|
+ |
13 |
// Constructor |
+ |
14 |
public Controller(Observable model, Observer view) { |
+ |
15 |
setModel(model); |
+ |
16 |
setView(view); |
+ |
17 |
} |
+ |
18 |
|
+ |
19 |
/** |
+ |
20 |
* Sets the model for this controller. |
+ |
21 |
* @param model The model that is to be controlled. |
+ |
22 |
*/ |
+ |
23 |
public void setModel(Observable model) { |
+ |
24 |
this.model.deleteObserver(this.view); |
+ |
25 |
this.model = model; |
+ |
26 |
this.model.addObserver(this.view); |
+ |
27 |
} |
+ |
28 |
|
+ |
29 |
/** |
+ |
30 |
* Returns the model that is being observed. |
+ |
31 |
* @return The model being observed. |
+ |
32 |
*/ |
+ |
33 |
public Observable getModel() { |
+ |
34 |
return model; |
+ |
35 |
} |
+ |
36 |
|
+ |
37 |
/** |
+ |
38 |
* Sets the view for this controller. |
+ |
39 |
* @param view The view for this controller. |
+ |
40 |
*/ |
+ |
41 |
public void setView(Observer view) { |
+ |
42 |
getModel().deleteObserver(this.view); |
+ |
43 |
this.view = view; |
+ |
44 |
getModel().addObserver(this.view); |
+ |
45 |
} |
+ |
46 |
|
+ |
47 |
/** |
+ |
48 |
* Returns the view for this controller. |
+ |
49 |
* @return The view for this controller. |
+ |
50 |
*/ |
+ |
51 |
public Observer getView() { |
+ |
52 |
return view; |
+ |
53 |
} |
+ |
54 |
|
+ |
55 |
} |
+ |
56 |
Challenge 2/Course.java ¶
71 additions and 0 deletions.
View changes Hide changes
+ |
1 |
|
+ |
2 |
/** |
+ |
3 |
* Course: Represents a course that is a part of a study. |
+ |
4 |
* @author Maarten Vangeneugden - 1438256 |
+ |
5 |
*/ |
+ |
6 |
class Course { |
+ |
7 |
private String m_name; // The name of the course. |
+ |
8 |
public String name() { |
+ |
9 |
return m_name; |
+ |
10 |
} |
+ |
11 |
public void setName(String name) { |
+ |
12 |
m_name = name; |
+ |
13 |
} |
+ |
14 |
|
+ |
15 |
private int m_capacity; // The maximum amount of allowed students. |
+ |
16 |
public int capacity() { |
+ |
17 |
return m_capacity; |
+ |
18 |
} |
+ |
19 |
public void setCapacity(int capacity) { |
+ |
20 |
m_capacity = capacity; |
+ |
21 |
} |
+ |
22 |
|
+ |
23 |
private int m_studyPoints; // An integer value representing how much study points this course has. |
+ |
24 |
public int studyPoints() { |
+ |
25 |
return m_studyPoints; |
+ |
26 |
} |
+ |
27 |
public void setStudyPoints(int studyPoints) { |
+ |
28 |
m_studyPoints = studyPoints; |
+ |
29 |
} |
+ |
30 |
|
+ |
31 |
private int m_semester; // Integer that represents in which semester this course takes place. |
+ |
32 |
public int semester() { |
+ |
33 |
return m_semester; |
+ |
34 |
} |
+ |
35 |
public void setSemester(int semester) { |
+ |
36 |
m_semester = semester; |
+ |
37 |
} |
+ |
38 |
|
+ |
39 |
private int m_ID; // Integer value representing the unique ID of this course. |
+ |
40 |
public int ID() { |
+ |
41 |
return m_ID; |
+ |
42 |
} |
+ |
43 |
public void setID(int ID) { |
+ |
44 |
m_ID = ID; |
+ |
45 |
} |
+ |
46 |
|
+ |
47 |
private List<Course> m_requiredCourses; // A list of courses that are required to follow this course. |
+ |
48 |
public List<Course> requiredCourses() { |
+ |
49 |
return m_requiredCourses; |
+ |
50 |
} |
+ |
51 |
public void setRequiredCourses(List<Course> requiredCourses) { |
+ |
52 |
m_requiredCourses = requiredCourses; |
+ |
53 |
} |
+ |
54 |
|
+ |
55 |
private boolean m_passed; // A boolean value that holds whether this course has been passed, indicating a credit for it has been acquired by the student. |
+ |
56 |
public boolean creditAcquired() { |
+ |
57 |
return m_passed; |
+ |
58 |
} |
+ |
59 |
public void setPassed(boolean pass) { |
+ |
60 |
m_passed = pass; |
+ |
61 |
} |
+ |
62 |
|
+ |
63 |
public Course(String name, int studyPoints, int semester, int ID, boolean pass) { |
+ |
64 |
setName(name); |
+ |
65 |
setStudyPoints(studyPoints); |
+ |
66 |
setSemester(semester); |
+ |
67 |
setID(ID); |
+ |
68 |
setPassed(pass); |
+ |
69 |
} |
+ |
70 |
} |
+ |
71 |
Challenge 2/DetailedCourseView.java ¶
75 additions and 0 deletions.
View changes Hide changes
+ |
1 |
import javax.swing.*; |
+ |
2 |
|
+ |
3 |
/** |
+ |
4 |
* This class creates a JPanel using the given course. This JPanel is then a detailed description of said course. |
+ |
5 |
* @author Maarten Vangeneugden - 1438256 |
+ |
6 |
*/ |
+ |
7 |
|
+ |
8 |
class DetailedCourseView extends AbstractView { |
+ |
9 |
private Course m_course; // The course of this class. |
+ |
10 |
public Course course() { |
+ |
11 |
return m_course; |
+ |
12 |
} |
+ |
13 |
private void setCourse(Course course) { // Sets the course, but also triggers to update the panel itself. This is necessary, since the panel will otherwise not be a representation of the course itself. |
+ |
14 |
m_course = course; |
+ |
15 |
setContent(); |
+ |
16 |
} |
+ |
17 |
|
+ |
18 |
private JPanel m_panel; // The panel that will represent the course. |
+ |
19 |
public JPanel panel() { |
+ |
20 |
return m_panel; |
+ |
21 |
} |
+ |
22 |
private void setPanel(JPanel panel) { |
+ |
23 |
m_panel = panel; |
+ |
24 |
} |
+ |
25 |
//There is no public panel setter. The panel can only be changed by modifying the course, since this panel represents the course. |
+ |
26 |
|
+ |
27 |
public DetailedCourseView(Course course) { |
+ |
28 |
super(course, null); |
+ |
29 |
setCourse(course); |
+ |
30 |
} |
+ |
31 |
|
+ |
32 |
/** |
+ |
33 |
* Will update the panel of the class according to the given course. |
+ |
34 |
* @pre The course of the class is not null. |
+ |
35 |
* @post The panel of the class is updated and is now an update representation of the given course. |
+ |
36 |
*/ |
+ |
37 |
private void setContent() { // Creates the content for this JPanel. Not that it's private; Its functionality may only be triggered by its own instance. |
+ |
38 |
// First, we handle the common members, since we know the base class can't be smaller than inherited classes. |
+ |
39 |
JLabel lblName = new JLabel("Naam: " + course().name()); |
+ |
40 |
JLabel lblID = new JLabel("ID: " + Integer.toString(course().ID())); |
+ |
41 |
JLabel lblStudyPoints = new JLabel("Studiepunten: " + Integer.toString(course().studyPoints())); |
+ |
42 |
JLabel lblSemester = new JLabel("Semester: " + Integer.toString(course().semester())); |
+ |
43 |
JLabel lblType = new JLabel(); // We wil fill this in later. |
+ |
44 |
|
+ |
45 |
JLabel lblPass = new JLabel(); |
+ |
46 |
if(course().creditAcquired()) |
+ |
47 |
lblPass.setText("Verworven credit"); |
+ |
48 |
else |
+ |
49 |
lblPass.setText("Credit niet verworven"); |
+ |
50 |
|
+ |
51 |
// Second, we do a RTTI on the given course by checking the instance type. (RTTI = RunTime Type Identification) |
+ |
52 |
if(course() instanceof MandatoryCourse) { |
+ |
53 |
lblType.setText("Verplicht vak - Jaar" + Integer.toString(((MandatoryCourse)course()).year())); |
+ |
54 |
} |
+ |
55 |
else if(course() instanceof ChoiceCourse) { |
+ |
56 |
lblType.setText("Keuzevak"); |
+ |
57 |
} |
+ |
58 |
|
+ |
59 |
// Third, we add all the labels to the panel. |
+ |
60 |
JPanel panel = new JPanel(); |
+ |
61 |
panel.add(lblName); |
+ |
62 |
panel.add(lblID); |
+ |
63 |
panel.add(lblStudyPoints); |
+ |
64 |
panel.add(lblSemester); |
+ |
65 |
panel.add(lblType); |
+ |
66 |
panel.add(lblPass); |
+ |
67 |
|
+ |
68 |
setPanel(panel); |
+ |
69 |
} |
+ |
70 |
|
+ |
71 |
private void update(Observable course, Object info) { |
+ |
72 |
setContent(course); |
+ |
73 |
} |
+ |
74 |
} |
+ |
75 |
Challenge 2/Main.java ¶
10 additions and 0 deletions.
Challenge 2/MainWindow.java ¶
78 additions and 0 deletions.
View changes Hide changes
+ |
1 |
import javax.swing.*; |
+ |
2 |
import java.util.*; |
+ |
3 |
import java.util.Arrays; |
+ |
4 |
|
+ |
5 |
/** |
+ |
6 |
* MainWindow - Abstraction layer for a window in an MVC architecture. It abstracts a lot of fluff caused by Swing being Swing. It also takes care of the state and layout of the window itself, therefore making 80% of what's most used in simple programs with a GUI possible in just 20% of the time it used to take. |
+ |
7 |
* @author Maarten Vangeneugden - 1438256 |
+ |
8 |
*/ |
+ |
9 |
class MainWindow extends AbstractView { |
+ |
10 |
|
+ |
11 |
private JFrame window; |
+ |
12 |
|
+ |
13 |
/** |
+ |
14 |
* Constructor. It creates an empty window. Period. |
+ |
15 |
* @param windowTitle The title of the created window. |
+ |
16 |
* @param widgets A list of widgets that can be added to the window. |
+ |
17 |
*/ |
+ |
18 |
public MainWindow(String windowTitle, List<JComponent> widgets) { |
+ |
19 |
|
+ |
20 |
super(null, null); |
+ |
21 |
// First, we create the window itself: |
+ |
22 |
JFrame window = new JFrame(windowTitle); // Create the new window. |
+ |
23 |
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set its close action. (Laymen's terms: Make it close down when you close it down.) |
+ |
24 |
setWindow(window); |
+ |
25 |
|
+ |
26 |
// Then, a JPanel: |
+ |
27 |
window.setContentPane(new JPanel()); |
+ |
28 |
|
+ |
29 |
// Then, we add the given widgets to it: |
+ |
30 |
if(widgets != null) { |
+ |
31 |
for (JComponent widget : widgets) { |
+ |
32 |
addWidget(widget); |
+ |
33 |
} |
+ |
34 |
} |
+ |
35 |
} |
+ |
36 |
|
+ |
37 |
public void makeVisible() { |
+ |
38 |
getWindow().pack(); |
+ |
39 |
getWindow().setVisible(true); |
+ |
40 |
} |
+ |
41 |
|
+ |
42 |
public void setWindow(JFrame window) { |
+ |
43 |
this.window = window; |
+ |
44 |
} |
+ |
45 |
|
+ |
46 |
public JFrame getWindow() { |
+ |
47 |
return window; |
+ |
48 |
} |
+ |
49 |
|
+ |
50 |
/** |
+ |
51 |
* Adds a given widget to the window. |
+ |
52 |
* It's highly recommended to have the widget's fluff (i.e. Actionlisteners) all prepped before adding. |
+ |
53 |
* @param widget The widget to be added to the window. |
+ |
54 |
*/ |
+ |
55 |
public void addWidget(JComponent widget) { |
+ |
56 |
getWindow().getContentPane().add(widget); |
+ |
57 |
} |
+ |
58 |
|
+ |
59 |
/** |
+ |
60 |
* Returns a list containing all widgets in this window. |
+ |
61 |
* @return A list of widgets. |
+ |
62 |
*/ |
+ |
63 |
/*public getWidgets() { |
+ |
64 |
return Arrays.asList(getWindow().getContentPane().getComponents()); |
+ |
65 |
}*/ |
+ |
66 |
|
+ |
67 |
/** |
+ |
68 |
* Removes the given widget from the window. |
+ |
69 |
* @param widget The widget that should be removed. |
+ |
70 |
* @post The given widget is no longer in the window. |
+ |
71 |
*/ |
+ |
72 |
public void removeWidget(JComponent widget) { |
+ |
73 |
getWindow().getContentPane().remove(widget); |
+ |
74 |
} |
+ |
75 |
|
+ |
76 |
public void update(Observable nope, Object no) {} |
+ |
77 |
} |
+ |
78 |
Challenge 2/MandatoryCourse.java ¶
18 additions and 0 deletions.
View changes Hide changes
+ |
1 |
* A mandatory course, representing a course that must be followed during the study. |
+ |
2 |
* @author Maarten Vangeneugden - 1438256 |
+ |
3 |
*/ |
+ |
4 |
class MandatoryCourse extends Course { |
+ |
5 |
private int m_year; // The year in which this course is given. |
+ |
6 |
public int year() { |
+ |
7 |
return m_year; |
+ |
8 |
} |
+ |
9 |
public void setYear(int year) { |
+ |
10 |
m_year = year; |
+ |
11 |
} |
+ |
12 |
|
+ |
13 |
public MandatoryCourse(String name, int studyPoints, int semester, int ID, boolean pass, int year) { |
+ |
14 |
super(name, studyPoints, semester, ID, pass); |
+ |
15 |
setYear(year); |
+ |
16 |
} |
+ |
17 |
} |
+ |
18 |
Challenge 2/Planner.java ¶
11 additions and 0 deletions.
View changes Hide changes
+ |
1 |
* This class will call the appropriate windows to use the program. |
+ |
2 |
* @author Maarten Vangeneugden - 1438256 |
+ |
3 |
*/ |
+ |
4 |
class Planner { |
+ |
5 |
public static void main(String[] args) { |
+ |
6 |
Course course = new MandatoryCourse("Informatiesystemen", 8, 1, 6845, true, 2); |
+ |
7 |
DetailedCourseView view = new DetailedCourseView(course); |
+ |
8 |
MainWindow window = new MainWindow("Ding", view.panel()); |
+ |
9 |
} |
+ |
10 |
} |
+ |
11 |
Challenge 2/SelectionProcedure.java ¶
35 additions and 0 deletions.
View changes Hide changes
+ |
1 |
import javax.swing.*; |
+ |
2 |
|
+ |
3 |
/** |
+ |
4 |
* SelectionProcedure - A view that gives the user a choice over which selection procedure to follow. |
+ |
5 |
* @author Maarten Vangeneugden |
+ |
6 |
*/ |
+ |
7 |
public class SelectionProcedure { |
+ |
8 |
|
+ |
9 |
public static void main(String[] args) { |
+ |
10 |
MainWindow window = new MainWindow("Selectieprocedure inschrijvingen", null); |
+ |
11 |
ButtonGroup options = new ButtonGroup(); |
+ |
12 |
JRadioButton fifo = new JRadioButton("Eerdere aanmelding heeft voorrang"); |
+ |
13 |
JRadioButton next = new JRadioButton("Voorrang volgens studentennummer"); |
+ |
14 |
JRadioButton random = new JRadioButton("Willekeurige voorrang"); |
+ |
15 |
|
+ |
16 |
JButton accept = new JButton("Doorgaan"); |
+ |
17 |
accept.addActionListener(actionPerformed); |
+ |
18 |
|
+ |
19 |
options.add(fifo); |
+ |
20 |
options.add(next); |
+ |
21 |
options.add(random); |
+ |
22 |
|
+ |
23 |
window.addWidget(fifo); |
+ |
24 |
window.addWidget(next); |
+ |
25 |
window.addWidget(random); |
+ |
26 |
|
+ |
27 |
window.makeVisible(); |
+ |
28 |
} |
+ |
29 |
|
+ |
30 |
public static void actionPerformed(ActionEvent e) { |
+ |
31 |
// This will close the window and send the choice to its listeners. |
+ |
32 |
} |
+ |
33 |
|
+ |
34 |
} |
+ |
35 |
Challenge 2/Student.java ¶
21 additions and 0 deletions.
View changes Hide changes
+ |
1 |
* Student - Represents a student. |
+ |
2 |
* @author Maarten Vangeneugden - 1438256 |
+ |
3 |
*/ |
+ |
4 |
public class Student { |
+ |
5 |
|
+ |
6 |
private int studentNumber; |
+ |
7 |
|
+ |
8 |
public Student(int studentNumber) { |
+ |
9 |
this.studentNumber = studentNumber; |
+ |
10 |
} |
+ |
11 |
|
+ |
12 |
public void setStudentNumber(int studentNumber) { |
+ |
13 |
this.studentNumber = studentNumber; |
+ |
14 |
} |
+ |
15 |
|
+ |
16 |
public int getStudentNumber() { |
+ |
17 |
return studentNumber; |
+ |
18 |
} |
+ |
19 |
|
+ |
20 |
} |
+ |
21 |
Challenge 2/StudyPath.java ¶
69 additions and 0 deletions.
View changes Hide changes
+ |
1 |
import java.util.Observable; |
+ |
2 |
/** |
+ |
3 |
* StudyPath describes a study path that a student wants to follow. it offers algorithms for suggesting paths and for checking the validity of the studypath. |
+ |
4 |
* @author Maarten Vangeneugden - 1438256 |
+ |
5 |
*/ |
+ |
6 |
class StudyPath extends Observable { |
+ |
7 |
private Study m_study; // The study the student wants to follow. |
+ |
8 |
public Study study() { |
+ |
9 |
return m_study; |
+ |
10 |
} |
+ |
11 |
public void setStudy(Study study) { |
+ |
12 |
m_study = study; |
+ |
13 |
} |
+ |
14 |
|
+ |
15 |
public int studyPoints() { |
+ |
16 |
int studyPoints = 0; |
+ |
17 |
for(Course course : courses()) { |
+ |
18 |
studyPoints += course.studyPoints(); |
+ |
19 |
} |
+ |
20 |
return studyPoints; |
+ |
21 |
} |
+ |
22 |
|
+ |
23 |
private List<Course> m_courses; // A list containing all courses that can be chosen in this study. |
+ |
24 |
public List<Course> courses() { |
+ |
25 |
return m_courses; |
+ |
26 |
} |
+ |
27 |
public void setCourses(List<Course> courses) { |
+ |
28 |
m_courses = courses; |
+ |
29 |
} |
+ |
30 |
|
+ |
31 |
/** |
+ |
32 |
* Checks if the given study path is valid. |
+ |
33 |
* @return A boolean value. False if the given study is not valid, true if the study path can be done. |
+ |
34 |
* @pre The list of courses and the study are complete. |
+ |
35 |
* @post Nothing's changed. |
+ |
36 |
*/ |
+ |
37 |
public boolean isValid() { // Checks whether the given study path is valid. |
+ |
38 |
int studyPoints = 0; |
+ |
39 |
for(Course course : courses()) { |
+ |
40 |
studyPoints += course.studyPoints(); |
+ |
41 |
if(course.requiredCourses().size() != 0) { |
+ |
42 |
for(Course requiredCourse : course.requiredCourses()) { |
+ |
43 |
if(course.requiredCourses().indexOf(requiredCourse) == -1) { // If the required course is not in the study path: |
+ |
44 |
return false; |
+ |
45 |
} |
+ |
46 |
else if(requiredCourse.creditAcquired() == false) { |
+ |
47 |
return false; |
+ |
48 |
} |
+ |
49 |
} |
+ |
50 |
} |
+ |
51 |
} |
+ |
52 |
if(!courses().containsAll(study().mandatoryCourses())) { // If the path does not contain all required courses: |
+ |
53 |
return false; |
+ |
54 |
} |
+ |
55 |
if(studyPoints<60 || studyPoints > 66) { |
+ |
56 |
return false; |
+ |
57 |
} |
+ |
58 |
|
+ |
59 |
return true; |
+ |
60 |
} |
+ |
61 |
|
+ |
62 |
/** |
+ |
63 |
* Returns a list of courses that can be followed. |
+ |
64 |
* @return see description |
+ |
65 |
*/ |
+ |
66 |
public List<Course> suggestedCourses() { |
+ |
67 |
} |
+ |
68 |
} |
+ |
69 |
Challenge 2/StudyPathController.java ¶
15 additions and 0 deletions.
View changes Hide changes
+ |
1 |
* StudyPathController - Supposed to be the controller of StudyPath and StudyPathView. |
+ |
2 |
* @see StudyPath |
+ |
3 |
* @see StudyPathView |
+ |
4 |
* @author Maarten Vangeneugden - 1438256 |
+ |
5 |
*/ |
+ |
6 |
public class StudyPathController extends Controller { |
+ |
7 |
public StudyPathController(Observable model, Observer view) { |
+ |
8 |
super(model, view); |
+ |
9 |
} |
+ |
10 |
|
+ |
11 |
// Supposed to go here: Functions that are called by the actionlisteners of the View. That way the controller can make the requested changes in the model. |
+ |
12 |
|
+ |
13 |
|
+ |
14 |
} |
+ |
15 |
Challenge 2/StudyPathView.java ¶
29 additions and 0 deletions.
View changes Hide changes
+ |
1 |
import java.util.Observer; |
+ |
2 |
import javax.swing.*; |
+ |
3 |
|
+ |
4 |
/** |
+ |
5 |
* StudyPathView - View class of the StudyPath and StudyPathController. |
+ |
6 |
* @see StudyPath |
+ |
7 |
* @see StudyPathController |
+ |
8 |
* @author Maarten Vangeneugden - 1438256 |
+ |
9 |
*/ |
+ |
10 |
public class StudyPathView extends AbstractView { |
+ |
11 |
public StudyPathView(StudyPath studyPath) { |
+ |
12 |
super(studyPath, null); |
+ |
13 |
createGUI(); |
+ |
14 |
} |
+ |
15 |
|
+ |
16 |
private void createGUI() { |
+ |
17 |
StudyPath studyPath = (StudyPath) getModel(); |
+ |
18 |
JList<Course> takenStudies = new JList<Course>(studyPath.courses()); |
+ |
19 |
JLabel lblStudyPoints = new JLabel((studyPath.studyPoints())); |
+ |
20 |
|
+ |
21 |
} |
+ |
22 |
|
+ |
23 |
|
+ |
24 |
|
+ |
25 |
private void update(Observable updatedStudyPath, Object info) { |
+ |
26 |
createGUI(); |
+ |
27 |
} |
+ |
28 |
} |
+ |
29 |
Challenge 2/ontwerpkeuzes.txt ¶
13 additions and 0 deletions.
View changes Hide changes
+ |
1 |
|
+ |
2 |
Er werd gevraagd om contractueel te programmeren. Dit houdt in dat pre- en postcondities worden gegeven, parameters beschreven, ... Ik heb deze bij de get- en setfuncties desondanks achterwege gelaten, omdat het contract bij deze functies te triviaal is, de tijd beperkt was, en omdat ik verwacht dat andere programmeurs zodanig veel verstand hebben dat ze het contract kunnen afleiden uit de context. |
+ |
3 |
|
+ |
4 |
Voor de opleidingsonderdelen heb ik besloten om een abstracte basisklasse te maken (Course) waarvan de verplichte vakken (MandatoryCourse) en keuzevakken (ChoiceCourse) zijn afgeleid. Ik tracht hiermee tegemoet te komen aan het open/closed-principe, zodat de code uitbreidbaar en overzichtelijk blijft. |
+ |
5 |
|
+ |
6 |
Ik heb ook bepaalde classes voorzien van een abstractielaag in de vorm van AbstractView, Controller, etc. om tegemoet te komen aan de MVC-architectuur. |
+ |
7 |
|
+ |
8 |
Ik heb de code toegevoegd van het selectieprocedurescherm. Deze is grotendeels af, maar moet nog gekoppeld worden aan een listener om de ontvangen waarde uit te lezen. |
+ |
9 |
|
+ |
10 |
Er is een Student-class toegevoegd, die een student voorstelt. |
+ |
11 |
|
+ |
12 |
De code zelf is volledig in het Engels. De GUI en (de inhoud van) opleidingsvakken zijn Nederlands. |
+ |
13 |
Challenge 2/study.java ¶
50 additions and 0 deletions.
View changes Hide changes
+ |
1 |
|
+ |
2 |
/** |
+ |
3 |
* Study: A class that describes a study that can be followed at the university. |
+ |
4 |
* @author Maarten Vangeugden - 1438256 |
+ |
5 |
*/ |
+ |
6 |
class Study { |
+ |
7 |
private String m_name; // The name of the study. |
+ |
8 |
public String name() { |
+ |
9 |
return m_name; |
+ |
10 |
} |
+ |
11 |
public void setName(String name) { |
+ |
12 |
m_name = name; |
+ |
13 |
} |
+ |
14 |
|
+ |
15 |
private List<MandatoryCourse> m_mandatoryCourses; // A list containing all mandatory courses tied to this study. |
+ |
16 |
public List<MandatoryCourse> mandatoryCourses() { |
+ |
17 |
return m_mandatoryCourses; |
+ |
18 |
} |
+ |
19 |
public void setMandatoryCourses(List<MandatoryCourse> mandatoryCourses) { |
+ |
20 |
m_mandatoryCourses = mandatoryCourses; |
+ |
21 |
} |
+ |
22 |
public void addMandatoryCourse(MandatoryCourse mandatoryCourse) { // This seems long and possibly redundant, but readibility outweighs size in my opinion. |
+ |
23 |
mandatoryCourses().add(mandatoryCourse); |
+ |
24 |
} |
+ |
25 |
|
+ |
26 |
private List<ChoiceCourse> m_choiceCourses; // A list containing all courses that can be chosen in this study. |
+ |
27 |
public List<ChoiceCourse> choiceCourses() { |
+ |
28 |
return m_choiceCourses; |
+ |
29 |
} |
+ |
30 |
public void setChoiceCourses(List<ChoiceCourse> choiceCourses) { |
+ |
31 |
m_choiceCourses = choiceCourses; |
+ |
32 |
} |
+ |
33 |
|
+ |
34 |
private int m_duration; // An integer representing the amount of years it takes to complete this study. |
+ |
35 |
public int duration() { |
+ |
36 |
return m_duration; |
+ |
37 |
} |
+ |
38 |
public void setDuration(int duration) { |
+ |
39 |
m_duration = duration; |
+ |
40 |
} |
+ |
41 |
|
+ |
42 |
private int m_studyPoints; // An integer value representing how much study points this study has. |
+ |
43 |
public int studyPoints() { |
+ |
44 |
return m_studyPoints; |
+ |
45 |
} |
+ |
46 |
public void setStudyPoints(int studyPoints) { |
+ |
47 |
m_studyPoints = studyPoints; |
+ |
48 |
} |
+ |
49 |
} |
+ |
50 |
MVC template/AbstractController.java ¶
0 additions and 34 deletions.
View changes Hide changes
1 |
- | |
2 |
- | public abstract class AbstractController implements Controller { |
3 |
- | private Observable mModel; |
4 |
- | private View mView; |
5 |
- | |
6 |
- | public AbstractController(Observable model) { |
7 |
- | // Set the model. |
8 |
- | setModel(model); |
9 |
- | } |
10 |
- | |
11 |
- | |
12 |
- | @Override |
13 |
- | public void setView(View view) { |
14 |
- | mView = view; |
15 |
- | } |
16 |
- | |
17 |
- | @Override |
18 |
- | public View getView() { |
19 |
- | return mView; |
20 |
- | } |
21 |
- | |
22 |
- | @Override |
23 |
- | public void setModel(Observable model) { |
24 |
- | mModel = model; |
25 |
- | } |
26 |
- | |
27 |
- | @Override |
28 |
- | public Observable getModel() { |
29 |
- | return mModel; |
30 |
- | } |
31 |
- | |
32 |
- | } |
33 |
- | |
34 |
- |
MVC template/AbstractView.java ¶
0 additions and 73 deletions.
View changes Hide changes
1 |
- | import java.util.Observer; |
2 |
- | |
3 |
- | /** |
4 |
- | * |
5 |
- | * @author jvermeulen |
6 |
- | */ |
7 |
- | public abstract class AbstractView implements View, Observer { |
8 |
- | |
9 |
- | private Observable mModel; |
10 |
- | private Controller mController; |
11 |
- | |
12 |
- | /** |
13 |
- | * Empty constructor so that the model and controller can be set later. |
14 |
- | */ |
15 |
- | public AbstractView() { |
16 |
- | } |
17 |
- | |
18 |
- | public AbstractView(Observable model, Controller controller) { |
19 |
- | // Set the model. |
20 |
- | setModel(model); |
21 |
- | // If a controller was supplied, use it. Otherwise let the first call to |
22 |
- | // getController() create the default controller. |
23 |
- | if (controller != null) { |
24 |
- | setController(controller); |
25 |
- | } |
26 |
- | } |
27 |
- | |
28 |
- | @Override |
29 |
- | public void setController(Controller controller) { |
30 |
- | mController = controller; |
31 |
- | // Tell the controller this object is its view. |
32 |
- | getController().setView(this); |
33 |
- | } |
34 |
- | |
35 |
- | @Override |
36 |
- | public Controller getController() { |
37 |
- | // If a controller hasn't been defined yet... |
38 |
- | if (mController == null) { |
39 |
- | // ...make one. Note that defaultController is normally overriden by |
40 |
- | // the AbstractView subclass so that it returns the appropriate |
41 |
- | // controller for the view. |
42 |
- | setController(defaultController(getModel())); |
43 |
- | } |
44 |
- | |
45 |
- | return mController; |
46 |
- | } |
47 |
- | |
48 |
- | @Override |
49 |
- | public void setModel(Observable model) { |
50 |
- | mModel = model; |
51 |
- | } |
52 |
- | |
53 |
- | @Override |
54 |
- | public Observable getModel() { |
55 |
- | return mModel; |
56 |
- | } |
57 |
- | |
58 |
- | @Override |
59 |
- | public Controller defaultController(Observable model) { |
60 |
- | return null; |
61 |
- | } |
62 |
- | |
63 |
- | /** |
64 |
- | * A do-nothing implementation of the Observer interface's update method. |
65 |
- | * Subclasses of AbstractView will provide a concrete implementation for |
66 |
- | * this method. |
67 |
- | */ |
68 |
- | @Override |
69 |
- | public void update(Observable o, Object arg) { |
70 |
- | } |
71 |
- | |
72 |
- | } |
73 |
- |
MVC template/Clock.java ¶
0 additions and 78 deletions.
View changes Hide changes
1 |
- | import java.awt.event.WindowEvent; |
2 |
- | import javax.swing.BoxLayout; |
3 |
- | import javax.swing.JFrame; |
4 |
- | |
5 |
- | /** |
6 |
- | * Main user interface for the application. Makes sure the model, views and |
7 |
- | * controllers are connected to each other. |
8 |
- | * @author jvermeulen |
9 |
- | */ |
10 |
- | public class Clock { |
11 |
- | // The clock data (i.e., the Model). |
12 |
- | private ClockModel mModel; |
13 |
- | |
14 |
- | // A digital view of the clock. |
15 |
- | private ClockDigitalView mDigitalView; |
16 |
- | |
17 |
- | // A toolbar for controlling the clock. |
18 |
- | private ClockTools mTools; |
19 |
- | |
20 |
- | // An analog view of the clock. |
21 |
- | private final ClockAnalogView mAnalogView; |
22 |
- | |
23 |
- | public Clock(int hour, int minute, int second) { |
24 |
- | // Create the data model. |
25 |
- | mModel = new ClockModel(); |
26 |
- | |
27 |
- | // Create the digital clock view. |
28 |
- | mDigitalView = new ClockDigitalView(mModel, null); |
29 |
- | mModel.addObserver(mDigitalView); |
30 |
- | |
31 |
- | // Create the analog clock view. |
32 |
- | mAnalogView = new ClockAnalogView(mModel, null); |
33 |
- | mModel.addObserver(mAnalogView); |
34 |
- | |
35 |
- | // Create the clock tools view. |
36 |
- | mTools = new ClockTools(mModel, null); |
37 |
- | mModel.addObserver(mTools); |
38 |
- | |
39 |
- | // Set the time. |
40 |
- | mModel.setTime(hour, minute, second); |
41 |
- | |
42 |
- | // Start the clock ticking. |
43 |
- | mModel.start(); |
44 |
- | } |
45 |
- | |
46 |
- | public void createGUI() { |
47 |
- | JFrame frame = new JFrame("Clock"); |
48 |
- | frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); |
49 |
- | frame.getContentPane().add(mDigitalView.getUI()); |
50 |
- | frame.getContentPane().add(mAnalogView); |
51 |
- | frame.getContentPane().add(mTools.getUI()); |
52 |
- | frame.addWindowListener(new WindowAdapter() { |
53 |
- | @Override |
54 |
- | public void windowClosing(WindowEvent e) { |
55 |
- | System.exit(0); |
56 |
- | } |
57 |
- | }); |
58 |
- | frame.pack(); |
59 |
- | frame.setVisible(true); |
60 |
- | } |
61 |
- | |
62 |
- | private static void createAndShowGUI() { |
63 |
- | Clock clock = new Clock(9, 10, 22); |
64 |
- | clock.createGUI(); |
65 |
- | } |
66 |
- | |
67 |
- | public static void main(String[] args) { |
68 |
- | //Schedule a job for the event-dispatching thread: |
69 |
- | //creating and showing this application's GUI. |
70 |
- | javax.swing.SwingUtilities.invokeLater(new Runnable() { |
71 |
- | @Override |
72 |
- | public void run() { |
73 |
- | createAndShowGUI(); |
74 |
- | } |
75 |
- | }); |
76 |
- | } |
77 |
- | } |
78 |
- |
MVC template/ClockAnalogView.java ¶
0 additions and 218 deletions.
View changes Hide changes
1 |
- | import java.awt.Dimension; |
2 |
- | import java.awt.Graphics; |
3 |
- | import java.awt.Graphics2D; |
4 |
- | import java.awt.RenderingHints; |
5 |
- | import java.awt.geom.AffineTransform; |
6 |
- | import java.awt.geom.Ellipse2D; |
7 |
- | import java.awt.geom.GeneralPath; |
8 |
- | import java.util.Calendar; |
9 |
- | import java.util.Observable; |
10 |
- | import javax.swing.JComponent; |
11 |
- | |
12 |
- | /** |
13 |
- | * An analog clock View for the ClockModel. This View has no user inputs, so |
14 |
- | * no controller is required. This class implements the View interface, and |
15 |
- | * subclasses JComponent directly, to illustrate that not all Views need to |
16 |
- | * inherit from AbstractView. |
17 |
- | * |
18 |
- | * Note: it is not necessary to understand all the details of the rendering |
19 |
- | * of the analog clock. |
20 |
- | * |
21 |
- | * @author jvermeulen (inspired by IBM Java2D tutorial) |
22 |
- | */ |
23 |
- | public class ClockAnalogView extends JComponent implements View { |
24 |
- | |
25 |
- | private Observable mModel; |
26 |
- | private Controller mController; |
27 |
- | |
28 |
- | int mHour; |
29 |
- | int mMinute; |
30 |
- | int mSecond; |
31 |
- | |
32 |
- | ClockAnalogView(Observable model, Controller controller) { |
33 |
- | mModel = model; |
34 |
- | mController = controller; |
35 |
- | } |
36 |
- | |
37 |
- | @Override |
38 |
- | public void setController(Controller controller) { |
39 |
- | mController = controller; |
40 |
- | } |
41 |
- | |
42 |
- | @Override |
43 |
- | public Controller getController() { |
44 |
- | return mController; |
45 |
- | } |
46 |
- | |
47 |
- | @Override |
48 |
- | public void setModel(Observable model) { |
49 |
- | mModel = model; |
50 |
- | } |
51 |
- | |
52 |
- | @Override |
53 |
- | public Observable getModel() { |
54 |
- | return mModel; |
55 |
- | } |
56 |
- | |
57 |
- | @Override |
58 |
- | public Controller defaultController(Observable model) { |
59 |
- | return null; |
60 |
- | } |
61 |
- | |
62 |
- | @Override |
63 |
- | public void update(Observable o, Object info) { |
64 |
- | // Cast info to ClockUpdate type. |
65 |
- | ClockUpdate clockInfo = (ClockUpdate) info; |
66 |
- | |
67 |
- | // Store hour, minute, second for repainting. |
68 |
- | mHour = clockInfo.getHour()+5; // +5 Geeft een latere tijdzone weer. Dit is namelijk opdracht 3. |
69 |
- | mMinute = clockInfo.getMinute(); |
70 |
- | mSecond = clockInfo.getSecond(); |
71 |
- | |
72 |
- | // Using a little trigonometry, set the transforms to rotate |
73 |
- | // each hand into the proper position. Center the rotation |
74 |
- | // around the pivot point (50, 50) instead of the origin |
75 |
- | hourTransform.setToRotation(((double) mHour) * |
76 |
- | (Math.PI / 6.0), 50, 50); |
77 |
- | minuteTransform.setToRotation(((double) mMinute) * |
78 |
- | (Math.PI / 30.0), 50, 50); |
79 |
- | secondTransform.setToRotation(((double) mSecond) * |
80 |
- | (Math.PI / 30.0), 50, 50); |
81 |
- | |
82 |
- | repaint(); |
83 |
- | } |
84 |
- | |
85 |
- | |
86 |
- | /** |
87 |
- | * Sets this components preferred size to 150x150. |
88 |
- | * @return |
89 |
- | */ |
90 |
- | @Override |
91 |
- | public Dimension getPreferredSize() { |
92 |
- | return new Dimension(100, 100); |
93 |
- | } |
94 |
- | |
95 |
- | // Create a shape for the face of the clock |
96 |
- | protected static Ellipse2D face = new Ellipse2D.Float(3, 3, 94, 94); |
97 |
- | |
98 |
- | // Create a path that represents a tick mark |
99 |
- | protected static GeneralPath tick = new GeneralPath(); |
100 |
- | static // http://www.jusfortechies.com/java/core-java/static-blocks.php |
101 |
- | { |
102 |
- | tick.moveTo(100, 100); |
103 |
- | tick.moveTo(49, 0); |
104 |
- | tick.lineTo(51, 0); |
105 |
- | tick.lineTo(51, 6); |
106 |
- | tick.lineTo(49, 6); |
107 |
- | tick.lineTo(49, 0); |
108 |
- | } |
109 |
- | |
110 |
- | // Create a cool hour hand |
111 |
- | protected static GeneralPath hourHand = new GeneralPath(); |
112 |
- | static |
113 |
- | { |
114 |
- | hourHand.moveTo(50, 15); |
115 |
- | hourHand.lineTo(53, 50); |
116 |
- | hourHand.lineTo(50, 53); |
117 |
- | hourHand.lineTo(47, 50); |
118 |
- | hourHand.lineTo(50, 15); |
119 |
- | } |
120 |
- | |
121 |
- | // Create a cool minute hand |
122 |
- | protected static GeneralPath minuteHand = new GeneralPath(); |
123 |
- | static |
124 |
- | { |
125 |
- | minuteHand.moveTo(50, 2); |
126 |
- | minuteHand.lineTo(53, 50); |
127 |
- | minuteHand.lineTo(50, 58); |
128 |
- | minuteHand.lineTo(47, 50); |
129 |
- | minuteHand.lineTo(50, 2); |
130 |
- | } |
131 |
- | |
132 |
- | // And a cool second hand |
133 |
- | protected static GeneralPath secondHand = new GeneralPath(); |
134 |
- | static |
135 |
- | { |
136 |
- | secondHand.moveTo(49, 5); |
137 |
- | secondHand.lineTo(51, 5); |
138 |
- | secondHand.lineTo(51, 62); |
139 |
- | secondHand.lineTo(49, 62); |
140 |
- | secondHand.lineTo(49, 5); |
141 |
- | } |
142 |
- | |
143 |
- | // Create some colors for the pieces of the clock |
144 |
- | protected static Color faceColor = new Color(220, 220, 220); |
145 |
- | protected static Color hourColor = Color.red.darker(); |
146 |
- | protected static Color minuteColor = Color.blue.darker(); |
147 |
- | protected static Color secondColor = new Color(180, 180, 0); |
148 |
- | protected static Color pinColor = Color.gray.brighter(); |
149 |
- | |
150 |
- | // Create circles for the pivot and center pin |
151 |
- | protected Ellipse2D pivot = new Ellipse2D.Float(47, 47, 6, 6); |
152 |
- | protected Ellipse2D centerPin = new Ellipse2D.Float(49, 49, 2, 2); |
153 |
- | |
154 |
- | |
155 |
- | // Create three transforms that center around the pivot point |
156 |
- | protected AffineTransform hourTransform = |
157 |
- | AffineTransform.getRotateInstance(0, 50, 50); |
158 |
- | protected AffineTransform minuteTransform = |
159 |
- | AffineTransform.getRotateInstance(0, 50, 50); |
160 |
- | protected AffineTransform secondTransform = |
161 |
- | AffineTransform.getRotateInstance(0, 50, 50); |
162 |
- | |
163 |
- | // Create a timer that fires once a second and a Calendar |
164 |
- | // instance for getting the time values |
165 |
- | // protected Timer timer = new Timer(1000, this); |
166 |
- | protected Calendar calendar = Calendar.getInstance(); |
167 |
- | |
168 |
- | // This is an alternative to creating a UI delegate. Since JPanel's |
169 |
- | // paint() method only paints the border and backgound, we can just |
170 |
- | // override the paint method of the component to do the graphics. |
171 |
- | public void paint(Graphics g) |
172 |
- | { |
173 |
- | // Call the superclass first to paint the border (if one is assigned) |
174 |
- | super.paint(g); |
175 |
- | |
176 |
- | |
177 |
- | // Get the graphics context and turn on anti-aliasing |
178 |
- | Graphics2D g2 = (Graphics2D) g; |
179 |
- | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
180 |
- | RenderingHints.VALUE_ANTIALIAS_ON); |
181 |
- | |
182 |
- | // Set the paint for the clock face and fill it in |
183 |
- | g2.setPaint(faceColor); |
184 |
- | g2.fill(face); |
185 |
- | |
186 |
- | // Set the paint to black and draw the clock's outline |
187 |
- | g2.setPaint(Color.black); |
188 |
- | g2.draw(face); |
189 |
- | |
190 |
- | // Fill in the 12 ticks around the face of the clock |
191 |
- | for (double p = 0.0; p < 12.0; p += 1.0) |
192 |
- | { |
193 |
- | // This is probably terribly inefficient and should be |
194 |
- | // done statically or in the constructor - draw the |
195 |
- | // tick as a transformed shape that is rotated. |
196 |
- | g2.fill(tick.createTransformedShape( |
197 |
- | AffineTransform.getRotateInstance((Math.PI / 6.0) * p, |
198 |
- | 50, 50))); |
199 |
- | } |
200 |
- | |
201 |
- | // Set the paint and draw the hour hand. It is lowest in the |
202 |
- | // 'z-order' so will appear underneath the other hands. Notice |
203 |
- | // how each hand is transformed by a different <AffineTransform>. |
204 |
- | g2.setPaint(hourColor); |
205 |
- | g2.fill(hourHand.createTransformedShape(hourTransform)); |
206 |
- | |
207 |
- | // Set the paint and draw the minute hand, the second hand, |
208 |
- | // the pivot and the center pin |
209 |
- | g2.setPaint(minuteColor); |
210 |
- | g2.fill(minuteHand.createTransformedShape(minuteTransform)); |
211 |
- | g2.setPaint(secondColor); |
212 |
- | g2.fill(secondHand.createTransformedShape(secondTransform)); |
213 |
- | g2.fill(pivot); |
214 |
- | g2.setPaint(pinColor); |
215 |
- | g2.fill(centerPin); |
216 |
- | } |
217 |
- | } |
218 |
- |
MVC template/ClockController.java ¶
0 additions and 42 deletions.
View changes Hide changes
1 |
- | import java.util.Calendar; |
2 |
- | |
3 |
- | /** |
4 |
- | * A clock controller that allows the clock to be started, stopped and reset. |
5 |
- | * @author jvermeulen |
6 |
- | */ |
7 |
- | |
8 |
- | //Toevoeging van Maarten V.: onSystemTime() voor updaten systeemtijd. Ook het aanpassen van ClockDigitalView wordt hier behandeld. |
9 |
- | //Extra toevoeging voor OEF04: onEdit() als de gebruiker via het tekstveld een aanpassing doorvoert. |
10 |
- | public class ClockController extends AbstractController { |
11 |
- | public ClockController(Observable model) { |
12 |
- | super(model); |
13 |
- | } |
14 |
- | |
15 |
- | public void onStart() { |
16 |
- | ((ClockModel)getModel()).start(); |
17 |
- | } |
18 |
- | |
19 |
- | public void onStop() { |
20 |
- | ((ClockModel)getModel()).stop(); |
21 |
- | } |
22 |
- | |
23 |
- | public void onReset() { |
24 |
- | ((ClockModel)getModel()).setTime(0,0,0); |
25 |
- | } |
26 |
- | |
27 |
- | public void onSystemTime() { |
28 |
- | Calendar calendar = Calendar.getInstance(); |
29 |
- | ((ClockModel)getModel()).setTime(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)); |
30 |
- | } |
31 |
- | |
32 |
- | public void onEdit(String givenTime) { |
33 |
- | String[] parts = givenTime.split(":"); |
34 |
- | System.out.println("Dit is al iets"); |
35 |
- | |
36 |
- | ((ClockModel)getModel()).setTime( |
37 |
- | Integer.parseInt(parts[0]), |
38 |
- | Integer.parseInt(parts[1]), |
39 |
- | Integer.parseInt(parts[2])); |
40 |
- | } |
41 |
- | } |
42 |
- |
MVC template/ClockDigitalView.java ¶
0 additions and 84 deletions.
View changes Hide changes
1 |
- | import java.util.Observable; |
2 |
- | import javax.swing.JComponent; |
3 |
- | import javax.swing.JTextField; |
4 |
- | import java.awt.event.ActionEvent; |
5 |
- | import java.awt.event.ActionListener; |
6 |
- | |
7 |
- | /** |
8 |
- | * A digital clock View for the ClockModel. This View has no user inputs, so no |
9 |
- | * Controller is required. |
10 |
- | * @author jvermeulen |
11 |
- | */ |
12 |
- | //BRING THE CHANGE! This should be updatable, so controllers and stuff will be added. |
13 |
- | public class ClockDigitalView extends AbstractView { |
14 |
- | |
15 |
- | // The user interface for this view. |
16 |
- | private JTextField mClock; |
17 |
- | |
18 |
- | public ClockDigitalView(Observable model, DigitalClockController controller) { |
19 |
- | super(model, controller); |
20 |
- | init(); |
21 |
- | } |
22 |
- | |
23 |
- | /** |
24 |
- | * Initializes the user interface. |
25 |
- | */ |
26 |
- | private void init() { |
27 |
- | mClock = new JTextField(); |
28 |
- | mClock.addActionListener(new ActionListener() { |
29 |
- | @Override |
30 |
- | public void actionPerformed(ActionEvent e) { |
31 |
- | ((DigitalClockController)getController()).onEdit(mClock.getText()); |
32 |
- | } |
33 |
- | }); |
34 |
- | } |
35 |
- | |
36 |
- | /** |
37 |
- | * Updates the state of the on-screen digital clock. |
38 |
- | * Invoked automatically by ClockModel. |
39 |
- | * @param o The ClockModel object that is broadcasting an update |
40 |
- | * @param info A ClockUpdate instance describing the changes that have |
41 |
- | * occurred in the ClockModel |
42 |
- | */ |
43 |
- | @Override |
44 |
- | public void update(Observable o, Object info) { |
45 |
- | // Cast info to ClockUpdate type. |
46 |
- | ClockUpdate clockInfo = (ClockUpdate) info; |
47 |
- | |
48 |
- | // Create a String representing the time. |
49 |
- | String timeString = formatTime(clockInfo); |
50 |
- | |
51 |
- | // Display the new time in the clock text field. |
52 |
- | mClock.setText(timeString); |
53 |
- | |
54 |
- | // Fade the color of the display if the clock isn't running. |
55 |
- | if (clockInfo.isRunning()) { |
56 |
- | mClock.setBackground(Color.white); |
57 |
- | } else { |
58 |
- | mClock.setBackground(Color.gray); |
59 |
- | } |
60 |
- | } |
61 |
- | |
62 |
- | /** |
63 |
- | * Convenience method to return the user interface component. We don't need |
64 |
- | * this if we implement View directly and directly subclass a GUI component. |
65 |
- | * @return the JComponent representing this View. |
66 |
- | */ |
67 |
- | @Override |
68 |
- | public Controller defaultController(Observable model) { |
69 |
- | return new DigitalClockController(model); |
70 |
- | } |
71 |
- | |
72 |
- | public JComponent getUI() { |
73 |
- | return mClock; |
74 |
- | } |
75 |
- | |
76 |
- | private String formatTime(ClockUpdate info) { |
77 |
- | return String.format("%d:%d:%d", |
78 |
- | info.getHour(), |
79 |
- | info.getMinute(), |
80 |
- | info.getSecond()); |
81 |
- | } |
82 |
- | |
83 |
- | } |
84 |
- |
MVC template/ClockModel.java ¶
0 additions and 153 deletions.
View changes Hide changes
1 |
- | import java.util.Date; |
2 |
- | import java.util.GregorianCalendar; |
3 |
- | import java.util.Observable; |
4 |
- | import java.util.Timer; |
5 |
- | import java.util.TimerTask; |
6 |
- | import javax.swing.SwingUtilities; |
7 |
- | |
8 |
- | /** |
9 |
- | * |
10 |
- | * @author jvermeulen |
11 |
- | */ |
12 |
- | public class ClockModel extends Observable { |
13 |
- | private int mHour; |
14 |
- | private int mMinute; |
15 |
- | private int mSecond; |
16 |
- | |
17 |
- | private final long mTickInterval = 1000; // tick every second |
18 |
- | private Timer mTimer; |
19 |
- | private boolean mIsRunning; |
20 |
- | |
21 |
- | public ClockModel() { |
22 |
- | // By default, set the clock time to the current system time. |
23 |
- | Calendar now = new GregorianCalendar(); |
24 |
- | setTime(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), |
25 |
- | now.get(Calendar.SECOND)); |
26 |
- | } |
27 |
- | |
28 |
- | public ClockModel(int hour, int minute, int second) { |
29 |
- | setTime(hour, minute, second); |
30 |
- | } |
31 |
- | /** |
32 |
- | * Helper class to run the tick() method multiple times |
33 |
- | * (e.g., every 1 second). |
34 |
- | */ |
35 |
- | class TickTask extends TimerTask { |
36 |
- | public void run() { |
37 |
- | tick(); |
38 |
- | } |
39 |
- | } |
40 |
- | |
41 |
- | public void start() { |
42 |
- | if (!mIsRunning) { |
43 |
- | mIsRunning = true; |
44 |
- | |
45 |
- | // Schedule the tick() method to run every mTickInterval milliseconds. |
46 |
- | mTimer = new Timer(); |
47 |
- | mTimer.schedule(new TickTask(), 0, mTickInterval); |
48 |
- | |
49 |
- | ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, |
50 |
- | mIsRunning); |
51 |
- | setChanged(); |
52 |
- | notifyObservers(info); |
53 |
- | } |
54 |
- | } |
55 |
- | |
56 |
- | public void stop() { |
57 |
- | if (mIsRunning) { |
58 |
- | mIsRunning = false; |
59 |
- | |
60 |
- | // Stop the timer. |
61 |
- | mTimer.cancel(); |
62 |
- | mTimer = null; |
63 |
- | |
64 |
- | ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, |
65 |
- | mIsRunning); |
66 |
- | setChanged(); |
67 |
- | notifyObservers(info); |
68 |
- | } |
69 |
- | } |
70 |
- | |
71 |
- | /** |
72 |
- | * Sets the current time. Notifies observers of any change in time. |
73 |
- | * @param hour The new hour. |
74 |
- | * @param minute The new minute |
75 |
- | * @param second The new second |
76 |
- | */ |
77 |
- | public void setTime(int hour, int minute, int second) { |
78 |
- | if (hour != mHour && isValidHour(hour)) { |
79 |
- | mHour = hour; |
80 |
- | setChanged(); |
81 |
- | } |
82 |
- | |
83 |
- | if (minute != mMinute && isValidMinute(minute)) { |
84 |
- | mMinute = minute; |
85 |
- | setChanged(); |
86 |
- | } |
87 |
- | |
88 |
- | if (second != mSecond && isValidSecond(second)) { |
89 |
- | mSecond = second; |
90 |
- | setChanged(); |
91 |
- | } |
92 |
- | |
93 |
- | // If the model has changed, notify Views. |
94 |
- | if (hasChanged()) { |
95 |
- | ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, |
96 |
- | mIsRunning); |
97 |
- | notifyObservers(info); |
98 |
- | } |
99 |
- | } |
100 |
- | |
101 |
- | /** |
102 |
- | * Checks to see if a number is a valid hour (i.e. in [0,23]). |
103 |
- | * @param hour The hour to check |
104 |
- | * @return True if this is a valid hour |
105 |
- | */ |
106 |
- | private boolean isValidHour(int hour) { |
107 |
- | return (hour >= 0 && hour <= 23); |
108 |
- | } |
109 |
- | |
110 |
- | /** |
111 |
- | * Checks to see if a number is a valid minute (i.e. in [0,59]). |
112 |
- | * @param minute The minute to check |
113 |
- | * @return True if this is a valid minute |
114 |
- | */ |
115 |
- | private boolean isValidMinute(int minute) { |
116 |
- | return (minute >= 0 && minute <= 59); |
117 |
- | } |
118 |
- | |
119 |
- | /** |
120 |
- | * Checks to see if a number is a valid minute (i.e. in [0,59]). |
121 |
- | * @param second The second to check |
122 |
- | * @return True if this is a valid second |
123 |
- | */ |
124 |
- | private boolean isValidSecond(int second) { |
125 |
- | return (second >= 0 && second <= 59); |
126 |
- | } |
127 |
- | |
128 |
- | private void tick() { |
129 |
- | // Get the current time |
130 |
- | int h = mHour; |
131 |
- | int m = mMinute; |
132 |
- | int s = mSecond; |
133 |
- | |
134 |
- | // Increment the current second, adjusting the minute and hour |
135 |
- | // if necessary. |
136 |
- | s += 1; |
137 |
- | if (s > 59) { |
138 |
- | s = 0; |
139 |
- | m += 1; |
140 |
- | if (m > 59) { |
141 |
- | m = 0; |
142 |
- | h += 1; |
143 |
- | if (h > 23) { |
144 |
- | h = 0; |
145 |
- | } |
146 |
- | } |
147 |
- | } |
148 |
- | |
149 |
- | // Set the new time. |
150 |
- | setTime(h, m, s); |
151 |
- | } |
152 |
- | } |
153 |
- |
MVC template/ClockTools.java ¶
0 additions and 110 deletions.
View changes Hide changes
1 |
- | import java.awt.event.ActionEvent; |
2 |
- | import java.awt.event.ActionListener; |
3 |
- | import java.util.Observable; |
4 |
- | import javax.swing.JButton; |
5 |
- | import javax.swing.JComponent; |
6 |
- | import javax.swing.JPanel; |
7 |
- | |
8 |
- | /** |
9 |
- | * |
10 |
- | * @author jvermeulen |
11 |
- | */ |
12 |
- | public class ClockTools extends AbstractView { |
13 |
- | // The user interface for this view. |
14 |
- | private JPanel mTools; |
15 |
- | private JButton mStart; |
16 |
- | private JButton mStop; |
17 |
- | private JButton mReset; |
18 |
- | //Toevoeging van Vngngdn: Knop toevoegen die de klok op huidige systeemtijd instelt: |
19 |
- | private JButton mSystemTime; |
20 |
- | |
21 |
- | public ClockTools(Observable model, ClockController controller) { |
22 |
- | super(model, controller); |
23 |
- | init(); |
24 |
- | } |
25 |
- | |
26 |
- | private void init() { |
27 |
- | mTools = new JPanel(); |
28 |
- | mTools.setLayout(new GridLayout(1, 3)); |
29 |
- | mStart = new JButton("Start"); |
30 |
- | mStart.setEnabled(false); |
31 |
- | mStop = new JButton("Stop"); |
32 |
- | mStop.setEnabled(false); |
33 |
- | mReset = new JButton("Reset"); |
34 |
- | mSystemTime = new JButton("Systeemtijd"); |
35 |
- | |
36 |
- | // handle events: pass to controller |
37 |
- | mStart.addActionListener(new ActionListener() { |
38 |
- | |
39 |
- | @Override |
40 |
- | public void actionPerformed(ActionEvent e) { |
41 |
- | ((ClockController)getController()).onStart(); |
42 |
- | } |
43 |
- | }); |
44 |
- | |
45 |
- | mStop.addActionListener(new ActionListener() { |
46 |
- | |
47 |
- | @Override |
48 |
- | public void actionPerformed(ActionEvent e) { |
49 |
- | ((ClockController)getController()).onStop(); |
50 |
- | } |
51 |
- | }); |
52 |
- | |
53 |
- | mReset.addActionListener(new ActionListener() { |
54 |
- | |
55 |
- | @Override |
56 |
- | public void actionPerformed(ActionEvent e) { |
57 |
- | ((ClockController)getController()).onReset(); |
58 |
- | } |
59 |
- | }); |
60 |
- | |
61 |
- | mSystemTime.addActionListener(new ActionListener() { |
62 |
- | |
63 |
- | @Override |
64 |
- | public void actionPerformed(ActionEvent e) { |
65 |
- | ((ClockController)getController()).onSystemTime(); |
66 |
- | } |
67 |
- | }); |
68 |
- | |
69 |
- | mTools.add(mStart); |
70 |
- | mTools.add(mStop); |
71 |
- | mTools.add(mReset); |
72 |
- | mTools.add(mSystemTime); |
73 |
- | } |
74 |
- | |
75 |
- | /** |
76 |
- | * Updates the state of the clock tools. |
77 |
- | * Invoked automatically by ClockModel. |
78 |
- | * @param o The ClockModel object that is broadcasting an update |
79 |
- | * @param info A ClockUpdate instance describing the changes that have |
80 |
- | * occurred in the ClockModel |
81 |
- | */ |
82 |
- | @Override |
83 |
- | public void update(Observable o, Object info) { |
84 |
- | // Cast info to ClockUpdate type. |
85 |
- | ClockUpdate clockInfo = (ClockUpdate) info; |
86 |
- | |
87 |
- | if (clockInfo.isRunning()) { |
88 |
- | mStop.setEnabled(true); |
89 |
- | mStart.setEnabled(false); |
90 |
- | } else { |
91 |
- | mStart.setEnabled(true); |
92 |
- | mStop.setEnabled(false); |
93 |
- | } |
94 |
- | } |
95 |
- | |
96 |
- | @Override |
97 |
- | public Controller defaultController(Observable model) { |
98 |
- | return new ClockController(model); |
99 |
- | } |
100 |
- | |
101 |
- | /** |
102 |
- | * Convenience method to return the user interface component. We don't need |
103 |
- | * this if we implement View directly and directly subclass a GUI component. |
104 |
- | * @return |
105 |
- | */ |
106 |
- | public JComponent getUI() { |
107 |
- | return mTools; |
108 |
- | } |
109 |
- | } |
110 |
- |
MVC template/ClockUpdate.java ¶
0 additions and 75 deletions.
View changes Hide changes
1 |
- | * Represent a clock update including all necessary information. |
2 |
- | * @author jvermeulen |
3 |
- | */ |
4 |
- | public class ClockUpdate { |
5 |
- | private int mHour; |
6 |
- | private int mMinute; |
7 |
- | private int mSecond; |
8 |
- | private boolean mIsRunning; |
9 |
- | |
10 |
- | |
11 |
- | ClockUpdate(int hour, int minute, int second, boolean isRunning) { |
12 |
- | mHour = hour; |
13 |
- | mMinute = minute; |
14 |
- | mSecond = second; |
15 |
- | mIsRunning = isRunning; |
16 |
- | } |
17 |
- | |
18 |
- | /** |
19 |
- | * @return the mHour |
20 |
- | */ |
21 |
- | public int getHour() { |
22 |
- | return mHour; |
23 |
- | } |
24 |
- | |
25 |
- | /** |
26 |
- | * @param hour the hour to set |
27 |
- | */ |
28 |
- | public void setHour(int hour) { |
29 |
- | this.mHour = hour; |
30 |
- | } |
31 |
- | |
32 |
- | /** |
33 |
- | * @return the mMinute |
34 |
- | */ |
35 |
- | public int getMinute() { |
36 |
- | return mMinute; |
37 |
- | } |
38 |
- | |
39 |
- | /** |
40 |
- | * @param minute the minute to set |
41 |
- | */ |
42 |
- | public void setMinute(int minute) { |
43 |
- | this.mMinute = minute; |
44 |
- | } |
45 |
- | |
46 |
- | /** |
47 |
- | * @return the mSecond |
48 |
- | */ |
49 |
- | public int getSecond() { |
50 |
- | return mSecond; |
51 |
- | } |
52 |
- | |
53 |
- | /** |
54 |
- | * @param second the second to set |
55 |
- | */ |
56 |
- | public void setSecond(int second) { |
57 |
- | this.mSecond = second; |
58 |
- | } |
59 |
- | |
60 |
- | /** |
61 |
- | * @return the mIsRunning |
62 |
- | */ |
63 |
- | public boolean isRunning() { |
64 |
- | return mIsRunning; |
65 |
- | } |
66 |
- | |
67 |
- | /** |
68 |
- | * @param isRunning the isRunning to set |
69 |
- | */ |
70 |
- | public void setRunning(boolean isRunning) { |
71 |
- | this.mIsRunning = isRunning; |
72 |
- | } |
73 |
- | |
74 |
- | } |
75 |
- |
MVC template/Controller.java ¶
0 additions and 13 deletions.
MVC template/DigitalClockController.java ¶
0 additions and 23 deletions.
View changes Hide changes
1 |
- | |
2 |
- | /** |
3 |
- | * A clock controller that allows the clock to be started, stopped and reset. |
4 |
- | * @author jvermeulen |
5 |
- | */ |
6 |
- | |
7 |
- | //Toevoeging van Maarten V.: onSystemTime() voor updaten systeemtijd. Ook het aanpassen van ClockDigitalView wordt hier behandeld. |
8 |
- | //Extra toevoeging voor OEF04: onEdit() als de gebruiker via het tekstveld een aanpassing doorvoert. |
9 |
- | public class DigitalClockController extends AbstractController { |
10 |
- | public DigitalClockController(Observable model) { |
11 |
- | super(model); |
12 |
- | } |
13 |
- | |
14 |
- | public void onEdit(String givenTime) { |
15 |
- | String[] parts = givenTime.split(":"); |
16 |
- | |
17 |
- | ((ClockModel)getModel()).setTime( |
18 |
- | Integer.parseInt(parts[0]), |
19 |
- | Integer.parseInt(parts[1]), |
20 |
- | Integer.parseInt(parts[2])); |
21 |
- | } |
22 |
- | } |
23 |
- |
MVC template/View.java ¶
0 additions and 17 deletions.
View changes Hide changes
1 |
- | import java.util.Observer; |
2 |
- | |
3 |
- | /** |
4 |
- | * |
5 |
- | * @author jvermeulen |
6 |
- | */ |
7 |
- | public interface View extends Observer { |
8 |
- | |
9 |
- | void setController(Controller controller); |
10 |
- | Controller getController(); |
11 |
- | |
12 |
- | void setModel(Observable model); |
13 |
- | Observable getModel(); |
14 |
- | |
15 |
- | Controller defaultController(Observable model); |
16 |
- | } |
17 |
- |
MVC template/readme.txt ¶
0 additions and 3 deletions.
New MVC/AbstractController.java → MVC/AbstractController.java ¶
0 additions and 0 deletions.
View changes Hide changes
New MVC/AbstractModel.java → MVC/AbstractModel.java ¶
0 additions and 0 deletions.
View changes Hide changes
New MVC/AbstractView.java → MVC/AbstractView.java ¶
0 additions and 0 deletions.
View changes Hide changes
New MVC/Controller.java → MVC/Controller.java ¶
0 additions and 0 deletions.
View changes Hide changes
New MVC/MainWindow.java → MVC/MainWindow.java ¶
0 additions and 0 deletions.