MASON-INTEREST-L Archives

September 2011

MASON-INTEREST-L@LISTSERV.GMU.EDU

Options: Use Monospaced Font
Show HTML Part by Default
Condense Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Mime-Version:
1.0
Sender:
MASON Multiagent Simulation Toolkit <[log in to unmask]>
Subject:
From:
Sam Brett <[log in to unmask]>
Date:
Thu, 22 Sep 2011 10:40:06 -0400
Content-Transfer-Encoding:
8bit
Content-Type:
text/plain; charset="ISO-8859-1"
Reply-To:
MASON Multiagent Simulation Toolkit <[log in to unmask]>
Parts/Attachments:
text/plain (83 lines)
Here is what I am doing now and am having the same problem:

public boolean handleMouseEvent(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3 &&amp; e.getID() ==
MouseEvent.MOUSE_CLICKED) {
            Point2D.Double p = new Point2D.Double(e.getX(), e.getY());

            Bag[] hitObjects = objectsHitBy(p);

            boolean empty = true;
            for (int i = 0; i < hitObjects.length; i++)
                if (hitObjects[i].numObjs > 0) {
                    empty = false;
                    break;
                }

            if (empty) {
                addPopup.setEvent(e);
                addPopup.show(this, e.getX(), e.getY());
                return true;
            } else {
                removePopup.removeAll();
                for (int x = hitObjects.length - 1; x >= 0; x--)
                    for (int i = 0; i < hitObjects[x].numObjs; i++) {
                        LocationWrapper wrapper = (LocationWrapper)
(hitObjects[x].objs[i]);
                        final Object obj = wrapper.getObject();
                        removePopup.add(new AbstractAction("Delete " +
obj.toString()) {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                synchronized (state.schedule) {
                                    IAgent agent = (IAgent) obj;
                                    if (agent instanceof Manager &&amp;
state.managers().length == 1) {
                                       
JOptionPane.showMessageDialog(InteractiveDisplay2D.this, "Need at least one
manager",
                                                "Manager Needed",
JOptionPane.ERROR_MESSAGE, null);
                                        return;
                                    }

                                    removeAgent(agent);
                                    if (agent instanceof Manager) {
                                       
ui.dataset.removeSeries(agent.toString() + " Profit");
                                        for (Restocker restocker :
((Manager) agent).getRestockers())
                                            removeAgent(restocker);
                                        for (CokeMachine machine :
((Manager) agent).getMachines())
                                            removeAgent(machine);
                                    }
                                }
                            }
                        });
                    }
            }
            removePopup.show(this, e.getX(), e.getY());
            return true;
        } else
            return super.handleMouseEvent(e);
    }

    private void removeAgent(IAgent agent) {
        for (IBehavior behavior : agent.getBehaviors())
            if (behavior.getStopper() != null)
                behavior.getStopper().stop();
        Manager[] managers = state.managers();
        for (Manager manager : managers) {
            if (agent instanceof Restocker)
                manager.getRestockers().remove(agent);
            else if (agent instanceof CokeMachine)
                manager.getMachines().remove(agent);
        }
        state.getAgents().remove(agent);
        state.agentGrid.remove(agent);
        ui.controller.refresh();
    }

IAgent and IBehavior are wrappers over MASON.

ATOM RSS1 RSS2