OOP2

Adding the remaining files to Git for tracking.

Author
Maarten Vangeneugden
Date
Nov. 21, 2015, 4:07 p.m.
Hash
5fab871ac7a91a310136d82a35850ab48869b026
Parent
ec784ef46729d6aa39254d243bd10d18625e1fad
Modified files
04/calculator/calculator.java
04/calculator/view.java
04/stockManagement/Boek.java
04/stockManagement/beschrijving.txt
04/stockManagement/boek.java
04/stockManagement/gui.java
04/stockManagement/help.java
04/stockManagement/main.java
05/AbstractController.java
05/AbstractView.java
05/Clock.java
05/ClockAnalogView.java
05/ClockController.java
05/ClockDigitalView.java
05/ClockModel.java
05/ClockTools.java
05/ClockUpdate.java
05/Controller.java
05/DigitalClockController.java
05/View.java
05/readme.txt
06/Opdracht2.txt
06/exercises/knopjes.java
06/exercises/tempie/Klok/AbstractController.java
06/exercises/tempie/Klok/AbstractView.java
06/exercises/tempie/Klok/Controller.java
06/exercises/tempie/Klok/View.java
06/exercises/tempie/Klok/clock/Clock.java
06/exercises/tempie/Klok/clock/ClockAnalogView.java
06/exercises/tempie/Klok/clock/ClockController.java
06/exercises/tempie/Klok/clock/ClockDigitalView.java
06/exercises/tempie/Klok/clock/ClockModel.java
06/exercises/tempie/Klok/clock/ClockTools.java
06/exercises/tempie/Klok/clock/ClockUpdate.java
06/exercises/tempie/calculator/Controller.java
06/exercises/tempie/calculator/View.java
06/exercises/tempie/calculator/actionButton.java
06/exercises/tempie/calculator/button.java
06/exercises/tempie/calculator/calculator.java
06/opdracht1.java
06/opdracht3.java
06/test.java
Challenge 1/ChoiceCourse.java
Challenge 1/Course.java
Challenge 1/DetailedCourseView.java
Challenge 1/MainWindow.java
Challenge 1/MandatoryCourse.java
Challenge 1/Planner.java
Challenge 1/StudyPath.java
Challenge 1/ontwerpkeuzes.txt
Challenge 1/opleidingsonderdelen.txt
Challenge 1/study.java

04/calculator/calculator.java

50 additions and 0 deletions.

View changes Hide changes
+
1
		public enum e_action {ADD, SUB, DIV, MUL};
+
2
+
3
		public calculator(int x, int y, e_action action) {
+
4
				setX(x);
+
5
				setY(y);
+
6
				switch(action){
+
7
						case ADD:
+
8
								addition();
+
9
								break;
+
10
						case SUB:
+
11
								substraction();
+
12
								break;
+
13
						case DIV:
+
14
								division();
+
15
								break;
+
16
						case MUL:
+
17
								multiplication();
+
18
								break;
+
19
				}
+
20
		}
+
21
		private int m_x;
+
22
		private int m_y;
+
23
		private int m_result;
+
24
+
25
		public int x() {
+
26
				return m_x;}
+
27
		public void setX(int x) {
+
28
				m_x = x;}
+
29
		
+
30
		public int y() {
+
31
				return m_y;}
+
32
		public void setY(int y) {
+
33
				m_y = y;}
+
34
		
+
35
		public int result() {
+
36
				return m_result;}
+
37
		public void setResult(int result) {
+
38
				m_result = result;}
+
39
+
40
		private addition() {
+
41
				setResult(x() + y());}
+
42
		private void substraction() {
+
43
				setResult(x() - y());}
+
44
		private void division() {
+
45
				setResult(x() / y());}
+
46
		private void multiplication() {
+
47
				setResult(x() * y());}
+
48
}
+
49
+
50

04/calculator/view.java

5 additions and 0 deletions.

View changes Hide changes
+
1
+
2
class view{
+
3
		private int test;
+
4
}
+
5

04/stockManagement/Boek.java

30 additions and 0 deletions.

View changes Hide changes
+
1
		public Boek(String naam, int prijs, e_type type) {
+
2
				setNaam(naam);
+
3
				setPrijs(prijs);
+
4
				setType(type);
+
5
		}
+
6
+
7
		public enum e_type {FOTOBOEK, ROMAN, STRIP};
+
8
		
+
9
		private e_type m_type;
+
10
		private String m_naam;
+
11
		private int m_prijs;
+
12
+
13
		public e_type type() {
+
14
				return m_type;}
+
15
		public void setType(e_type type) {
+
16
				m_type = type;}
+
17
+
18
		public String naam() {
+
19
				return m_naam;}
+
20
		public void setNaam(String naam) {
+
21
				m_naam = naam;}
+
22
		
+
23
		public int prijs() {
+
24
				return m_prijs;}
+
25
		public void setPrijs(int prijs) {
+
26
				m_prijs = prijs;}
+
27
		
+
28
}
+
29
+
30

04/stockManagement/beschrijving.txt

5 additions and 0 deletions.

View changes Hide changes
+
1
In plaats van voor alle verschillende boeken een aparte class te maken, heb ik besloten om de verschillende soorten aan te duiden middels een enum.
+
2
De verschillende soorten boeken bieden geen specialisatie aan. Ook zou inheritance ervoor zorgen dat, als er in de basisklasse iets verandert (een extra variabele), in elke afgeleide klasse dezelfde aanpassingen moeten worden doorgevoerd.
+
3
Deze keuze komt ook tegemoet aan het principe van low coupling.
+
4
Ik heb ook besloten de GUI een aparte class te geven, om tegemoet te komen aan de MVC-architectuur.
+
5

04/stockManagement/boek.java

30 additions and 0 deletions.

View changes Hide changes
+
1
		public Boek(String naam, int prijs, e_type type) {
+
2
				setNaam(naam);
+
3
				setPrijs(prijs);
+
4
				setType(type);
+
5
		}
+
6
+
7
		public enum e_type {FOTOBOEK, ROMAN, STRIP};
+
8
		
+
9
		private e_type m_type;
+
10
		private String m_naam;
+
11
		private int m_prijs;
+
12
+
13
		public e_type type() {
+
14
				return m_type;}
+
15
		public void setType(e_type type) {
+
16
				m_type = type;}
+
17
+
18
		public String naam() {
+
19
				return m_naam;}
+
20
		public void setNaam(String naam) {
+
21
				m_naam = naam;}
+
22
		
+
23
		public int prijs() {
+
24
				return m_prijs;}
+
25
		public void setPrijs(int prijs) {
+
26
				m_prijs = prijs;}
+
27
		
+
28
}
+
29
+
30

04/stockManagement/gui.java

73 additions and 0 deletions.

View changes Hide changes
+
1
		import java.util.*;
+
2
+
3
		class gui extends javax.swing.JFrame {
+
4
				public gui() {
+
5
						//Generating a couple of books:
+
6
						List<Boek> temp = new ArrayList<Boek>();
+
7
						temp.add(new Boek("Fotocollage", 5, Boek.e_type.FOTOBOEK));
+
8
						temp.add(new Boek("Vijftig tinten", 80, Boek.e_type.ROMAN));
+
9
						temp.add(new Boek("De jacht op een voetbal", 2, Boek.e_type.STRIP));
+
10
						setBoeken(temp);
+
11
						
+
12
						//Putting the books in a JList:
+
13
						DefaultListModel list = new DefaultListModel();
+
14
+
15
						for(int i=0; i<temp.size(); i++) {
+
16
								list.addElement(temp.get(i).naam());
+
17
						}
+
18
+
19
						final JList jlist = new JList(list);
+
20
+
21
						jlist.addListSelectionListener( //Dit voegt de listeners toe voor deze list. werk. (By Mannu)
+
22
										new javax.swing.event.ListSelectionListener() {
+
23
												public void valueChanged(javax.swing.event.ListSelectionEvent event) {
+
24
														viewBookDetails(event, jlist); }
+
25
										});
+
26
+
27
						JPanel panel = new JPanel();
+
28
						panel.add(jlist);
+
29
						JLabel label = new JLabel("Een test");
+
30
						panel.add(label);
+
31
+
32
						JFrame frame = new JFrame("Stock management");
+
33
						frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
34
+
35
						frame.getContentPane().add(panel);
+
36
+
37
						
+
38
						frame.pack();
+
39
						frame.setVisible(true);
+
40
				}
+
41
+
42
				public void viewBookDetails(javax.swing.event.ListSelectionEvent event, JList boeken) {
+
43
						Boek boek = boeken.getSelectedValue();
+
44
						//Creating a new panel with book information:
+
45
						JPanel panel = new JPanel();
+
46
						JLabel naam = new JLabel(boek.naam());
+
47
						JLabel prijs = new JLabel(boek.prijs());
+
48
						panel.add(naam);
+
49
						panel.add(prijs);
+
50
						frame().getContentPane.add(panel);
+
51
+
52
						frame.pack();
+
53
						frame.setVisible(true);
+
54
		}
+
55
+
56
+
57
+
58
			private List<Boek> l_boeken;
+
59
			private JPanel m_jpanel;
+
60
+
61
			public List<Boek> boeken() {
+
62
					return l_boeken;}
+
63
			public void setBoeken(List<Boek> boeken) {
+
64
					l_boeken = boeken;}
+
65
		
+
66
			public JPanel jpanel() {
+
67
					return m_jpanel;}
+
68
			public void setPanel(JPanel jpanel) {
+
69
					m_jpanel = jpanel;}
+
70
+
71
}
+
72
+
73

04/stockManagement/help.java

15 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.*;
+
2
class help {
+
3
		public static int selectedIndex(ListSelectionModel list) {
+
4
				//Declaring variables to limit the search to the selected options only:
+
5
				int minimum = list.getMinSelectionIndex();
+
6
				int maximum = list.getMaxSelectionIndex();
+
7
				//Looping through the possible options and checking if it's selected:
+
8
				for(int i = minimum; i <= maximum; i++) {
+
9
						if(list.isSelectedIndex(i))
+
10
								return i;
+
11
				}
+
12
				return -1;
+
13
		}
+
14
}
+
15

04/stockManagement/main.java

6 additions and 0 deletions.

View changes Hide changes
+
1
		public static void main(String[] args) {
+
2
				gui test = new gui();
+
3
+
4
		}
+
5
}
+
6

05/AbstractController.java

37 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 *
+
4
 * @author jvermeulen
+
5
 */
+
6
public abstract class AbstractController implements Controller {
+
7
    private Observable mModel;
+
8
    private View mView;
+
9
    
+
10
    public AbstractController(Observable model) {
+
11
        // Set the model.
+
12
        setModel(model);
+
13
    }
+
14
    
+
15
    
+
16
    @Override
+
17
    public void setView(View view) {
+
18
        mView = view;
+
19
    }
+
20
+
21
    @Override
+
22
    public View getView() {
+
23
        return mView;
+
24
    }
+
25
+
26
    @Override
+
27
    public void setModel(Observable model) {
+
28
        mModel = model;
+
29
    }
+
30
+
31
    @Override
+
32
    public Observable getModel() {
+
33
        return mModel;
+
34
    }
+
35
    
+
36
}
+
37

05/AbstractView.java

73 additions and 0 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

05/Clock.java

78 additions and 0 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

05/ClockAnalogView.java

218 additions and 0 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

05/ClockController.java

42 additions and 0 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

05/ClockDigitalView.java

84 additions and 0 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

05/ClockModel.java

153 additions and 0 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

05/ClockTools.java

110 additions and 0 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

05/ClockUpdate.java

75 additions and 0 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

05/Controller.java

13 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 *
+
4
 * @author jvermeulen
+
5
 */
+
6
public interface Controller {
+
7
+
8
    void setView(View view);
+
9
    View getView();
+
10
    void setModel(Observable model);
+
11
    Observable getModel();
+
12
}
+
13

05/DigitalClockController.java

23 additions and 0 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

05/View.java

17 additions and 0 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

05/readme.txt

3 additions and 0 deletions.

View changes Hide changes
+
1
+
2
Aanpassen gebeurt direct na RETURN. Om te voorkomen dat de cursor steeds verspringt, zet de klok op pauze.
+
3

06/Opdracht2.txt

18 additions and 0 deletions.

View changes Hide changes
+
1
Permission is granted to copy, distribute and/or modify this document
+
2
under the terms of the GNU Free Documentation License, Version 1.3
+
3
or any later version published by the Free Software Foundation;
+
4
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
+
5
A copy of the license can be found at https://gnu.org/licenses/fdl.html
+
6
+
7
BOUNDED TYPE PARAMETERS
+
8
Bij generic typing worden doorgaans alle types toegelaten. Wenst men daarentegen een bepaalde selectie van types toe te laten (Bv. een machtsverheffing kan men moeilijk toepassen op een string), dan maakt men gebruik van bounded type parameters. De voorbeelden op https://docs.oracle.com/javase/tutorial/java/generics/bounded.html demonstreren hoe men dergelijk gedrag kan forceren.
+
9
Merk op dat het woord "extends" geen inheritance beschrijft. Dit is ook waarom het lijkt alsof multiple inheritance in Java gaat.
+
10
(In Java bestaat multiple inheritance, maar da's iets anders)
+
11
+
12
UNBOUNDED WILDCARDS
+
13
Dit is een principe een vorm van generic typing die complementair is met bounded type parameters; een unbounded wildcard accepteert elk mogelijk type.
+
14
Merk ook op dat het niet mogelijk is om objecten toe te voegen aan een list van unbounded wildcards; enkel null is mogelijk.
+
15
+
16
UPPER BOUNDED WILDCARDS
+
17
Dit is niet hetzelfde als bounded type parameters. In principe betekent dit dat men een class type geeft aan een unbounded wildcard (? extends eggs). Dit heeft als gevolg dat enkel de types worden toegelaten die ofwel van het type eggs zijn, of een subclass zijn van eggs (dus een class "spam extends eggs" voldoet aan de wildcard).
+
18

06/exercises/knopjes.java

25 additions and 0 deletions.

View changes Hide changes
+
1
 */
+
2
import javax.swing.*;
+
3
+
4
class knopjes {
+
5
		public static void main(String[] args) {
+
6
				JFrame frame = new JFrame("Knoppentest");
+
7
				JPanel panel = new JPanel();
+
8
				frame.setContentPane(panel);
+
9
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
10
				//frame.setLayout(frame.getContentPane());
+
11
+
12
				JMenu menu = new JMenu();
+
13
				JButton knop = new JButton("Dit is de knop");
+
14
				
+
15
				panel.add(menu);
+
16
				panel.add(knop);
+
17
				frame.pack();
+
18
+
19
+
20
+
21
+
22
				frame.setVisible(true);
+
23
		}
+
24
}
+
25

06/exercises/tempie/Klok/AbstractController.java

39 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.util.Observable;
+
3
+
4
/**
+
5
 *
+
6
 * @author jvermeulen
+
7
 */
+
8
public abstract class AbstractController implements Controller {
+
9
    private Observable mModel;
+
10
    private View mView;
+
11
    
+
12
    public AbstractController(Observable model) {
+
13
        // Set the model.
+
14
        setModel(model);
+
15
    }
+
16
    
+
17
    
+
18
    @Override
+
19
    public void setView(View view) {
+
20
        mView = view;
+
21
    }
+
22
+
23
    @Override
+
24
    public View getView() {
+
25
        return mView;
+
26
    }
+
27
+
28
    @Override
+
29
    public void setModel(Observable model) {
+
30
        mModel = model;
+
31
    }
+
32
+
33
    @Override
+
34
    public Observable getModel() {
+
35
        return mModel;
+
36
    }
+
37
    
+
38
}
+
39

06/exercises/tempie/Klok/AbstractView.java

75 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.util.Observable;
+
3
import java.util.Observer;
+
4
+
5
/**
+
6
 *
+
7
 * @author jvermeulen
+
8
 */
+
9
public abstract class AbstractView implements View, Observer {
+
10
+
11
    private Observable mModel;
+
12
    private Controller mController;
+
13
    
+
14
    /**
+
15
     * Empty constructor so that the model and controller can be set later.
+
16
     */
+
17
    public AbstractView() {        
+
18
    }
+
19
    
+
20
    public AbstractView(Observable model, Controller controller) {
+
21
        // Set the model.
+
22
        setModel(model);
+
23
        // If a controller was supplied, use it. Otherwise let the first call to 
+
24
        // getController() create the default controller.
+
25
        if (controller != null) {
+
26
            setController(controller);
+
27
        }
+
28
    }
+
29
    
+
30
    @Override
+
31
    public void setController(Controller controller) {
+
32
        mController = controller;
+
33
        // Tell the controller this object is its view.
+
34
        getController().setView(this);
+
35
    }
+
36
+
37
    @Override
+
38
    public Controller getController() {
+
39
        // If a controller hasn't been defined yet...
+
40
        if (mController == null) {
+
41
            // ...make one. Note that defaultController is normally overriden by 
+
42
            // the AbstractView subclass so that it returns the appropriate 
+
43
            // controller for the view.
+
44
            setController(defaultController(getModel()));
+
45
        }
+
46
        
+
47
        return mController;
+
48
    }
+
49
+
50
    @Override
+
51
    public void setModel(Observable model) {
+
52
        mModel = model;
+
53
    }
+
54
+
55
    @Override
+
56
    public Observable getModel() {
+
57
        return mModel;
+
58
    }
+
59
+
60
    @Override
+
61
    public Controller defaultController(Observable model) {
+
62
        return null;
+
63
    }
+
64
+
65
    /**
+
66
     * A do-nothing implementation of the Observer interface's update method.
+
67
     * Subclasses of AbstractView will provide a concrete implementation for 
+
68
     * this method.
+
69
     */
+
70
    @Override
+
71
    public void update(Observable o, Object arg) {    
+
72
    }
+
73
    
+
74
}
+
75

06/exercises/tempie/Klok/Controller.java

15 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.util.Observable;
+
3
+
4
/**
+
5
 *
+
6
 * @author jvermeulen
+
7
 */
+
8
public interface Controller {
+
9
+
10
    void setView(View view);
+
11
    View getView();
+
12
    void setModel(Observable model);
+
13
    Observable getModel();
+
14
}
+
15

06/exercises/tempie/Klok/View.java

19 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.util.Observable;
+
3
import java.util.Observer;
+
4
+
5
/**
+
6
 *
+
7
 * @author jvermeulen
+
8
 */
+
9
public interface View extends Observer {
+
10
      
+
11
    void setController(Controller controller);
+
12
    Controller getController();
+
13
    
+
14
    void setModel(Observable model);
+
15
    Observable getModel();
+
16
    
+
17
    Controller defaultController(Observable model);
+
18
}
+
19

06/exercises/tempie/Klok/clock/Clock.java

80 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.awt.event.WindowAdapter;
+
3
import java.awt.event.WindowEvent;
+
4
import javax.swing.BoxLayout;
+
5
import javax.swing.JFrame;
+
6
+
7
/**
+
8
 * Main user interface for the application. Makes sure the model, views and
+
9
 * controllers are connected to each other.
+
10
 * @author jvermeulen
+
11
 */
+
12
public class Clock {
+
13
    // The clock data (i.e., the Model).
+
14
    private ClockModel mModel;
+
15
    
+
16
    // A digital view of the clock.
+
17
    private ClockDigitalView mDigitalView;
+
18
    
+
19
    // A toolbar for controlling the clock.
+
20
    private ClockTools mTools;
+
21
    
+
22
    // An analog view of the clock.
+
23
    private final ClockAnalogView mAnalogView;
+
24
    
+
25
    public Clock(int hour, int minute, int second) {
+
26
        // Create the data model.
+
27
        mModel = new ClockModel();
+
28
+
29
        // Create the digital clock view.
+
30
        mDigitalView = new ClockDigitalView(mModel, null);
+
31
        mModel.addObserver(mDigitalView);
+
32
+
33
        // Create the analog clock view.
+
34
        mAnalogView = new ClockAnalogView(mModel, null);
+
35
        mModel.addObserver(mAnalogView);
+
36
        
+
37
        // Create the clock tools view.
+
38
        mTools = new ClockTools(mModel, null);
+
39
        mModel.addObserver(mTools);
+
40
        
+
41
        // Set the time.
+
42
        mModel.setTime(hour, minute, second);
+
43
+
44
        // Start the clock ticking.
+
45
        mModel.start();
+
46
    }
+
47
    
+
48
    public void createGUI() {
+
49
        JFrame frame = new JFrame("Clock");
+
50
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
+
51
        frame.getContentPane().add(mDigitalView.getUI());
+
52
        frame.getContentPane().add(mAnalogView);
+
53
        frame.getContentPane().add(mTools.getUI());                
+
54
        frame.addWindowListener(new WindowAdapter() {
+
55
            @Override
+
56
            public void windowClosing(WindowEvent e) {
+
57
                System.exit(0);
+
58
            }
+
59
        });
+
60
        frame.pack();
+
61
        frame.setVisible(true);
+
62
    }
+
63
    
+
64
    private static void createAndShowGUI() {
+
65
        Clock clock = new Clock(9, 10, 22);
+
66
        clock.createGUI();
+
67
    }
+
68
    
+
69
    public static void main(String[] args) {
+
70
        //Schedule a job for the event-dispatching thread:
+
71
        //creating and showing this application's GUI.
+
72
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
+
73
            @Override
+
74
            public void run() {
+
75
                createAndShowGUI();
+
76
            }
+
77
        });
+
78
    }
