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

jadex.bdi.examples.blackjack.dealer.DealerFrame Maven / Gradle / Ivy

Go to download

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

There is a newer version: 2.4
Show newest version
package jadex.bdi.examples.blackjack.dealer;

import jadex.bdi.examples.blackjack.Dealer;
import jadex.bdi.examples.blackjack.GameState;
import jadex.bdi.examples.blackjack.gui.GUIImageLoader;
import jadex.bdi.examples.blackjack.gui.GameStateFrame;
import jadex.bdi.runtime.AgentEvent;
import jadex.bdi.runtime.IAgentListener;
import jadex.bdi.runtime.IBDIExternalAccess;
import jadex.commons.SGUI;
import jadex.commons.concurrent.SwingDefaultResultListener;

import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.swing.SwingUtilities;

/**
 * This GUI gives an overview of the ongoings during the game.
 * It shows different panels representing the 'internal states'
 * (the cards, accounts and bets) of the dealer- and all the
 * player-agents as well as one special panel containing a
 * progressBar and a few buttons (the 'OptionPanel').
 * But this class is not just a viewing-component, but also an
 * agent-plan and therefor it contains some jadex-specific code,
 * which is perhaps worth looking at.
 */
public class DealerFrame extends GameStateFrame
{
	//-------- attributes --------

	/** The agent access object. */
	protected IBDIExternalAccess	agent;
	
	/** child windows (e.g. statistics). */
	protected Set	children;

	//-------- constructors --------
	
	/**
	 * Creates a new instance of the dealer frame.
	 * Here, the GUI is build up for the first time, all
	 * panels are instantiated and shown on the screen.
	 */
	public DealerFrame(final Dealer me, final IBDIExternalAccess agent)
	{
		super(null, null);
		this.agent	= agent;
		this.children	= new HashSet();

		// Show Jadex version information.
		String	title	= "Blackjack Dealer";
			//+ Configuration.getConfiguration().getReleaseNumber()
			//+ " (" + Configuration.getConfiguration().getReleaseDate() + ")";

		//Create the 'Main'-Window and the contentPane
		setTitle(title);
		//setResizable(false);
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				DealerFrame.this.agent.killAgent();
			}
		});
		agent.addAgentListener(new IAgentListener()
		{
			public void agentTerminating(AgentEvent ae)
			{
				SwingUtilities.invokeLater(new Runnable()
				{
					public void run()
					{
						DealerFrame.this.dispose();
					}
				});
			}
			public void agentTerminated(AgentEvent ae)
			{
			}
		});

		// set the icon to be displayed for the frame
		setIconImage(GUIImageLoader.getImage("heart_small_d").getImage());

		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				agent.getBeliefbase().getBeliefFact("gamestate").addResultListener(new SwingDefaultResultListener(DealerFrame.this)
				{
					public void customResultAvailable(Object source, Object result)
					{
						DealerFrame.this.setGameState((GameState)result);
					}
				});
			}
		});
		setControlPanel(new DealerOptionPanel(agent, DealerFrame.this));

		/*getContentPane().setLayout(new GridLayout(0, 2));

		// initialise the JPanels for the Dealer and the progressBar
		PlayerPanel	dealerPanel = new PlayerPanel(me);

		// add these JPanels as the first-row to the contentPane
		getContentPane().add(dealerPanel);
		getContentPane().add(new DealerOptionPanel(agent, DealerFrame.this));*/

		// display the gui on the screen
		pack();
		setLocation(SGUI.calculateMiddlePosition(DealerFrame.this));
		setVisible(true);
	}

	/**
	 *  Add a child window to be disposed on exit.
	 */
	public void	addChildWindow(Window child)
	{
		children.add(child);
	}

	/**
	 *  Remove a child window to be disposed on exit.
	 */
	public void	removeChildWindow(Window child)
	{
		children.remove(child);
	}

	/**
	 *  Dispose this window and child windows.
	 */
	public void	dispose()
	{
		super.dispose();

		for(Iterator i=children.iterator(); i.hasNext(); )
			((Window)i.next()).dispose();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy