jadex.quickstart.cleanerworld.gui.EnvironmentGui Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-applications-quickstart Show documentation
Show all versions of jadex-applications-quickstart Show documentation
The Jadex quickstart package contains the ready to use "Cleanerworld" environment as a testbed for experimenting with autonomous agents and intelligent behavior.
package jadex.quickstart.cleanerworld.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.UIDefaults;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import jadex.bridge.ComponentIdentifier;
import jadex.commons.gui.SGUI;
import jadex.quickstart.cleanerworld.environment.ILocation;
import jadex.quickstart.cleanerworld.environment.IPheromone;
import jadex.quickstart.cleanerworld.environment.impl.Chargingstation;
import jadex.quickstart.cleanerworld.environment.impl.Cleaner;
import jadex.quickstart.cleanerworld.environment.impl.Environment;
import jadex.quickstart.cleanerworld.environment.impl.Location;
import jadex.quickstart.cleanerworld.environment.impl.Waste;
import jadex.quickstart.cleanerworld.environment.impl.Wastebin;
/**
* The gui for the cleaner world example.
* Shows the world from the viewpoint of the environment agent.
*/
public class EnvironmentGui extends JFrame
{
//-------- constants --------
// auto generated by eclipse
private static final long serialVersionUID = -1451077178653064172L;
/** The image icons. */
private static UIDefaults icons = new UIDefaults(new Object[]
{
"waste", SGUI.makeIcon(EnvironmentGui.class, "/jadex/quickstart/cleanerworld/gui/images/waste.png"),
"wastebin", SGUI.makeIcon(EnvironmentGui.class, "/jadex/quickstart/cleanerworld/gui/images/wastebin.png"),
"wastebin_full", SGUI.makeIcon(EnvironmentGui.class, "/jadex/quickstart/cleanerworld/gui/images/wastebin_full.png"),
"chargingstation", SGUI.makeIcon(EnvironmentGui.class, "/jadex/quickstart/cleanerworld/gui/images/chargingstation.png"),
"cleaner", SGUI.makeIcon(EnvironmentGui.class, "/jadex/quickstart/cleanerworld/gui/images/cleaner.png"),
"background", SGUI.makeIcon(EnvironmentGui.class, "/jadex/quickstart/cleanerworld/gui/images/background.png"),
"background_night", SGUI.makeIcon(EnvironmentGui.class, "/jadex/quickstart/cleanerworld/gui/images/background_night.png")
});
//-------- attributes --------
/** The repaint timer. */
private Timer timer;
//-------- constructors --------
/**
* Create a new gui.
*/
public EnvironmentGui()
{
super("Welcome to Cleaner World");
final Environment env = Environment.getInstance();
// Option panel.
JPanel options = new JPanel(new GridBagLayout());
options.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Environment Control"));
final JCheckBox daytime = new JCheckBox("", env.getDaytime());
daytime.setHorizontalTextPosition(SwingConstants.LEFT);
final JLabel wastecnt = new JLabel(""+env.getWastes().length);
final JComboBox wastebinchoice = new JComboBox();
Wastebin[] wastebins = env.getWastebins();
for(int i=0; i fillstate = new JComboBox(new String[]{"empty", "full"});
JButton setfillstate = new JButton("Set fill-state");
Insets insets = new Insets(2, 4, 4, 2);
options.add(new JLabel("Toggle daytime"), new GridBagConstraints(0, 0, 1, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0 , 0));
options.add(daytime, new GridBagConstraints(1, 0, 3, 1, 1, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0 , 0));
options.add(new JLabel("Waste count:"), new GridBagConstraints(0, 1, 1, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0 , 0));
options.add(wastecnt, new GridBagConstraints(1, 1, 3, 1, 1, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0 , 0));
options.add(new JLabel("Wastebin:"), new GridBagConstraints(0, 2, 1, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0 , 0));
options.add(wastebinchoice, new GridBagConstraints(1, 2, 1, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0 , 0));
options.add(fillstate, new GridBagConstraints(2, 2, 1, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0 , 0));
options.add(setfillstate, new GridBagConstraints(3, 2, 1, 1, 1, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0 , 0));
final Image waste_image = ((ImageIcon)icons.getIcon("waste")).getImage();
final Image wastebin_image = ((ImageIcon)icons.getIcon("wastebin")).getImage();
final Image wastebin_full_image = ((ImageIcon)icons.getIcon("wastebin_full")).getImage();
final Image chargingstation_image = ((ImageIcon)icons.getIcon("chargingstation")).getImage();
final Image cleaner_image = ((ImageIcon)icons.getIcon("cleaner")).getImage();
final Image background_image = ((ImageIcon)icons.getIcon("background")).getImage();
final Image background_night_image = ((ImageIcon)icons.getIcon("background_night")).getImage();
final JLabel waste = new JLabel(new ImageIcon(waste_image), JLabel.CENTER);
final JLabel wastebin = new JLabel("dummy", new ImageIcon(wastebin_image), JLabel.CENTER);
wastebin.setVerticalTextPosition(JLabel.BOTTOM);
wastebin.setHorizontalTextPosition(JLabel.CENTER);
final JLabel wastebin_full = new JLabel("dummy", new ImageIcon(wastebin_full_image), JLabel.CENTER);
wastebin_full.setVerticalTextPosition(JLabel.BOTTOM);
wastebin_full.setHorizontalTextPosition(JLabel.CENTER);
final JLabel chargingstation = new JLabel("dummy", new ImageIcon(chargingstation_image), JLabel.CENTER);
chargingstation.setVerticalTextPosition(JLabel.BOTTOM);
chargingstation.setHorizontalTextPosition(JLabel.CENTER);
final JLabel cleaner = new JLabel("dummy", new ImageIcon(cleaner_image), JLabel.CENTER);
cleaner.setVerticalTextPosition(JLabel.CENTER);
cleaner.setHorizontalTextPosition(JLabel.RIGHT);
// Map panel for painting the cleaner world view
final JPanel map = new JPanel()
{
// generated by eclipse
private static final long serialVersionUID = 6637115521263586060L;
// overridden paint method.
protected void paintComponent(Graphics g)
{
// Get world state from environment object.
boolean daytime = env.getDaytime();
// Paint background (dependent on daytime).
Rectangle bounds = getBounds();
//g.setColor(daytime ? Color.lightGray : Color.darkGray);
//g.fillRect(0, 0, bounds.width, bounds.height);
Image image = daytime ? background_image : background_night_image;
int w = image.getWidth(this);
int h = image.getHeight(this);
if(w>0 && h>0)
{
for(int y=0; y"
+ cleaners[i].getAgentIdentifier().getLocalName()+"
"
+ "battery: " + (int)(cleaners[i].getChargestate()*100.0) + "%
"
+ "waste: " + (cleaners[i].getCarriedWaste()!=null ? "yes" : "no")+"