Basic WorldView
This commit is contained in:
parent
0148eefab1
commit
7197d94939
9 changed files with 276 additions and 2 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -12,7 +12,7 @@
|
|||
|
||||
MAS airSeaRescue {
|
||||
|
||||
infrastructure: Local
|
||||
environment: operator.WorldEnvironment(3,50,yes)
|
||||
|
||||
agents: sample_agent;
|
||||
}
|
||||
|
|
|
@ -1,10 +1,60 @@
|
|||
package operator;
|
||||
|
||||
import jason.asSyntax.Structure;
|
||||
import jason.environment.Environment;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class WorldEnvironment extends Environment {
|
||||
|
||||
private Logger logger = Logger.getLogger("jasonTeamSimLocal.mas2j." + WorldModel.class.getName());
|
||||
WorldModel model;
|
||||
WorldView view;
|
||||
|
||||
int sleep = 0;
|
||||
|
||||
@Override
|
||||
public void init(String[] args) {
|
||||
super.init(args);
|
||||
sleep = Integer.parseInt(args[1]);
|
||||
model = new WorldModel(35, 35, 4);
|
||||
view = new WorldView(model);
|
||||
view.setEnv(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean executeAction(String ag, Structure action) {
|
||||
boolean result = false;
|
||||
try {
|
||||
// TODO: add actions
|
||||
/* // get the agent id based on its name
|
||||
int agId = getAgIdBasedOnName(ag);
|
||||
|
||||
if (action.equals(up)) {
|
||||
result = model.move(Move.UP, agId);
|
||||
} else if (action.equals(down)) {
|
||||
result = model.move(Move.DOWN, agId);
|
||||
} else if (action.equals(right)) {
|
||||
result = model.move(Move.RIGHT, agId);
|
||||
} else if (action.equals(left)) {
|
||||
result = model.move(Move.LEFT, agId);
|
||||
} else if (action.equals(skip)) {
|
||||
result = true;
|
||||
} else if (action.equals(pick)) {
|
||||
result = model.pick(agId);
|
||||
} else if (action.equals(drop)) {
|
||||
result = model.drop(agId);
|
||||
view.udpateCollectedGolds();
|
||||
} else {
|
||||
logger.info("executing: " + action + ", but not implemented!");
|
||||
}
|
||||
if (result) {
|
||||
updateAgPercept(agId);
|
||||
return true;
|
||||
}*/
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "error executing " + action + " for " + ag, e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
17
src/java/operator/WorldModel.java
Normal file
17
src/java/operator/WorldModel.java
Normal file
|
@ -0,0 +1,17 @@
|
|||
package operator;
|
||||
|
||||
import jason.environment.grid.GridWorldModel;
|
||||
|
||||
public class WorldModel extends GridWorldModel {
|
||||
|
||||
|
||||
public static final int VICTIM = 16;
|
||||
public static final int DEPOT = 32;
|
||||
public static final int ENEMY = 64;
|
||||
|
||||
public WorldModel(int w, int h, int nbAgs) {
|
||||
super(w, h, nbAgs);
|
||||
}
|
||||
|
||||
|
||||
}
|
207
src/java/operator/WorldView.java
Normal file
207
src/java/operator/WorldView.java
Normal file
|
@ -0,0 +1,207 @@
|
|||
package operator;
|
||||
|
||||
import jason.environment.grid.GridWorldModel;
|
||||
import jason.environment.grid.GridWorldView;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.Hashtable;
|
||||
|
||||
public class WorldView extends GridWorldView {
|
||||
|
||||
WorldEnvironment env = null;
|
||||
|
||||
public WorldView(GridWorldModel model) {
|
||||
super(model, "Rescue World", 600);
|
||||
setVisible(true);
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void setEnv(WorldEnvironment env) {
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
JLabel jlMouseLoc;
|
||||
JComboBox scenarios;
|
||||
JSlider jSpeed;
|
||||
JLabel jGoldsC;
|
||||
|
||||
@Override
|
||||
public void initComponents(int width) {
|
||||
super.initComponents(width);
|
||||
scenarios = new JComboBox();
|
||||
for (int i=1; i<=3; i++) {
|
||||
scenarios.addItem(i);
|
||||
}
|
||||
JPanel args = new JPanel();
|
||||
args.setLayout(new BoxLayout(args, BoxLayout.Y_AXIS));
|
||||
|
||||
JPanel sp = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
||||
sp.setBorder(BorderFactory.createEtchedBorder());
|
||||
sp.add(new JLabel("Scenario:"));
|
||||
sp.add(scenarios);
|
||||
|
||||
jSpeed = new JSlider();
|
||||
jSpeed.setMinimum(0);
|
||||
jSpeed.setMaximum(400);
|
||||
jSpeed.setValue(50);
|
||||
jSpeed.setPaintTicks(true);
|
||||
jSpeed.setPaintLabels(true);
|
||||
jSpeed.setMajorTickSpacing(100);
|
||||
jSpeed.setMinorTickSpacing(20);
|
||||
jSpeed.setInverted(true);
|
||||
Hashtable<Integer,Component> labelTable = new Hashtable<Integer,Component>();
|
||||
labelTable.put( 0, new JLabel("max") );
|
||||
labelTable.put( 200, new JLabel("speed") );
|
||||
labelTable.put( 400, new JLabel("min") );
|
||||
jSpeed.setLabelTable( labelTable );
|
||||
JPanel p = new JPanel(new FlowLayout());
|
||||
p.setBorder(BorderFactory.createEtchedBorder());
|
||||
p.add(jSpeed);
|
||||
|
||||
args.add(sp);
|
||||
args.add(p);
|
||||
|
||||
JPanel msg = new JPanel();
|
||||
msg.setLayout(new BoxLayout(msg, BoxLayout.Y_AXIS));
|
||||
msg.setBorder(BorderFactory.createEtchedBorder());
|
||||
|
||||
p = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
||||
p.add(new JLabel("Click on the cells to add new pieces of gold."));
|
||||
msg.add(p);
|
||||
p = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
||||
p.add(new JLabel("(mouse at:"));
|
||||
jlMouseLoc = new JLabel("0,0)");
|
||||
p.add(jlMouseLoc);
|
||||
msg.add(p);
|
||||
p = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
||||
p.add(new JLabel("Collected golds:"));
|
||||
jGoldsC = new JLabel("0");
|
||||
p.add(jGoldsC);
|
||||
msg.add(p);
|
||||
|
||||
JPanel s = new JPanel(new BorderLayout());
|
||||
s.add(BorderLayout.WEST, args);
|
||||
s.add(BorderLayout.CENTER, msg);
|
||||
getContentPane().add(BorderLayout.SOUTH, s);
|
||||
|
||||
// Events handling
|
||||
jSpeed.addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
if (env != null) {
|
||||
// env.setSleep((int)jSpeed.getValue());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
scenarios.addItemListener(new ItemListener() {
|
||||
public void itemStateChanged(ItemEvent ievt) {
|
||||
int w = (Integer) scenarios.getSelectedItem();
|
||||
if (env != null) {
|
||||
/* env.endSimulation();
|
||||
env.initWorld(w);*/
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
getCanvas().addMouseListener(new MouseListener() {
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
int col = e.getX() / cellSizeW;
|
||||
int lin = e.getY() / cellSizeH;
|
||||
if (col >= 0 && lin >= 0 && col < getModel().getWidth() && lin < getModel().getHeight()) {
|
||||
/* WorldModel wm = (WorldModel)model;
|
||||
wm.add(WorldModel.GOLD, col, lin);
|
||||
wm.setInitialNbGolds(wm.getInitialNbGolds()+1);
|
||||
update(col, lin);
|
||||
udpateCollectedGolds();*/
|
||||
}
|
||||
}
|
||||
public void mouseExited(MouseEvent e) {}
|
||||
public void mouseEntered(MouseEvent e) {}
|
||||
public void mousePressed(MouseEvent e) {}
|
||||
public void mouseReleased(MouseEvent e) {}
|
||||
});
|
||||
|
||||
getCanvas().addMouseMotionListener(new MouseMotionListener() {
|
||||
public void mouseDragged(MouseEvent e) { }
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
int col = e.getX() / cellSizeW;
|
||||
int lin = e.getY() / cellSizeH;
|
||||
if (col >= 0 && lin >= 0 && col < getModel().getWidth() && lin < getModel().getHeight()) {
|
||||
jlMouseLoc.setText(col+","+lin+")");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void udpateCollectedGolds() {
|
||||
WorldModel wm = (WorldModel)model;
|
||||
//jGoldsC.setText(wm.getGoldsInDepot() + "/" + wm.getInitialNbGolds());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics g, int x, int y, int object) {
|
||||
switch (object) {
|
||||
case WorldModel.DEPOT:
|
||||
drawDepot(g, x, y);
|
||||
break;
|
||||
case WorldModel.VICTIM:
|
||||
drawVictim(g, x, y);
|
||||
break;
|
||||
case WorldModel.ENEMY:
|
||||
drawEnemy(g, x, y);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawAgent(Graphics g, int x, int y, Color c, int id) {
|
||||
Color idColor = Color.black;
|
||||
/*if (((WorldModel)model).isCarryingGold(id)) {
|
||||
super.drawAgent(g, x, y, Color.yellow, -1);
|
||||
} else {*/
|
||||
super.drawAgent(g, x, y, c, -1);
|
||||
idColor = Color.white;
|
||||
//}
|
||||
g.setColor(idColor);
|
||||
drawString(g, x, y, defaultFont, String.valueOf(id+1));
|
||||
}
|
||||
|
||||
public void drawDepot(Graphics g, int x, int y) {
|
||||
g.setColor(Color.gray);
|
||||
g.fillRect(x * cellSizeW, y * cellSizeH, cellSizeW, cellSizeH);
|
||||
g.setColor(Color.pink);
|
||||
g.drawRect(x * cellSizeW + 2, y * cellSizeH + 2, cellSizeW - 4, cellSizeH - 4);
|
||||
g.drawLine(x * cellSizeW + 2, y * cellSizeH + 2, (x + 1) * cellSizeW - 2, (y + 1) * cellSizeH - 2);
|
||||
g.drawLine(x * cellSizeW + 2, (y + 1) * cellSizeH - 2, (x + 1) * cellSizeW - 2, y * cellSizeH + 2);
|
||||
}
|
||||
|
||||
public void drawVictim(Graphics g, int x, int y) {
|
||||
g.setColor(Color.green);
|
||||
g.drawRect(x * cellSizeW + 2, y * cellSizeH + 2, cellSizeW - 4, cellSizeH - 4);
|
||||
int[] vx = new int[4];
|
||||
int[] vy = new int[4];
|
||||
vx[0] = x * cellSizeW + (cellSizeW / 2);
|
||||
vy[0] = y * cellSizeH;
|
||||
vx[1] = (x + 1) * cellSizeW;
|
||||
vy[1] = y * cellSizeH + (cellSizeH / 2);
|
||||
vx[2] = x * cellSizeW + (cellSizeW / 2);
|
||||
vy[2] = (y + 1) * cellSizeH;
|
||||
vx[3] = x * cellSizeW;
|
||||
vy[3] = y * cellSizeH + (cellSizeH / 2);
|
||||
g.fillPolygon(vx, vy, 4);
|
||||
}
|
||||
|
||||
public void drawEnemy(Graphics g, int x, int y) {
|
||||
g.setColor(Color.red);
|
||||
g.fillOval(x * cellSizeW + 7, y * cellSizeH + 7, cellSizeW - 8, cellSizeH - 8);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
WorldEnvironment env = new WorldEnvironment();
|
||||
env.init(new String[] {"5","50","yes"});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue