Commit 953522e2 authored by Daniel Vogel's avatar Daniel Vogel

added nomvc and hellomvc3b demos, added comments to other mvc demos

parent 1dd19c8d
......@@ -89,9 +89,12 @@ class Canvas extends JComponent {
g2.drawOval(-5, -5, 10, 10);
// create transformation matrices
AffineTransform R = AffineTransform.getRotateInstance(Math.toRadians(theta));
AffineTransform T = AffineTransform.getTranslateInstance(tx, ty);
AffineTransform S = AffineTransform.getScaleInstance(sx, sy);
AffineTransform R =
AffineTransform.getRotateInstance(Math.toRadians(theta));
AffineTransform T =
AffineTransform.getTranslateInstance(tx, ty);
AffineTransform S =
AffineTransform.getScaleInstance(sx, sy);
// concatenate the matrices in 1 of 6 orders
String s = "p'=";
......
......@@ -24,8 +24,10 @@ public class Main {
// tell Model about View.
model.setView(view);
// create the window
// add view (view is a JPanel)
frame.getContentPane().add(view);
// setup window
frame.setPreferredSize(new Dimension(300,300));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
......
......@@ -12,7 +12,7 @@ public class Model {
// the data in the model, just a counter
private int counter;
// the view
// the view (note, only supports 1 view!)
IView view;
// set the view observer
......
......@@ -18,6 +18,7 @@ class View extends JPanel implements IView {
private Model model;
View(Model model, Controller controller) {
// create the view UI
button = new JButton("?");
button.setMaximumSize(new Dimension(100, 50));
......
......@@ -23,19 +23,22 @@ public class Main {
Controller controller = new Controller(model);
// create View, tell it about model and controller
View view = new View(model, controller);
// tell Model about View.
// tell Model about View
model.addView(view);
// create second view ...
View2 view2 = new View2(model, controller);
model.addView(view2);
// create the window
// create a layout panel to hold the two views
JPanel p = new JPanel(new GridLayout(2,1));
frame.getContentPane().add(p);
// add views (each view is a JPanel)
p.add(view);
p.add(view2);
// setup the window
frame.setPreferredSize(new Dimension(300,300));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
......
......@@ -18,6 +18,7 @@ class View extends JPanel implements IView {
private Model model;
View(Model model, Controller controller) {
// create the view UI
button = new JButton("?");
button.setMaximumSize(new Dimension(100, 50));
......
......@@ -15,17 +15,17 @@ class View2 extends JPanel implements IView {
private JLabel label = new JLabel();
View2(Model model, Controller controller) {
// create UI
setBackground(Color.WHITE);
setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(this.label);
// set the model
this.model = model;
// setup the event to go to the controller
addMouseListener(controller);
//addActionListener(controller);
this.add(this.label);
}
// IView interface
......
......@@ -28,12 +28,15 @@ public class Main {
View2 view2 = new View2(model);
model.addView(view2);
// create the window
// create a layout panel to hold the two views
JPanel p = new JPanel(new GridLayout(2,1));
frame.getContentPane().add(p);
// add views (each view is a JPanel)
p.add(view);
p.add(view2);
// create the window
frame.setPreferredSize(new Dimension(300,300));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
......
......@@ -18,6 +18,7 @@ class View extends JPanel implements IView {
private Model model;
public View(Model model) {
// create the view UI
button = new JButton("?");
button.setMaximumSize(new Dimension(100, 50));
......
......@@ -14,13 +14,13 @@ class View2 extends JPanel implements IView {
private Model model;
private JLabel label = new JLabel();
View2(Model model_) {
View2(Model model) {
// create UI
setBackground(Color.WHITE);
setLayout(new FlowLayout(FlowLayout.LEFT));
// set the model
model = model_;
this.model = model;
// setup the event to go to the "controller"
// (this anonymous class is essentially the controller)
......@@ -29,6 +29,7 @@ class View2 extends JPanel implements IView {
model.incrementCounter();
}
});
this.add(this.label);
}
......
// HelloMVC: a simple MVC example
// the model is just a counter
// inspired by code by Joseph Mack, http://www.austintek.com/mvc/
/**
* Views using a listener anonymous class to handle Model update "events".
*/
import javax.swing.*;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;
public class Main {
public static void main(String[] args){
JFrame frame = new JFrame("HelloMVC3b");
// create Model and initialize it
Model model = new Model();
// create View, tell it about model
View view = new View(model);
// tell Model about View's listener inner class
model.addView(view.modelListener);
// create second view ...
View2 view2 = new View2(model);
// tell Model about View's listener inner class
model.addView(view2.modelListener);
// create a layout panel to hold the two views
JPanel p = new JPanel(new GridLayout(2,1));
frame.getContentPane().add(p);
// add views (each view is a JPanel)
p.add(view);
p.add(view2);
// create the window
frame.setPreferredSize(new Dimension(300,300));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.util.ArrayList;
// HelloMVC: a simple MVC example
// the model is just a counter
// inspired by code by Joseph Mack, http://www.austintek.com/mvc/
// View interface
interface IView {
public void updateView();
}
public class Model {
// the data in the model, just a counter
private int counter;
// all views of this model
private ArrayList<IView> views = new ArrayList<IView>();
// set the view observer
public void addView(IView view) {
views.add(view);
// update the view to current state of the model
view.updateView();
}
public int getCounterValue() {
return counter;
}
public void incrementCounter() {
if (counter < 5) {
counter++;
System.out.println("Model: increment counter to " + counter);
notifyObservers();
}
}
// notify the IView observer
private void notifyObservers() {
for (IView view : this.views) {
System.out.println("Model: notify View");
view.updateView();
}
}
}
// HelloMVC: a simple MVC example
// the model is just a counter
// inspired by code by Joseph Mack, http://www.austintek.com/mvc/
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
class View extends JPanel {
// the view's main user interface
private JButton button;
// the model that this view is showing
private Model model;
// model listener
public IView modelListener;
public View(Model model) {
// create the view UI
button = new JButton("?");
button.setMaximumSize(new Dimension(100, 50));
button.setPreferredSize(new Dimension(100, 50));
// a GridBagLayout with default constraints centres
// the widget in the window
this.setLayout(new GridBagLayout());
this.add(button, new GridBagConstraints());
// set the model
this.model = model;
// anonymous class acts as model listener
modelListener = new IView() {
public void updateView() {
System.out.println("View: updateView");
button.setText(Integer.toString(model.getCounterValue()));
}
};
// setup the event to go to the "controller"
// (this anonymous class is essentially the controller)
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.incrementCounter();
}
});
}
}
// HelloMVC: a simple MVC example
// the model is just a counter
// inspired by code by Joseph Mack, http://www.austintek.com/mvc/
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.*;
import java.util.*;
class View2 extends JPanel {
// the model that this view is showing
private Model model;
private JLabel label = new JLabel();
// model listener
public IView modelListener;
View2(Model model) {
// create UI
setBackground(Color.WHITE);
setLayout(new FlowLayout(FlowLayout.LEFT));
// set the model
this.model = model;
// anonymous class acts as model listener
modelListener = new IView() {
public void updateView() {
System.out.println("View2: updateView");
// just displays an 'X' for each counter value
String s = "";
for (int i=0; i< model.getCounterValue(); i++) s = s + "X";
label.setText(s);
}
};
// setup the event to go to the "controller"
// (this anonymous class is essentially the controller)
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
model.incrementCounter();
}
});
this.add(this.label);
}
}
# super simple makefile
# call it using 'make NAME=name_of_code_file_without_extension'
# (assumes a .java extension)
NAME = "Main"
all:
@echo "Compiling..."
javac *.java
run: all
@echo "Running..."
java $(NAME)
clean:
rm -rf *.class
......@@ -32,13 +32,15 @@ public class Main{
// let all the views know that they're connected to the model
model.notifyObservers();
// create the window
// create a layout panel to hold the two views
JPanel p = new JPanel(new GridLayout(2,1));
frame.getContentPane().add(p);
// add views (each view is a JPanel)
p.add(view);
p.add(view2);
// setup window
frame.setPreferredSize(new Dimension(300,300));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
......
......@@ -19,18 +19,20 @@ class View extends JPanel implements Observer {
// the model that this view is showing
private Model model;
View(Model model_) {
View(Model model) {
// create the view UI
button = new JButton("?");
button.setMaximumSize(new Dimension(100, 50));
button.setPreferredSize(new Dimension(100, 50));
// a GridBagLayout with default constraints centres
// the widget in the window
this.setLayout(new GridBagLayout());
this.add(button, new GridBagConstraints());
// set the model
model = model_;
this.model = model;
// setup the event to go to the "controller"
// (this anonymous class is essentially the controller)
......
......@@ -14,13 +14,15 @@ class View2 extends JPanel implements Observer {
private Model model;
private JLabel label = new JLabel();
View2(Model model_) {
View2(Model model) {
// create UI
setBackground(Color.WHITE);
setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(this.label);
// set the model
model = model_;
this.model = model;
// setup the event to go to the "controller"
// (this anonymous class is essentially the controller)
......@@ -29,7 +31,7 @@ class View2 extends JPanel implements Observer {
model.incrementCounter();
}
});
this.add(this.label);
}
// Observer interface
......
import javax.swing.*;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.*;
public class Main extends JPanel {
public static void main(String[] args){
JFrame frame = new JFrame("NoMvc");
frame.getContentPane().add(new Main());
frame.setPreferredSize(new Dimension(300,300));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
int counter = 0;
void increaseCounter() {
if (counter < 5) {
counter++;
String s = Integer.toString(counter);
button.setText(s);
// just displays an 'X' for each counter value
s = "";
for (int i = 0; i< counter; i++) s = s + "X";
label.setText(s);
}
}
JButton button;
JLabel label;
public Main() {
this.setLayout(new GridLayout(2,1));
JPanel topPanel = new JPanel();
button = new JButton("?");
button.setMaximumSize(new Dimension(100, 50));
button.setPreferredSize(new Dimension(100, 50));
topPanel.setLayout(new GridBagLayout());
topPanel.add(button, new GridBagConstraints());
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
increaseCounter();
}
});
this.add(topPanel);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
label = new JLabel("");
bottomPanel.add(label);
bottomPanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
increaseCounter();
}
});
this.add(bottomPanel);
}
}
\ No newline at end of file
# super simple makefile
# call it using 'make NAME=name_of_code_file_without_extension'
# (assumes a .java extension)
NAME = "Main"
all:
@echo "Compiling..."
javac *.java
run: all
@echo "Running..."
java $(NAME)
clean:
rm -rf *.class
......@@ -7,6 +7,7 @@ Simple MVC examples using an incremental counter model. Each directory has a mak
* `hellomvc1/` separate view and controller classes, only 1 view
* `hellomvc2/` separate view and controller classes, multiple views
* `hellomvc3/` controller combined into view, multiple views
* `hellomvc3b/` controller combined into view, views use modelListeners
* `hellomvc4/` using Java's Observer interface and Observable base class
## Triangle Demos
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment