swarm-support
[Top][All Lists]
Advanced

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

Another call for volunteers (was Re: Drawing Objects


From: Paul E Johnson
Subject: Another call for volunteers (was Re: Drawing Objects
Date: Fri, 26 Jan 2001 10:36:38 -0600

"Marcus G. Daniels" wrote:
> 
 
> Otherwise, it's a question Swing programming, or some other Java-accessible
> GUI library.

I renew my call for volunteers to help me with this Swing project.  It
won't take much more work, but I need people with a little more
experience than I have with Swing.

I've been a bit underwhelmed by the response to my previous request. 
Swarm is a user community, at least I hope.  There must be somebody
there that could spare me an hour...

I've checked out Marcus's recommendation to use the gnome-gcj to write a
raster and I've decided to wait until gnome-libs 2.0 is released before
I dig into that. I could not compile a recent version of gnome-gcj and
the author said he is investing most of his effor planning for the
upcoming release of gnome.  When gnome 2 appears, I expect I will use it
to re-write the Raster class, but that is 10 months away
(optimistically).  I love the gnome library, gtk+ and glib, and think it
will be fun.

But in the interim, I need some java Swing help.  So email me. Here is
what I have so far.  As  you see if you run it, it does draw dots and
writes text on there, although for some reason I don't get, the
coordinates are not what I expect (I think the dots and words should be
in the same spot, but they aint, but that's a flaw in my agent code, not
raster, I think).   I want to be able to draw Pixmaps in here, and also
want to control fonts and so forth. I also want to comply with the
general javax guidelines on widgets, which I've not grasped yet, because
I got all befuddled reading a manual for JDK1.1 when I should have had
JDK1.3.  Some of the Swarm interface to the ZoomRaster class is not
correct, but I expect I can figure that part out.  (I cobbled this code
together after looking at Marcus's original TextRasterDemo, the jipd
example code from users contrib, and an example in the Sun Java
Tutorial.  If you think its jumbled in style, you are probably right)


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;


public class TextRasterDemo extends GUISwarmImpl {
    final int w = 256;
    final int h = 256;
 
    TextRaster 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(); 
            it.id = 0;
            agentList.add(it);
        } 
    
        textRaster = new TextRaster ("My zoom", w, h, 2 );
    }

    public void updateCoordinates (){
        for ( int i = 0; i < 10; i++) {
            ((Agent) agentList.get(i)).updatePosition();
            ((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 (), 
"clearPixels",
false));
        } catch (Exception e) {
            e.printStackTrace ();
        }

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

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

    public Activity activateIn (Swarm swarmContext) {
        super.activateIn (swarmContext);
    
        schedule.activateIn (this);
        return getActivity ();
    }
  
    public void move () {
        textRaster.repaint ();
        getActionCache ().doTkEvents ();
    }

 
    class SwarmFrame extends JFrame {
        public void update (Graphics g) {
            super.update(g);
            //paint (g);
        }
    }
  

    class Agent {
        int x = 0;
        int y = 0;
        int id = 0;
        public void updatePosition (){
            x = Globals.env.uniformIntRand.getIntegerWithMin$withMax (0, w-1);
            y = Globals.env.uniformIntRand.getIntegerWithMin$withMax (0, h-1);
        }
        
        public void drawAPoint(TextRaster r) {
            r.drawPointX$Y$Color(x,y,Color.black.getRGB() );
        }
    }


    class TextRaster 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();

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

            frame = new SwarmFrame ();
            frame.setBounds (200, 200, 200+ width*zf, 200+height*zf);
            frame.setTitle("title");
            frame.setBackground(Color.green);
            
            zoomFactor = zf;
            logicalWidth = width;
            logicalHeight = height;
            pixelWidth = zoomFactor*logicalWidth;
            pixelHeight = zoomFactor*logicalHeight;
            super.setSize (pixelWidth, pixelHeight);
            pixels = new int[pixelWidth* pixelHeight];
            clearPixels();

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

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


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

        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 update (Graphics g) {
            //super.update(g);
            //paint (g);
        }

        public void drawPointX$Y$Color (int lx, int ly, int c) {
            int rectangleX = lx*zoomFactor;
            int rectangleY = ly*zoomFactor;
            int rectangleWidth = zoomFactor;
            int rectangleHeight = zoomFactor;
            for (int i=rectangleX;i<rectangleX+rectangleWidth;i++)
                for (int j=rectangleY;j<rectangleY+rectangleHeight;j++){
                    pixels[i*pixelWidth+j] = c;
                }
            System.out.println("draw point");
            //imgsrc.newPixels(rectangleX, rectangleY,
            //                 rectangleWidth, rectangleHeight);
        }

        public void paint (Graphics g) {
        
            source.newPixels();
            g.drawImage (image, 0, 0, this);

            g.drawRect (10,10,30,30);
            g.fillRect(33, 22, 50, 10); 
        

            for (int i = 0; i< 10; i++){
                StringBuffer aName = new StringBuffer("Agent ");
                aName.append(i);
          
                g.drawString (aName.toString(), ((Agent) 
agentList.get(i)).x,((Agent)
agentList.get(i)).y );
            }
        }
    }

    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);
    }
}

-- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

                  ==================================
   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]