+
79
}
+
80

06/exercises/tempie/Klok/clock/ClockAnalogView.java

222 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import be.uhasselt.oo2.mvc.Controller;
+
3
import be.uhasselt.oo2.mvc.View;
+
4
import java.awt.Color;
+
5
import java.awt.Dimension;
+
6
import java.awt.Graphics;
+
7
import java.awt.Graphics2D;
+
8
import java.awt.RenderingHints;
+
9
import java.awt.geom.AffineTransform;
+
10
import java.awt.geom.Ellipse2D;
+
11
import java.awt.geom.GeneralPath;
+
12
import java.util.Calendar;
+
13
import java.util.Observable;
+
14
import javax.swing.JComponent;
+
15
+
16
/**
+
17
 * An analog clock View for the ClockModel. This View has no user inputs, so
+
18
 * no controller is required. This class implements the View interface, and
+
19
 * subclasses JComponent directly, to illustrate that not all Views need to
+
20
 * inherit from AbstractView.
+
21
 *
+
22
 * Note: it is not necessary to understand all the details of the rendering 
+
23
 * of the analog clock.
+
24
 *
+
25
 * @author jvermeulen (inspired by IBM Java2D tutorial)
+
26
 */
+
27
public class ClockAnalogView extends JComponent implements View {
+
28
+
29
    private Observable mModel;
+
30
    private Controller mController;
+
31
+
32
    int mHour;
+
33
    int mMinute;
+
34
    int mSecond;
+
35
+
36
    ClockAnalogView(Observable model, Controller controller) {
+
37
        mModel = model;
+
38
        mController = controller;
+
39
    }
+
40
+
41
    @Override
+
42
    public void setController(Controller controller) {
+
43
        mController = controller;
+
44
    }
+
45
+
46
    @Override
+
47
    public Controller getController() {
+
48
        return mController;
+
49
    }
+
50
+
51
    @Override
+
52
    public void setModel(Observable model) {
+
53
        mModel = model;
+
54
    }
+
55
+
56
    @Override
+
57
    public Observable getModel() {
+
58
        return mModel;
+
59
    }
+
60
+
61
    @Override
+
62
    public Controller defaultController(Observable model) {
+
63
        return null;
+
64
    }
+
65
+
66
    @Override
+
67
    public void update(Observable o, Object info) {
+
68
        // Cast info to ClockUpdate type.
+
69
        ClockUpdate clockInfo = (ClockUpdate) info;
+
70
+
71
        // Store hour, minute, second for repainting.
+
72
        mHour = clockInfo.getHour();
+
73
        mMinute = clockInfo.getMinute();
+
74
        mSecond = clockInfo.getSecond();
+
75
+
76
        // Using a little trigonometry, set the transforms to rotate
+
77
        // each hand into the proper position.  Center the rotation
+
78
        // around the pivot point (50, 50) instead of the origin
+
79
        hourTransform.setToRotation(((double) mHour) *
+
80
                                            (Math.PI / 6.0), 50, 50);
+
81
        minuteTransform.setToRotation(((double) mMinute) *
+
82
                                            (Math.PI / 30.0), 50, 50);
+
83
        secondTransform.setToRotation(((double) mSecond) *
+
84
                                            (Math.PI / 30.0), 50, 50);
+
85
+
86
        repaint();
+
87
    }
+
88
+
89
+
90
    /**
+
91
     * Sets this components preferred size to 150x150.
+
92
     * @return
+
93
     */
+
94
    @Override
+
95
    public Dimension getPreferredSize() {
+
96
        return new Dimension(100, 100);
+
97
    }
+
98
+
99
    // Create a shape for the face of the clock
+
100
    protected static Ellipse2D face = new Ellipse2D.Float(3, 3, 94, 94);
+
101
+
102
    // Create a path that represents a tick mark
+
103
    protected static GeneralPath tick = new GeneralPath();
+
104
    static // http://www.jusfortechies.com/java/core-java/static-blocks.php
+
105
    {
+
106
        tick.moveTo(100, 100);
+
107
        tick.moveTo(49, 0);
+
108
        tick.lineTo(51, 0);
+
109
        tick.lineTo(51, 6);
+
110
        tick.lineTo(49, 6);
+
111
        tick.lineTo(49, 0);
+
112
    }
+
113
+
114
    // Create a cool hour hand
+
115
    protected static GeneralPath hourHand = new GeneralPath();
+
116
    static
+
117
    {
+
118
        hourHand.moveTo(50, 15);
+
119
        hourHand.lineTo(53, 50);
+
120
        hourHand.lineTo(50, 53);
+
121
        hourHand.lineTo(47, 50);
+
122
        hourHand.lineTo(50, 15);
+
123
    }
+
124
+
125
    // Create a cool minute hand
+
126
    protected static GeneralPath minuteHand = new GeneralPath();
+
127
    static
+
128
    {
+
129
        minuteHand.moveTo(50, 2);
+
130
        minuteHand.lineTo(53, 50);
+
131
        minuteHand.lineTo(50, 58);
+
132
        minuteHand.lineTo(47, 50);
+
133
        minuteHand.lineTo(50, 2);
+
134
    }
+
135
+
136
    // And a cool second hand
+
137
    protected static GeneralPath secondHand = new GeneralPath();
+
138
    static
+
139
    {
+
140
        secondHand.moveTo(49, 5);
+
141
        secondHand.lineTo(51, 5);
+
142
        secondHand.lineTo(51, 62);
+
143
        secondHand.lineTo(49, 62);
+
144
        secondHand.lineTo(49, 5);
+
145
    }
+
146
+
147
    // Create some colors for the pieces of the clock
+
148
    protected static Color faceColor = new Color(220, 220, 220);
+
149
    protected static Color hourColor = Color.red.darker();
+
150
    protected static Color minuteColor = Color.blue.darker();
+
151
    protected static Color secondColor = new Color(180, 180, 0);
+
152
    protected static Color pinColor = Color.gray.brighter();
+
153
+
154
    // Create circles for the pivot and center pin
+
155
    protected Ellipse2D pivot = new Ellipse2D.Float(47, 47, 6, 6);
+
156
    protected Ellipse2D centerPin = new Ellipse2D.Float(49, 49, 2, 2);
+
157
+
158
+
159
    // Create three transforms that center around the pivot point
+
160
    protected AffineTransform hourTransform =
+
161
                    AffineTransform.getRotateInstance(0, 50, 50);
+
162
    protected AffineTransform minuteTransform =
+
163
                    AffineTransform.getRotateInstance(0, 50, 50);
+
164
    protected AffineTransform secondTransform =
+
165
                    AffineTransform.getRotateInstance(0, 50, 50);
+
166
+
167
    // Create a timer that fires once a second and a Calendar
+
168
    // instance for getting the time values
+
169
    // protected Timer timer = new Timer(1000, this);
+
170
    protected Calendar calendar = Calendar.getInstance();
+
171
+
172
    // This is an alternative to creating a UI delegate.  Since JPanel's
+
173
    // paint() method only paints the border and backgound, we can just
+
174
    // override the paint method of the component to do the graphics.
+
175
    public void paint(Graphics g)
+
176
    {
+
177
        // Call the superclass first to paint the border (if one is assigned)
+
178
        super.paint(g);
+
179
+
180
+
181
        // Get the graphics context and turn on anti-aliasing
+
182
        Graphics2D g2 = (Graphics2D) g;
+
183
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+
184
                            RenderingHints.VALUE_ANTIALIAS_ON);
+
185
+
186
        // Set the paint for the clock face and fill it in
+
187
        g2.setPaint(faceColor);
+
188
        g2.fill(face);
+
189
+
190
        // Set the paint to black and draw the clock's outline
+
191
        g2.setPaint(Color.black);
+
192
        g2.draw(face);
+
193
+
194
        // Fill in the 12 ticks around the face of the clock
+
195
        for (double p = 0.0; p < 12.0; p += 1.0)
+
196
        {
+
197
            // This is probably terribly inefficient and should be
+
198
            // done statically or in the constructor - draw the
+
199
            // tick as a transformed shape that is rotated.
+
200
            g2.fill(tick.createTransformedShape(
+
201
                AffineTransform.getRotateInstance((Math.PI / 6.0)  * p,
+
202
                        50, 50)));
+
203
        }
+
204
+
205
        // Set the paint and draw the hour hand.  It is lowest in the
+
206
        // 'z-order' so will appear underneath the other hands.  Notice
+
207
        // how each hand is transformed by a different <AffineTransform>.
+
208
        g2.setPaint(hourColor);
+
209
        g2.fill(hourHand.createTransformedShape(hourTransform));
+
210
+
211
        // Set the paint and draw the minute hand, the second hand,
+
212
        // the pivot and the center pin
+
213
        g2.setPaint(minuteColor);
+
214
        g2.fill(minuteHand.createTransformedShape(minuteTransform));
+
215
        g2.setPaint(secondColor);
+
216
        g2.fill(secondHand.createTransformedShape(secondTransform));
+
217
        g2.fill(pivot);
+
218
        g2.setPaint(pinColor);
+
219
        g2.fill(centerPin);
+
220
    }
+
221
}
+
222

06/exercises/tempie/Klok/clock/ClockController.java

26 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import be.uhasselt.oo2.mvc.AbstractController;
+
3
import java.util.Observable;
+
4
+
5
/**
+
6
 * A clock controller that allows the clock to be started, stopped and reset.
+
7
 * @author jvermeulen
+
8
 */
+
9
public class ClockController extends AbstractController {
+
10
    public ClockController(Observable model) {
+
11
        super(model);
+
12
    }
+
13
    
+
14
    public void onStart() {
+
15
        ((ClockModel)getModel()).start();
+
16
    }
+
17
    
+
18
    public void onStop() {
+
19
        ((ClockModel)getModel()).stop();
+
20
    }
+
21
    
+
22
    public void onReset() {
+
23
        ((ClockModel)getModel()).setTime(0,0,0);
+
24
    }
+
25
}
+
26

06/exercises/tempie/Klok/clock/ClockDigitalView.java

74 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import be.uhasselt.oo2.mvc.AbstractView;
+
3
import be.uhasselt.oo2.mvc.Controller;
+
4
import java.awt.Color;
+
5
import java.util.Observable;
+
6
import javax.swing.JComponent;
+
7
import javax.swing.JTextField;
+
8
+
9
/**
+
10
 * A digital clock View for the ClockModel. This View has no user inputs, so no 
+
11
 * Controller is required.
+
12
 * @author jvermeulen
+
13
 */
