Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • tintas/cs349_w18_examples
  • aycheng/cs349_w18_examples
  • j95he/cs349_w18_examples
  • skantor/cs349_w18_examples
  • ss22kim/cs349_w18_examples
  • w43wei/cs349_w18_examples
  • kkatsura/cs349_w18_examples
7 results
Show changes
Showing
with 777 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/
/**
* 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);
}
}
# 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 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);
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();
}
}
}
// 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.*;
import java.util.Observable;
import java.util.Observer;
class View extends JPanel implements Observer {
// 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
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();
}
});
}
// Observer interface
@Override
public void update(Observable arg0, Object arg1) {
System.out.println("View: update");
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 Observer {
// 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));
this.add(this.label);
// 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();
}
});
}
// Observer interface
@Override
public void update(Observable o, Object arg) {
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