207 lines
6.7 KiB
Java
207 lines
6.7 KiB
Java
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;
|
|
JSlider jSpeed;
|
|
JLabel jGoldsC;
|
|
|
|
@Override
|
|
public void initComponents(int width) {
|
|
super.initComponents(width);
|
|
|
|
JPanel args = new JPanel();
|
|
args.setLayout(new BoxLayout(args, BoxLayout.Y_AXIS));
|
|
|
|
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(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 victims"));
|
|
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("Rescued victims:"));
|
|
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());
|
|
}
|
|
}
|
|
});
|
|
|
|
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(id <4){
|
|
idColor = Color.RED;
|
|
} else if (id < 6) {
|
|
idColor = Color.MAGENTA;
|
|
}
|
|
super.drawAgent(g, x, y, idColor, -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"});
|
|
}
|
|
|
|
@Override
|
|
public void drawEmpty(Graphics g, int x, int y) {
|
|
g.setColor(Color.blue);
|
|
g.fillRect(x * cellSizeW + 1, y * cellSizeH + 1, cellSizeW - 1, cellSizeH - 1);
|
|
g.setColor(Color.cyan);
|
|
g.drawRect(x * cellSizeW, y * cellSizeH, cellSizeW, cellSizeH);
|
|
}
|
|
}
|