swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Would one of my Java friends test drive this Swarm Raster class?


From: Paul E Johnson
Subject: Would one of my Java friends test drive this Swarm Raster class?
Date: Sat, 03 Feb 2001 16:27:28 -0600
User-agent: Mozilla/5.0 (X11; U; Linux 2.2.16-22 i686; en-US; 0.7) Gecko/20010126

// Paul Johnson Feb.3, 2001
// This is my effort to build a class that can substitute for Swarm's ZoomRaster. // There is a layer of pixels, accessed from agents by the same drawPointX$Y()
// as in the objc swarm.
// I also wanted to be able to draw SHAPES and TEXT and IMAGES on TOP of the
// raster dots. // I ended up taking this approach that the Raster object buffers the pixels, // strings, rectangles, filled rectangles, and images. It does this by creating // objects that it stores in a collection, each time it is told to add a feature
// in the picture.  Then at paint() time,
// it draws the pixels first, and then goes through the collections of shapes,
// strings, and images.

// This seemed kludgy to me, but every other way I tried would not layer
// the things in the right order. One problem is that, as this thing runs, the // text, which is supposed to be shown with the dots, does not always keep up.

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;

import javax.swing.JFrame;
import javax.swing.JComponent;

import java.awt.Container;
import java.awt.Image;

import java.awt.Graphics;
import java.awt.image.MemoryImageSource;
import java.awt.Window;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.Math;


public class TextRasterDemo extends GUISwarmImpl {
  final int w = 100;
  final int h = 100;

  SwarmRaster textRaster;

  ArrayList agentList;

  Schedule schedule;

  TextRasterDemo (Zone aZone) {
  super (aZone);

  agentList = new ArrayList( 10);

  for (int i = 0; i < 10; i++){
      Agent it = new Agent(i);
      agentList.add(it);
  }
textRaster = new SwarmRaster ("My zoom", w, h, 6 ); }

  public void stepThroughAgents (){
  for ( int i = 0; i < 10; i++) {
      ((Agent) agentList.get(i)).updatePosition();
  }

  for ( int i = 0; i < 10; i++) {
      ((Agent) agentList.get(i)).drawAPoint (textRaster);
  }
}

  public Object buildActions () {
  schedule = new ScheduleImpl (getZone (), 1);
try {
      schedule.at$createActionTo$message
(0, textRaster, new Selector (textRaster.getClass (), "clear", false));
  } catch (Exception e) {
      e.printStackTrace ();
  }

  try {
      schedule.at$createActionTo$message
      (0, this, new Selector (getClass (), "stepThroughAgents", false));
  } catch (Exception e) {
      e.printStackTrace ();
  }

  try {
      schedule.at$createActionTo$message
      (0, this, new Selector (getClass (), "_update_display_", false));
  } catch (Exception e) {
      e.printStackTrace ();
  }
  return this;
  }

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

  public void _update_display_ () {
  textRaster.drawSelf();
  getActionCache ().doTkEvents ();
  }







  class Agent {
  int x = 0;
  int y = 0;
  int id = 0;
  StringBuffer myName;
  public Agent(int identification){
      id = identification;
      x= w - 30 - 3* id;
      y= h - 30 - 3* id;
      myName = new StringBuffer("Agent ");
      myName.append(id);

      //myI=getImage(getDocumentBase(),"joe.surf.yellow.small.gif");

  }
public void updatePosition (){
      x = Globals.env.uniformIntRand.getIntegerWithMin$withMax (10, w-10);
      y = Globals.env.uniformIntRand.getIntegerWithMin$withMax (10, h-10);
  }
public void drawAPoint(SwarmRaster r) {
      StringBuffer announcement = new StringBuffer("Ag#");
      announcement.append(id + " (" + x + "," + y + ")");
r.drawPointX$Y$Color(x,y,id+Color.red.getRGB() );
      //r.addString(myName.toString(),x,y);
      r.addString(announcement.toString(),x,y);

      if(id == 5)
      r.fillRectangleX0$Y0$X1$Y1(12, 14, 9,10,Color.black.getRGB());
      if (id == 7 )
r.rectangleX0$Y0$X1$Y1(7+id*3,3+id*5, 3+id*3,19+id*5,Color.green.getRGB());

  }
  }


  class SwarmRaster extends JComponent{
JFrame frame = null;
  Image image = null;
  MemoryImageSource source = null;
  int logicalWidth = 0;
  int logicalHeight = 0;
  int zoomFactor = 1;
  int pixelWidth = 0;
  int pixelHeight = 0;
  int[] pixels;

  int backgroundColor = Color.white.getRGB();
  ArrayList rasterStringList;
  ArrayList rasterRectList;
  ArrayList rasterFRectList;
  ArrayList rasterImageList;

  public SwarmRaster (String title, int width, int height, int zf) {
      super();

      frame = new JFrame ();
frame.setBounds (100, 100, 1+width*zf, 1+height*zf); frame.show(); frame.setTitle("title");
      frame.setBackground(Color.green);
zoomFactor = zf;
      logicalWidth = width;
      logicalHeight = height;
      pixelWidth = zoomFactor*logicalWidth;
      pixelHeight = zoomFactor*logicalHeight;
      pixels = new int[pixelWidth* pixelHeight];
repaint();

      rasterStringList= new ArrayList();
      rasterRectList= new ArrayList();
      rasterFRectList = new ArrayList();
      rasterImageList = new ArrayList();

source = new MemoryImageSource (pixelWidth, pixelHeight, pixels, 0, pixelWidth);
      source.setAnimated (true);
      image = createImage (source);
      setVisible(true);

      frame.getContentPane().add (this);
      frame.show ();
}

  private class RasterString
  {
      public String aString;
      public int posx;
      public int posy;
      public RasterString(String s, int newx, int newy){
      aString = s;
      posx = newx;
      posy = newy;
      }
  }
private class RasterImage
  {
      public Image aImage;
      public int posx;
      public int posy;
      public RasterImage(Image img, int newx, int newy){
      aImage = img;
      posx = newx;
      posy = newy;
      }
  }

  private class RasterRect{
      public int posx;
      public int posy;
      public int width;
      public int height;
      public int color;
      public RasterRect( int newx, int newy, int wid, int hei, int col ){
      posx = newx;
      posy = newy;
      width = wid;
      height = hei;
      color = col;
      }
  }

public void setBackgroundColor (int x){
      backgroundColor = x;
  }


  public void clear(){
      clearPixels();
      rasterStringList.clear();
      rasterImageList.clear();
      rasterFRectList.clear();
      rasterRectList.clear();
      rasterImageList.clear();
  }

  public void clearPixels() {
      setPixelBuffer(backgroundColor);
  }


  public void setPixelBuffer(int c) {
      for (int i=0;i<pixelWidth;i++)
      for (int j=0;j<pixelHeight;j++)
          pixels[i*pixelWidth + j] = c;
  }

public void rectangleX0$Y0$X1$Y1 (int x0, int y0, int x1, int y1, int color){ RasterRect aRect = new RasterRect(zoomFactor*x0,zoomFactor*y0,zoomFactor*Math.abs(x1-x0),zoomFactor*Math.abs(y1-y0),color);
      rasterRectList.add(aRect);
  }

public void fillRectangleX0$Y0$X1$Y1 (int x0, int y0, int x1, int y1, int color){ RasterRect aRect = new RasterRect(zoomFactor*x0,zoomFactor*y0,zoomFactor*Math.abs(x1-x0),zoomFactor*Math.abs(y1-y0),color);
      rasterFRectList.add(aRect);
  }

  public void drawPointX$Y$Color (int lx, int ly, int c) {
      int rectangleX = lx*zoomFactor;
      int rectangleY = ly*zoomFactor;
for (int i=rectangleX;i<rectangleX+zoomFactor;i++)
      for (int j=rectangleY;j<rectangleY+zoomFactor;j++){
          pixels[j*pixelWidth+i] = c;
      }
  }


  public void addString (String s, int lx, int ly){
RasterString aString = new RasterString(s,zoomFactor*lx,zoomFactor*ly);
      rasterStringList.add(aString);
  }


  public void addImage (Image img, int lx, int ly){
RasterImage anImage = new RasterImage(img,zoomFactor*lx,zoomFactor*ly);
      rasterImageList.add(anImage);
    }

  public Object drawSelf(){
      source.newPixels();
      return this;
  }

  public void paint (Graphics g) {
      g.drawImage (image, 0, 0, this);
for (Iterator index=rasterFRectList.iterator(); index.hasNext(); ) {
        RasterRect aRasterRect = (RasterRect) index.next();
g.fillRect (aRasterRect.posx,aRasterRect.posy,aRasterRect.width,aRasterRect.height);
      //Caution: This does not adjust the color to match user request.
      }

      for (Iterator index=rasterRectList.iterator(); index.hasNext(); ) {
        RasterRect aRasterRect = (RasterRect) index.next();
g.drawRect (aRasterRect.posx,aRasterRect.posy,aRasterRect.width,aRasterRect.height);
      //Caution: This does not adjust the color to match user request.
      }

      for (Iterator index=rasterStringList.iterator(); index.hasNext(); ) {
        RasterString aRasterString = (RasterString) index.next();
g.drawString (aRasterString.aString, aRasterString.posx,aRasterString.posy);
        }

      for (Iterator index=rasterImageList.iterator(); index.hasNext(); ) {
        RasterImage aRasterImage = (RasterImage) index.next();
g.drawImage (aRasterImage.aImage, aRasterImage.posx,aRasterImage.posy, this);
        }
  }
  }

  static public void main (String args[]) {
  Globals.env.initSwarm ("TextRasterDemo", "0.0", "address@hidden",
                 args);
TextRasterDemo textRasterDemo =
      new TextRasterDemo (Globals.env.globalZone);
textRasterDemo.buildObjects ();
  textRasterDemo.buildActions ();
  textRasterDemo.activateIn (null);
  textRasterDemo.go ();
  System.exit (0);
  }
}




                 ==================================
  Swarm-Support is for discussion of the technical details of the day
  to day usage of Swarm.  For list administration needs (esp.
  [un]subscribing), please send a message to <address@hidden>
  with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]