+
14
public class ClockDigitalView extends AbstractView {
+
15
    
+
16
    // The user interface for this view.
+
17
    private JTextField mClock;
+
18
    
+
19
    public ClockDigitalView(Observable model, Controller controller) {
+
20
        super(model, controller);
+
21
        init();
+
22
    }
+
23
    
+
24
    /**
+
25
     * Initializes the user interface.
+
26
     */
+
27
    private void init() {
+
28
        mClock = new JTextField();
+
29
    }
+
30
        
+
31
    /**
+
32
     * Updates the state of the on-screen digital clock.
+
33
     * Invoked automatically by ClockModel.
+
34
     * @param o The ClockModel object that is broadcasting an update
+
35
     * @param info A ClockUpdate instance describing the changes that have 
+
36
     * occurred in the ClockModel
+
37
     */
+
38
    @Override
+
39
    public void update(Observable o, Object info) {    
+
40
        // Cast info to ClockUpdate type.
+
41
        ClockUpdate clockInfo = (ClockUpdate) info;
+
42
        
+
43
        // Create a String representing the time.        
+
44
        String timeString = formatTime(clockInfo);
+
45
+
46
        // Display the new time in the clock text field.
+
47
        mClock.setText(timeString);
+
48
+
49
        // Fade the color of the display if the clock isn't running.
+
50
        if (clockInfo.isRunning()) {
+
51
          mClock.setBackground(Color.white);
+
52
        } else {
+
53
          mClock.setBackground(Color.gray);
+
54
        }
+
55
    }
+
56
    
+
57
    /**
+
58
     * Convenience method to return the user interface component. We don't need 
+
59
     * this if we implement View directly and directly subclass a GUI component.
+
60
     * @return the JComponent representing this View.
+
61
     */
+
62
    public JComponent getUI() {
+
63
        return mClock;
+
64
    }
+
65
    
+
66
    private String formatTime(ClockUpdate info) {
+
67
        return String.format("%d:%d:%d", 
+
68
                             info.getHour(), 
+
69
                             info.getMinute(), 
+
70
                             info.getSecond());
+
71
    }        
+
72
    
+
73
}
+
74

06/exercises/tempie/Klok/clock/ClockModel.java

152 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import java.util.Calendar;
+
3
import java.util.Date;
+
4
import java.util.GregorianCalendar;
+
5
import java.util.Observable;
+
6
import java.util.Timer;
+
7
import java.util.TimerTask;
+
8
import javax.swing.SwingUtilities;
+
9
+
10
/**
+
11
 *
+
12
 * @author jvermeulen
+
13
 */
+
14
public class ClockModel extends Observable {
+
15
    private int mHour;
+
16
    private int mMinute;
+
17
    private int mSecond;
+
18
    
+
19
    private final long mTickInterval = 1000; // tick every second
+
20
    private Timer mTimer;
+
21
    private boolean mIsRunning;
+
22
    
+
23
    public ClockModel() {
+
24
        // By default, set the clock time to the current system time.
+
25
        Calendar now = new GregorianCalendar();        
+
26
        setTime(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), 
+
27
                now.get(Calendar.SECOND));        
+
28
    }
+
29
    
+
30
    /**
+
31
     * Helper class to run the tick() method multiple times 
+
32
     * (e.g., every 1 second).
+
33
     */
+
34
    class TickTask extends TimerTask {
+
35
        public void run() {
+
36
            tick();
+
37
        }
+
38
    }
+
39
    
+
40
    public void start() {
+
41
        if (!mIsRunning) {
+
42
            mIsRunning = true;
+
43
+
44
            // Schedule the tick() method to run every mTickInterval milliseconds.
+
45
            mTimer = new Timer();
+
46
            mTimer.schedule(new TickTask(), 0, mTickInterval); 
+
47
            
+
48
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
49
                                               mIsRunning);
+
50
            setChanged();
+
51
            notifyObservers(info);
+
52
        }
+
53
    }
+
54
    
+
55
    public void stop() {
+
56
        if (mIsRunning) {
+
57
            mIsRunning = false;
+
58
            
+
59
            // Stop the timer.
+
60
            mTimer.cancel();
+
61
            mTimer = null;
+
62
            
+
63
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
64
                                               mIsRunning);
+
65
            setChanged();
+
66
            notifyObservers(info);
+
67
        }
+
68
    }
+
69
    
+
70
    /**
+
71
     * Sets the current time. Notifies observers of any change in time.
+
72
     * @param hour The new hour.
+
73
     * @param minute The new minute
+
74
     * @param second The new second
+
75
     */
+
76
    public void setTime(int hour, int minute, int second) {
+
77
        if (hour != mHour && isValidHour(hour)) {
+
78
            mHour = hour;
+
79
            setChanged();
+
80
        }
+
81
        
+
82
        if (minute != mMinute && isValidMinute(minute)) {
+
83
            mMinute = minute;
+
84
            setChanged();
+
85
        }
+
86
                
+
87
        if (second != mSecond && isValidSecond(second)) {
+
88
            mSecond = second;
+
89
            setChanged();
+
90
        }
+
91
        
+
92
        // If the model has changed, notify Views.
+
93
        if (hasChanged()) {
+
94
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
95
                                               mIsRunning);
+
96
            notifyObservers(info);
+
97
        }
+
98
    }    
+
99
    
+
100
    /**
+
101
     * Checks to see if a number is a valid hour (i.e. in [0,23]).
+
102
     * @param hour The hour to check
+
103
     * @return True if this is a valid hour
+
104
     */
+
105
    private boolean isValidHour(int hour) {
+
106
        return (hour >= 0 && hour <= 23);
+
107
    }
+
108
    
+
109
    /**
+
110
     * Checks to see if a number is a valid minute (i.e. in [0,59]).
+
111
     * @param minute The minute to check
+
112
     * @return True if this is a valid minute
+
113
     */
+
114
    private boolean isValidMinute(int minute) {
+
115
        return (minute >= 0 && minute <= 59);
+
116
    }
+
117
    
+
118
    /**
+
119
     * Checks to see if a number is a valid minute (i.e. in [0,59]).
+
120
     * @param second The second to check
+
121
     * @return True if this is a valid second
+
122
     */
+
123
    private boolean isValidSecond(int second) {
+
124
        return (second >= 0 && second <= 59);
+
125
    }
+
126
    
+
127
    private void tick() {
+
128
        // Get the current time
+
129
        int h = mHour;
+
130
        int m = mMinute;
+
131
        int s = mSecond;
+
132
        
+
133
        // Increment the current second, adjusting the minute and hour 
+
134
        // if necessary.
+
135
        s += 1;
+
136
        if (s > 59) {
+
137
            s = 0;
+
138
            m += 1;
+
139
            if (m > 59) {
+
140
                m = 0;
+
141
                h += 1;
+
142
                if (h > 23) {
+
143
                    h = 0;
+
144
                }
+
145
            }
+
146
        }
+
147
+
148
        // Set the new time.
+
149
        setTime(h, m, s);
+
150
    }
+
151
}
+
152

06/exercises/tempie/Klok/clock/ClockTools.java

102 additions and 0 deletions.

View changes Hide changes
+
1
+
2
import be.uhasselt.oo2.mvc.AbstractView;
+
3
import be.uhasselt.oo2.mvc.Controller;
+
4
import java.awt.GridLayout;
+
5
import java.awt.event.ActionEvent;
+
6
import java.awt.event.ActionListener;
+
7
import java.util.Observable;
+
8
import javax.swing.JButton;
+
9
import javax.swing.JComponent;
+
10
import javax.swing.JPanel;
+
11
+
12
/**
+
13
 *
+
14
 * @author jvermeulen
+
15
 */
+
16
public class ClockTools extends AbstractView {
+
17
    // The user interface for this view.
+
18
    private JPanel mTools;
+
19
    private JButton mStart;
+
20
    private JButton mStop;
+
21
    private JButton mReset;
+
22
    
+
23
    public ClockTools(Observable model, ClockController controller) {
+
24
        super(model, controller);
+
25
        init();
+
26
    }
+
27
+
28
    private void init() {
+
29
        mTools = new JPanel();
+
30
        mTools.setLayout(new GridLayout(1,