Skip to content
Snippets Groups Projects
Commit 29172287 authored by Keiko Katsuragawa's avatar Keiko Katsuragawa
Browse files

add 3-0-MVC

parent 4e969ad9
No related branches found
No related tags found
No related merge requests found
Showing
with 761 additions and 0 deletions
// 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);
// 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);
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 (note, only supports 1 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 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);
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));
this.add(this.label);
// set the model
this.model = model;
// setup the event to go to the controller
addMouseListener(controller);
}
// 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 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 implements IView {
// the view's main user interface
private JButton button;
// the model that this view is showing
private Model model;
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;
// 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
this.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/
/**
* 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);
// create second view ...
View2 view2 = new View2(model);
// 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;
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
this.model.addView(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();
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
this.model.addView(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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment