/* Marcus Daniels November 21, 1999 swarm-support
CS> So the best way to get past this would be to unset all the colours
CS> before setting them, and to do this after the call to ZoomRaster
CS> setColormap: ?

setColormap: is what augments the palette (or creates a private colormap),
so that isn't a robust solution.

However, the code below shows how to get all the colors you'd ever want in 
Swarm for Java.

Requires JDK 1.2, Kaffe's AWT doesn't have all the needed features.
Tested with:
 
 ftp://ftp.santafe.edu/pub/swarm/2.0.1-fixes/
    javaswarm.dll.gz  (unpack and install in $SWARMHOME/bin)
    swarm.jar.gz      (unpack and install in $SWARMHOME/share/swarm)
*/
import java.awt.Frame;
import java.awt.Event;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.image.MemoryImageSource;
import java.awt.image.IndexColorModel;

import swarm.Globals;
import swarm.activity.Schedule;
import swarm.activity.ScheduleImpl;
import swarm.activity.Activity;
import swarm.objectbase.Swarm;
import swarm.simtoolsgui.GUISwarmImpl;
import swarm.defobj.Zone;
import swarm.Selector;

public class ColormapDemo extends Frame {
  final int w = 128;
  final int h = 128;
  final int gradations = 128;
  int pixels[] = new int[w * h];
  MemoryImageSource source;
  Image image;
  ObserverSwarm observerSwarm;

  class ObserverSwarm extends GUISwarmImpl {
    Schedule schedule;
    Schedule stopSchedule;

    ObserverSwarm (Zone aZone) {
      super (aZone);
    }

    public Object buildActions () {
      schedule = new ScheduleImpl (getZone (), 1);
      stopSchedule = new ScheduleImpl (getZone (), true);
      
      try {
        schedule.at$createActionTo$message
          (0, this, new Selector (getClass (), "step", false));
        stopSchedule.at$createActionTo$message
          (gradations - 1,
           this,
           new Selector (getClass (), "stop", false));
      } catch (Exception e) {
        e.printStackTrace ();
      }
      return this;
    }

    public Activity activateIn (Swarm swarmContext) {
      super.activateIn (swarmContext);
      
      schedule.activateIn (this);
      stopSchedule.activateIn (this);
      return getActivity ();
    }

    public Object step () {
      for (int i = 0; i < w * h; i++)
        pixels[i] += w * h;
      source.newPixels (0, 0, w, h);
      ColormapDemo.this.repaint ();
      return this;
    }
  
    public Object stop () {
      getControlPanel ().setStateStopped ();
      return this;
    }
  }

  public ColormapDemo () {
    super ();

    byte red[] = new byte[w * h * gradations];
    byte green[] = new byte[w * h * gradations];
    byte blue[] = new byte[w * h * gradations];
    
    for (int ri = 0; ri < gradations; ri++) {
      int roffset = ri * w * h;
      for (int gi = 0; gi < h; gi++) {
        int goffset = gi * w;
        for (int bi = 0; bi < w; bi++) {
          int boffset = goffset + bi;
          int offset = roffset + boffset;
          red[offset] = (byte) (ri * 255 / (gradations - 1));
          blue[offset] = (byte) (bi * 255 / (w - 1));
          green[offset] = (byte) (gi * 255 / (h - 1));
          pixels[boffset] = boffset;
        }
      }
    }
    IndexColorModel cm = new IndexColorModel (21, 2097152,
                                              red, green, blue);
    source = new MemoryImageSource (w, h, cm, pixels, 0, w);
    source.setAnimated (true);
    image = createImage (source);
    setBounds (100, 100, w, h);
    show ();

    observerSwarm = new ObserverSwarm (Globals.env.globalZone);
    
    observerSwarm.buildObjects ();
    observerSwarm.buildActions ();
    observerSwarm.activateIn (null);
    observerSwarm.go ();
  }

  public void update (Graphics g) {
    paint (g);
  }

  public void paint (Graphics g) {
    g.drawImage (image, 0, 0, this);
  }
  
  public boolean processEvent (Event event) {
    if (event.id == Event.WINDOW_DESTROY)
      System.exit (0);
    
    return false;
  }

  static public void main (String args[]) {
    Globals.env.initSwarm ("ColormapDemo", "0.0", "bug-swarm@santafe.edu",
                           args);
    
    ColormapDemo colormapDemo = new ColormapDemo ();
  }
}


