All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.quickstart.cleanerworld.gui.EnvironmentGui Maven / Gradle / Ivy

Go to download

The Jadex quickstart package contains the ready to use "Cleanerworld" environment as a testbed for experimenting with autonomous agents and intelligent behavior.

There is a newer version: 4.0.267
Show newest version
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")+""); cleaner.setForeground(daytime ? Color.black : Color.white); render(g, cleaner, new Point(p.x+45, p.y)); // Hack!!! } } }; // Set widget sizes for clean layout wastebinchoice.setPreferredSize(new Dimension((int)wastebinchoice.getPreferredSize().getWidth()+10, (int)wastebinchoice.getPreferredSize().getHeight())); fillstate.setPreferredSize(new Dimension((int)fillstate.getPreferredSize().getWidth()+10, (int)fillstate.getPreferredSize().getHeight())); // Add listeners for setting widgets daytime.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { env.setDaytime(daytime.isSelected()); } }); setfillstate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Wastebin wb = env.getWastebin((String)wastebinchoice.getSelectedItem()); if(fillstate.getSelectedItem().equals("empty")) wb.empty(); if(fillstate.getSelectedItem().equals("full")) wb.fill(); } }); // Handle scaling of images whenever window is resized. map.addComponentListener(new ComponentAdapter() { private Rectangle _bounds; public void componentResized(ComponentEvent ce) { Rectangle bounds = map.getBounds(); if(_bounds==null) _bounds = bounds; double scale = Math.min(bounds.width/(double)_bounds.width, bounds.height/(double)_bounds.height); // Wastes ((ImageIcon)waste.getIcon()).setImage( waste_image.getScaledInstance( (int)(waste_image.getWidth(map)*scale), (int)(waste_image.getHeight(map)*scale), Image.SCALE_DEFAULT)); // Wastebin ((ImageIcon)wastebin.getIcon()).setImage( wastebin_image.getScaledInstance( (int)(wastebin_image.getWidth(map)*scale), (int)(wastebin_image.getHeight(map)*scale), Image.SCALE_DEFAULT)); // Full Wastebin ((ImageIcon)wastebin_full.getIcon()).setImage( wastebin_full_image.getScaledInstance( (int)(wastebin_full_image.getWidth(map)*scale), (int)(wastebin_full_image.getHeight(map)*scale), Image.SCALE_DEFAULT)); // Chargingstation ((ImageIcon)chargingstation.getIcon()).setImage( chargingstation_image.getScaledInstance( (int)(chargingstation_image.getWidth(map)*scale), (int)(chargingstation_image.getHeight(map)*scale), Image.SCALE_DEFAULT)); // Cleaner ((ImageIcon)cleaner.getIcon()).setImage( cleaner_image.getScaledInstance( (int)(cleaner_image.getWidth(map)*scale), (int)(cleaner_image.getHeight(map)*scale), Image.SCALE_DEFAULT)); } }); // Allow placing waste with mouse map.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { Point p = me.getPoint(); Rectangle bounds = map.getBounds(); final Location mouseloc = new Location((double)p.x/(double)bounds.width, 1.0-(double)p.y/(double)bounds.height); final double tol = 7/(double)bounds.height; final Environment env = Environment.getInstance(); Waste[] wastes = env.getWastes(); Waste nearest = null; double dist = 0; for(int i=0; i wbsadd = new LinkedHashSet<>(); // Set wbsremove = new LinkedHashSet<>(); // DefaultComboBoxModel wbsmodel = (DefaultComboBoxModel)wastebinchoice.getModel(); // for(Wastebin wb: env.getWastebins()) // { // } // // for(int i=wbsmodel.getSize()-1; i>=0; i--) // { // if(!nwbs.contains(wbsmodel.getElementAt(i))) // { // wbsmodel.r // } // } // // // } // else if("wastebins".equals(propertyname)) // { // if(ce.getNewValue()==null) // { // String name = ((Wastebin)ce.getOldValue()).getId(); // .removeElement(name); // } // else // { // String name = ((Wastebin)ce.getNewValue()).getId(); // int size = wastebinchoice.getModel().getSize(); // boolean found = false; // for(int i=0; i)wastebinchoice.getModel()).addElement(name); // } // } } }); timer.start(); // Exit program on window close. addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } //-------- helper methods -------- /** * Get the on screen location for a location in the world. */ private Point onScreenLocation(ILocation loc, Rectangle bounds) { return new Point((int)(bounds.width*loc.getX()), (int)(bounds.height*(1.0-loc.getY()))); } /** * Render a component on screen. * @param g The graphics object. * @param comp The component. * @param p The on screen location. */ private void render(Graphics g, Component comp, Point p) { Dimension d = comp.getPreferredSize(); Rectangle bounds = new Rectangle(p.x - d.width/2, p.y - d.height/2, d.width+1, d.height); comp.setBounds(bounds); g.translate(bounds.x, bounds.y); comp.paint(g); g.translate(-bounds.x, -bounds.y); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy