/* Main.java */

package CounterDemo; 

import javax.swing.*;

public class Main {
    public static void main(String args[]) {
  Counter c = new Counter(true);
  Screen s = new Screen("Screen"100100100100);
  CounterObserver so = new ScreenObserver(s,c);
  CounterObserver co = new ConsoleObserver(c);
  c.setObserver(so);
  Control l = new Control(c, so, co);
    }
    static {
  // use the platform look and feel by default
  try {
      UIManager.setLookAndFeel
    (UIManager.getSystemLookAndFeelClassName());
  catch (Exception e) { }
    }
}
Java2html

/* ConsoleObserver.java */

package CounterDemo;

class ConsoleObserver implements CounterObserver 
    Counter counter;
    ConsoleObserver(Counter c) {
  counter = c;
    }

    public void value() {
  System.out.print("Counter Value = ");
  System.out.print(counter.value());
  System.out.print("\n");
    }

    public void incremented() {
  System.out.print("Counter Incremented, New Value = ");
  System.out.print(counter.value());
  System.out.print("\n");
    }

    public void decremented() {
  System.out.print("Counter Decremented, New Value = ");
  System.out.print(counter.value());
  System.out.print("\n");
    }
}
Java2html

/* Control.java */

package CounterDemo;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class Control extends JPanel implements ActionListener {
    protected JButton b1, b2, b3, b4;
    CounterObserver screen, console;
    protected Counter counter;

    public Control(Counter c, CounterObserver so, CounterObserver co) {
        counter = c;
  screen = so;
  console = co;
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI()
            }
        });
    }

    public void initialize() {
        b1 = new JButton("Incrémente");
        b1.setActionCommand("increment");

        b2 = new JButton("Décrémente");
        b2.setActionCommand("decrement");

        b3 = new JButton("Fenêtre");
        b3.setActionCommand("screen");

        b4 = new JButton("Console");
        b4.setActionCommand("console");

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);

        b1.setToolTipText("Incrémente le compteur");
        b2.setToolTipText("Décrémente le compteur");
        b3.setToolTipText("Affiche le résultat sur la fenêtre");
        b4.setToolTipText("Affiche le résultat sur la console");

        //Ajout de composants au conteneur avec le FlowLayout par défaut.
        add(b1);
        add(b2);
        add(b3);
        add(b4);
    }

    public void actionPerformed(ActionEvent e) {
        if ("increment".equals(e.getActionCommand())) {
    counter.increment();
  else if ("decrement".equals(e.getActionCommand())) {
      counter.decrement();
  else if ("screen".equals(e.getActionCommand())) {
      counter.setObserver(screen);
  else if ("console".equals(e.getActionCommand())) {
      counter.setObserver(console);
  }
    }
    

    /**
     * Création de l'interface et affichage.
     * Doit être invoqué du fil d'événements.
     */
    private void createAndShowGUI() {
        // Décorations des fenêtres
        JFrame.setDefaultLookAndFeelDecorated(true);

        // Création de la fenêtre.
        JFrame frame = new JFrame("Exemple MVC Counter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Création du panneau de contenu.
  initialize();
        this.setOpaque(true)// le panneau doit être opaque
        frame.setContentPane(this);

        // Affichage de la fenêtre.
        frame.pack();
        frame.setVisible(true);
    }
}
Java2html

/* Counter.java */

package CounterDemo; 

public class Counter 
    int value = 0;
    CounterObserver observer;
    //    boolean wait = true;
    boolean wait = false;

    Counter(boolean w) { wait = w; }
    public void setObserver(CounterObserver co) {
  observer = co;
  co.value();
    }
    public void increment() {
  if (wait) { 
      try {
      Thread.sleep(1000);
      catch (Exception e) {}
  }
  CounterObserver o;
  o = observer;
  value++;
  if (o != nullo.incremented();
    
    public void decrement() {
  CounterObserver o; 
  synchronized (this) {
      o = observer;
      value--;
  
  if (o != nullo.decremented();
    }
    public int value() { return value; 
}
Java2html

/* CounterObserver.java */

package CounterDemo;

interface CounterObserver 
    public void value();
    public void incremented();
    public void decremented();
}
Java2html

/* Screen.java */

package CounterDemo;

import java.io.IOException;
import java.io.StringReader;
import javax.swing.*;        
import javax.swing.event.*;
import javax.swing.text.html.*;
import java.awt.Color;
import java.util.*;
import java.text.*;

public class Screen {
    private JEditorPane jep;
    private JScrollPane jsp;
    private JFrame frame;
    private String text = "<font size=\"+1\">";

    Screen(final String s, final int lx, final int uy, final int sx, final int sy) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(s, lx, uy, sx, sy);
            }
  });
    }

    public void setText(final String s) {
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        text = s;
        jep.setText("<font size=\"+1\">" + s);
    }});
    }

    public void createAndShowGUI(String title, int lx, int uy, int sx, int sy) {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        frame = new JFrame(title);
        frame.setBackground(Color.white);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // Some text.
  jep = new JEditorPane("text/html", text);
  jep.setCaretPosition(0);
  jep.setEditable(false);
        jsp = new JScrollPane(jep);
  jsp.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

  frame.add(jsp);

  // CSS stylesheet for this text
  String styles = "a { text-decoration: none; }";
  StyleSheet s = ((HTMLEditorKit)jep.getEditorKit()).getStyleSheet();
  try {
      s.loadRules(new StringReader(styles)null);
  catch (IOException e) { /* should never happen */ }

        //Display the window.
        frame.pack();
  frame.setSize(sx, sy);
  frame.setLocation(lx, uy);
        frame.setVisible(true);
    }
}
Java2html

/* ScreenObserver.java */

package CounterDemo;

class ScreenObserver implements CounterObserver 
    Screen screen;
    Counter counter;
    ScreenObserver(Screen s, Counter c) {
  screen = s;
  counter = c;
    }

    public void value() {
  String text = Integer.toString(counter.value());
  screen.setText(text);
    }

    public void incremented() {
  String text = Integer.toString(counter.value());
  screen.setText(text);
    }

    public void decremented() {
  String text = Integer.toString(counter.value());
  screen.setText(text);
    }
}
Java2html