0

编辑:当 field 或 hunterField 在任何位置都没有对象时,一切都会正确显示。field 和 hunterField 都专门持有扩展同一个类的对象,所以我猜它可能与继承有关......?

我使用 MASON 创建了一个简单的基于代理的模型。后端工作找到,但是当我尝试显示我的代理时,只显示“墙”代理。(墙壁描绘)我的代码在下面......有什么想法吗?

package sim.app.celai;

import java.awt.Color;

import javax.swing.JFrame;

import sim.app.tutorial3.Tutorial3WithUI;
import sim.display.Controller;
import sim.display.Display2D;
import sim.display.GUIState;
import sim.portrayal.grid.SparseGridPortrayal2D;
import sim.util.Bag;

public class FieldWithGUI extends GUIState {

    public Display2D display;
    public JFrame frame;


    SparseGridPortrayal2D hunterPortrayal = new SparseGridPortrayal2D();
    SparseGridPortrayal2D wallPortrayal = new SparseGridPortrayal2D();
    SparseGridPortrayal2D childPortrayal = new SparseGridPortrayal2D();
    public FieldWithGUI() {

        super(new Field(System.currentTimeMillis()));

    }

    public void setupPortrayals() {

        childPortrayal.setField(((Field) state).field);
        hunterPortrayal.setField(((Field) state).hunterField);
        wallPortrayal.setField(((Field) state).wallField);


        childPortrayal.setPortrayalForAll(new sim.portrayal.simple.OvalPortrayal2D(Color.blue));
        hunterPortrayal.setPortrayalForAll(new sim.portrayal.simple.OvalPortrayal2D(Color.red));
        wallPortrayal.setPortrayalForAll(new sim.portrayal.simple.OvalPortrayal2D(Color.green));

        display.reset();
        display.repaint();


    }

    public void quit()
    {
    super.quit();

    if (frame!=null) frame.dispose();
    frame = null;  // let gc
    display = null;       // let gc
    }


    public static void main(String[] args)
    {
      new FieldWithGUI().createController();
    }

    public void start()
    {
    super.start();
    // set up our portrayals
    setupPortrayals();
    }

    public void init(Controller c)
    {
    super.init(c);

    // Make the Display2D.  We'll have it display stuff later.
    display = new Display2D(400,400,this); // at 400x400, we've got 4x4 per array position
    frame = display.createFrame();
    c.registerFrame(frame);   // register the frame so it appears in the "Display" list
    frame.setVisible(true);

    // specify the backdrop color  -- what gets painted behind the displays
    display.setBackdrop(Color.black);

    // attach the portrayals
    display.attach(childPortrayal, "children");
    display.attach(hunterPortrayal, "hunter");
    display.attach(wallPortrayal, "wall");
    }

}
4

0 回答 0