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

jadex.bdi.examples.cleanerworld_classic.environment.EnvironmentGui Maven / Gradle / Ivy

Go to download

The Jadex BDI applications package contain several example applications, benchmarks and testcases using BDI agents.

The newest version!
package jadex.bdi.examples.cleanerworld_classic.environment;

import jadex.bdi.examples.cleanerworld_classic.Chargingstation;
import jadex.bdi.examples.cleanerworld_classic.Cleaner;
import jadex.bdi.examples.cleanerworld_classic.Environment;
import jadex.bdi.examples.cleanerworld_classic.Location;
import jadex.bdi.examples.cleanerworld_classic.Waste;
import jadex.bdi.examples.cleanerworld_classic.Wastebin;
import jadex.bdi.runtime.IBDIExternalAccess;
import jadex.bdi.runtime.IBDIInternalAccess;
import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.service.types.monitoring.IMonitoringEvent;
import jadex.bridge.service.types.monitoring.IMonitoringService.PublishEventLevel;
import jadex.commons.beans.PropertyChangeEvent;
import jadex.commons.beans.PropertyChangeListener;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.commons.future.IntermediateDefaultResultListener;
import jadex.commons.gui.SGUI;
import jadex.commons.gui.future.SwingIntermediateResultListener;
import jadex.commons.transformation.annotations.Classname;

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.DefaultComboBoxModel;
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.SwingUtilities;
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;


/**
 *  The gui for the cleaner world example.
 *  Shows the world from the viewpoint of the environment agent.
 */
public class EnvironmentGui	extends JFrame
{
	//-------- constants --------

	/** The image icons. */
	private static UIDefaults	icons	= new UIDefaults(new Object[]
	{
		"waste",	SGUI.makeIcon(EnvironmentGui.class, "/jadex/bdi/examples/cleanerworld_classic/images/waste.png"),
		"wastebin",	SGUI.makeIcon(EnvironmentGui.class, "/jadex/bdi/examples/cleanerworld_classic/images/wastebin.png"),
		"wastebin_full", SGUI.makeIcon(EnvironmentGui.class, "/jadex/bdi/examples/cleanerworld_classic/images/wastebin_full.png"),
		"chargingstation", SGUI.makeIcon(EnvironmentGui.class, "/jadex/bdi/examples/cleanerworld_classic/images/chargingstation.png"),
		"cleaner", SGUI.makeIcon(EnvironmentGui.class, "/jadex/bdi/examples/cleanerworld_classic/images/cleaner.png"),
		"background", SGUI.makeIcon(EnvironmentGui.class, "/jadex/bdi/examples/cleanerworld_classic/images/background.png"),
		"background_night", SGUI.makeIcon(EnvironmentGui.class, "/jadex/bdi/examples/cleanerworld_classic/images/background_night.png")
	});
	
	//-------- attributes --------
	
	/** The repaint timer. */
	protected Timer	timer;

	//-------- constructors --------

	/**
	 *  Create a new gui plan.
	 */
	public EnvironmentGui(final IBDIExternalAccess agent)
	{
		super(agent.getComponentIdentifier().getLocalName());

		agent.scheduleStep(new IComponentStep()
		{
			@Classname("disp")
			public IFuture execute(IInternalAccess ia)
			{
				IBDIInternalAccess bia = (IBDIInternalAccess)ia;
				
//				bia.addComponentListener(new TerminationAdapter()
//				{
//					public void componentTerminated()
//					{
//						SwingUtilities.invokeLater(new Runnable()
//						{
//							public void run()
//							{
//								if(timer!=null)
//									timer.stop();
//								EnvironmentGui.this.dispose();
//							}
//						});
//					}
//				});
				
				bia.subscribeToEvents(IMonitoringEvent.TERMINATION_FILTER, false, PublishEventLevel.COARSE)
					.addResultListener(new SwingIntermediateResultListener(new IntermediateDefaultResultListener()
				{
					public void intermediateResultAvailable(IMonitoringEvent result)
					{
						if(timer!=null)
							timer.stop();
						EnvironmentGui.this.dispose();
					}
				}));
				
				final Environment env = (Environment)bia.getBeliefbase().getBelief("environment").getFact();
				SwingUtilities.invokeLater(new Runnable()
				{
					public void run()
					{
						//System.out.println("Now accessing env, GUIPlan: "+env.hashCode());

						// 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("0");

						final JComboBox wastebinchoice = new JComboBox();
						Wastebin[] wastebins = env.getWastebins();
						for(int i=0; i0 && h>0)
								{
									for(int y=0; y"
										+ cleaners[i].getName()+"
" + "battery: " + (int)(cleaners[i].getChargestate()*100.0) + "%
" + "waste: " + (cleaners[i].getCarriedWaste()!=null ? "yes" : "no")+""); cleaner.setForeground(daytime ? new Color(age*2,age*2,age*2) : new Color(255-age*2,255-age*2,255-age*2)); render(g, cleaner, new Point(p.x+45, p.y)); // Hack!!! } } }; // Set sizes. 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 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. map.addComponentListener(new ComponentAdapter() { protected 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)); } }); map.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { // Problem!!! Beliefbase is changed in // the gui thread. 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; agent.scheduleStep(new IComponentStep() { @Classname("mouse") public IFuture execute(IInternalAccess ia) { IBDIInternalAccess bia = (IBDIInternalAccess)ia; Environment env = (Environment)bia.getBeliefbase().getBelief("environment").getFact(); Waste[] wastes = env.getWastes(); Waste nearest = null; double dist = 0; for(int i=0; i0) // { // cs.deleteContext(contexts[0], null); // } // } // else // { // System.out.println("No context service found: Killing agents manually."); // // Todo: move to end goal. // Environment en = (Environment)agent.getBeliefbase().getBelief("environment").getFact(); // Cleaner[] cleaners = en.getCleaners(); // for(int i=0; i() { public void resultAvailable(Void result) { } public void exceptionOccurred(Exception exception) { dispose(); } }); } //-------- helper methods -------- /** * Get the on screen location for a location in the world. */ protected Point onScreenLocation(Location 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. */ protected 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 - 2025 Weber Informatics LLC | Privacy Policy