package com.annimon.jecp.demo;

import com.annimon.jecp.ApplicationListener;
import com.annimon.jecp.Graphics;
import com.annimon.jecp.demo.screens.*;

/**
 * @author aNNiMON
 */
public class Main implements ApplicationListener, Screen.OnNextScreenListener {
    
    private static final int SCREEN_FIRST = 1, SCREEN_LAST = 5;
    
    private int width, height;
    
    private int screenIndex;
    private Screen currentScreen;
    
    public void onStartApp(int width, int height) {
        this.width = width;
        this.height = height;
        
        screenIndex = SCREEN_FIRST;
        initScreen();
    }

    public void onPauseApp() { }

    public void onDestroyApp() { }

    public void onPaint(Graphics g) {
        currentScreen.onPaint(g);
    }

    public void onUpdate() {
        currentScreen.onUpdate();
    }

    public void onNextScreen() {
        screenIndex++;
        if (screenIndex > SCREEN_LAST) {
            screenIndex = SCREEN_FIRST;
        }
        initScreen();
    }
    
    private void initScreen() {
        switch (screenIndex) {
            case 1:
                currentScreen = new LinesScreen(width, height);
                break;
            case 2:
                currentScreen = new CellsScreen(width, height);
                break;
            case 3:
                currentScreen = new ControllingSquareScreen(width, height);
                break;
            case 4:
                currentScreen = new VisualMEScreen(width, height);
                break;
            case 5:
                currentScreen = new PolygonScreen(width, height);
                break;
        }
        currentScreen.setOnNextScreenListener(this);
    }
}

@@ -132,22 +132,146 @@
 ecp.
-demo.screens.*
+Prefs;%0Aimport com.annimon.jecp.demo.screens.*;%0Aimport java.io.DataInputStream;%0Aimport java.io.DataOutputStream;%0Aimport java.io.IOException
 ;%0A%0A/
@@ -669,24 +669,271 @@
 t;%0A        %0A
+        // Load preferences.%0A        try %7B%0A            DataInputStream prefs = Prefs.loadAsStream();%0A            screenIndex = prefs.readInt();%0A        %7D catch (IOException ex) %7B%0A            // If unable to load preferences, init by defaults.%0A    
         scre
@@ -948,32 +948,42 @@
 = SCREEN_FIRST;%0A
+        %7D%0A
         initScre
@@ -1057,24 +1057,254 @@
 stroyApp() %7B
+%0A        // Store preferences.%0A        Prefs.saveAsStream(new Prefs.StoreListener() %7B%0A            public void save(DataOutputStream dos) throws IOException %7B%0A                dos.writeInt(screenIndex);%0A            %7D%0A        %7D);%0A   
  %7D%0A%0A    publ

Add example for Prefs load and store as stream