Source of MVCApplet.java


  1: // MVCApplet.java
  2: // A "shell" for any simple applet that uses an MVC design pattern 
  3: // in which the view and controller are combined.
  4: 
  5: import java.awt.*;
  6: import java.applet.Applet;
  7: import java.awt.event.*;
  8: 
  9: 
 10: public class AnyMVCApplet
 11:     extends Applet
 12:     implements ActionListener
 13: {
 14:     // Put instance variables here, such as ...
 15:     private Button aButton;
 16:     private Model aModelInstance;
 17: 
 18: 
 19:     // Override this method to set things up.
 20:     public void init()
 21:     {
 22:         aButton = new Button("Press Me");
 23:         add(aButton);
 24:         aButton.addActionListener(this);
 25:         
 26:         aModelInstance = new Model();
 27:     }
 28: 
 29: 
 30:     public void paint(Graphics g)
 31:     {
 32:         aModelInstance.display(g);
 33:     }
 34: 
 35: 
 36:     public void actionPerformed(ActionEvent event)
 37:     {
 38:         if(event.getSource() == aButton)
 39:         {
 40:             // Code to respond to button being pressed.
 41:             // For example ...
 42:             aModelInstance.getSomething();
 43:         }
 44:         repaint(); // if needed
 45:     }
 46: }
 47: 
 48: 
 49: class Model
 50: {
 51:     // Put (private) instance variables here ...
 52:     
 53:     public void setSomething()
 54:     {
 55:         // Code ...
 56:     }
 57:     
 58:     public void getSomething()
 59:     {
 60:         // Code ...
 61:     }
 62:     
 63:     public void display(Graphics g)
 64:     {
 65:         // Code to display current state
 66:     }
 67: }