Commit a807328f authored by Daniel Vogel's avatar Daniel Vogel

=added MVC demos

parent 20259324
......@@ -16,4 +16,6 @@
* `BarExercise` Demo of multiple transformation exercise.
* `/shapedemo` Example shape model (`ShapeDemo.java` and `Shape.java`).
\ No newline at end of file
......@@ -27,14 +27,15 @@ public class ShapeDemo extends JPanel {
shape.setColour(Color.BLUE);
// try uncommenting this block ...
// // remember transformations are post-multipied ...
// AffineTransform T = new AffineTransform();
// // then scale
// T.concatenate(AffineTransform.getScaleInstance(-1, 1));
// // translate first
// T.concatenate(AffineTransform.getTranslateInstance(-300,0));
// shape.setTransform(T);
// remember transformations are post-multipied ...
// let's mirror the shape ...
AffineTransform T = new AffineTransform();
// then scale
T.concatenate(AffineTransform.getScaleInstance(-1, 1));
// translate first
T.concatenate(AffineTransform.getTranslateInstance(-300,0));
shape.setTransform(T);
repaint();
}
......
// HelloMVC: a simple MVC example
// the model is just a counter
// inspired by code by Joseph Mack, http://www.austintek.com/mvc/
// (C) Joseph Mack 2011, jmack (at) wm7d (dot) net, released under GPL v3 (or any later version)
import java.awt.event.*;
class Controller implements ActionListener {
Model model;
Controller(Model model) {
this.model = model;
}
// event from the view's button
public void actionPerformed(java.awt.event.ActionEvent e){
System.out.println("Controller: changing Model");
model.incrementCounter();
}
}
// HelloMVC: a simple MVC example
// the model is just a counter
// inspired by code by Joseph Mack, http://www.austintek.com/mvc/
/**
* One view. Separate controller.
*/
import javax.swing.*;
import java.awt.Dimension;
import java.awt.event.*;
public class Main{
public static void main(String[] args){
JFrame frame = new JFrame("HelloMVC1");
// create Model and initialize it
Model model = new Model();
// create Controller, tell it about model
Controller controller = new Controller(model);
// create View, tell it about model and controller
View view = new View(model, controller);
// tell Model about View.
model.setView(view);
// create the window
frame.getContentPane().add(view);
frame.setPreferredSize(new Dimension(300,300));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
// 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;
// the view
IView view;
// set the view observer
public void setView(IView view) {
this.view = 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);
notifyObserver();
}
}
// notify the IView observer
private void notifyObserver() {
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 implements IView {
// the view's main user interface
private JButton button;
// the model that this view is showing
private Model model;
View(Model model, Controller controller) {
// 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;
// setup the event to go to the controller
button.addActionListener(controller);
}
// IView interface
public void updateView() {
System.out.println("View: updateView");
button.setText(Integer.toString(model.getCounterValue()));
}
}
# 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
// HelloMVC: a simple MVC example
// the model is just a counter
// inspired by code by Joseph Mack, http://www.austintek.com/mvc/
// (C) Joseph Mack 2011, jmack (at) wm7d (dot) net, released under GPL v3 (or any later version)
import java.awt.event.*;
class Controller implements ActionListener, MouseListener {
Model model;
Controller(Model model) {
this.model = model;
}
// event from the view's button
public void actionPerformed(java.awt.event.ActionEvent e){
System.out.println("Controller: changing Model (actionPerformed)");
model.incrementCounter();
}
public void mouseClicked(MouseEvent e) {
System.out.println("Controller: changing Model (mouseClicked)");
model.incrementCounter();
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
}
// HelloMVC: a simple MVC example
// the model is just a counter
// inspired by code by Joseph Mack, http://www.austintek.com/mvc/
/**
* Two views coordinated with the observer pattern. Separate controller.
* The mechanics of a separate controller are starting to break down.
*/
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("HelloMVC2");
// create Model and initialize it
Model model = new Model();
// create Controller, tell it about model
Controller controller = new Controller(model);
// create View, tell it about model and controller
View view = new View(model, controller);
// tell Model about View.
model.addView(view);
// create second view ...
View2 view2 = new View2(model, controller);
model.addView(view2);
// create the window
JPanel p = new JPanel(new GridLayout(2,1));
frame.getContentPane().add(p);
p.add(view);
p.add(view2);
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);
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 implements IView {
// the view's main user interface
private JButton button;
// the model that this view is showing
private Model model;
View(Model model, Controller controller) {
// 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;
// setup the event to go to the controller
button.addActionListener(controller);
}
// IView interface
public void updateView() {
System.out.println("View: updateView");
button.setText(Integer.toString(model.getCounterValue()));
}
}
// 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 implements IView {
// the model that this view is showing
private Model model;
private JLabel label = new JLabel();
View2(Model model, Controller controller) {
// create UI
setBackground(Color.WHITE);
setLayout(new FlowLayout(FlowLayout.LEFT));
// set the model
this.model = model;
// setup the event to go to the controller
addMouseListener(controller);
//addActionListener(controller);
this.add(this.label);
}
// IView interface
public void updateView() {
System.out.println("View2: updateView");
// just displays an 'X' for each counter value
String s = "";
for (int i=0; i<this.model.getCounterValue(); i++) s = s + "X";
this.label.setText(s);
}
}
# 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
// HelloMVC: a simple MVC example
// the model is just a counter
// inspired by code by Joseph Mack, http://www.austintek.com/mvc/
/**
* Two views with integrated controllers.
*/
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("HelloMVC3");
// create Model and initialize it
Model model = new Model();
// create View, tell it about model
View view = new View(model);
// tell Model about View.
model.addView(view);
// create second view ...
View2 view2 = new View2(model);
model.addView(view2);
// create the window
JPanel p = new JPanel(new GridLayout(2,1));
frame.getContentPane().add(p);
p.add(view);
p.add(view2);
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 implements IView {
// the view's main user interface
private JButton button;
// the model that this view is showing
private 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_;
// 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();
}
});
}
// IView interface
public void updateView() {
System.out.println("View: updateView");
button.setText(Integer.toString(model.getCounterValue()));
}
}
// 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 implements IView {
// the model that this view is showing
private Model model;
private JLabel label = new JLabel();
View2(Model model_) {
// create UI
setBackground(Color.WHITE);
setLayout(new FlowLayout(FlowLayout.LEFT));
// set the model
model = model_;
// 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);
}
// IView interface
public void updateView() {
System.out.println("View2: updateView");
// just displays an 'X' for each counter value
String s = "";
for (int i=0; i<this.model.getCounterValue(); i++) s = s + "X";
this.label.setText(s);
}
}
# 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
// HelloMVC: a simple MVC example
// the model is just a counter
// inspired by code by Joseph Mack, http://www.austintek.com/mvc/
/**
* Two views with integrated controllers. Uses java.util.Observ{er, able} instead
* of custom IView.
*/
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("HelloMVC4");
// create Model and initialize it
Model model = new Model();
// create View, tell it about model (and controller)
View view = new View(model);
// tell Model about View.
model.addObserver(view);
// create second view ...
View2 view2 = new View2(model);
model.addObserver(view2);
// let all the views know that they're connected to the model
model.notifyObservers();
// create the window
JPanel p = new JPanel(new GridLayout(2,1));
frame.getContentPane().add(p);
p.add(view);
p.add(view2);
frame.setPreferredSize(new Dimension(300,300));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.util.Observable;
// HelloMVC: a simple MVC example
// the model is just a counter
// inspired by code by Joseph Mack, http://www.austintek.com/mvc/
public class Model extends Observable {
// the data in the model, just a counter
private int counter;
Model() {
setChanged();
}
public int getCounterValue() {
return counter;
}
public void incrementCounter() {
if (counter < 5) {
counter++;
System.out.println("Model: increment counter to " + counter);
setChanged();
notifyObservers();
}