jadex.bdi.examples.blocksworld.BlocksworldPanel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-applications-bdi Show documentation
Show all versions of jadex-applications-bdi Show documentation
The Jadex BDI applications package contain
several example applications, benchmarks and
testcases using BDI agents.
package jadex.bdi.examples.blocksworld;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Map;
import java.util.WeakHashMap;
import javax.swing.JPanel;
/**
* Shows the blocksworld.
*/
public class BlocksworldPanel extends JPanel
{
//-------- constants --------
/** The block placement x variance as fraction of the total available space (0-1). */
protected static final double XVARIANCE = 0.2;
/** The block placement variance as fraction of the total available space (0-XVARIANCE). */
protected static final double YVARIANCE = 0.04;
//-------- attributes --------
/** The table. */
protected Table table;
/** The change listener to update the gui. */
protected PropertyChangeListener pcl;
/** The known blocks. */
protected Map blocks;
/** The block size (in pixels). */
protected int blocksize;
/** The imaginary flag. */
protected boolean imaginary;
//-------- constructors --------
/**
* Create a blocksworld panel.
* @param table The table.
* @param imaginary Flag indicating that its not the real world.
*/
public BlocksworldPanel(Table table, boolean imaginary)
{
this.table = table;
this.imaginary = imaginary;
this.blocksize = 100;
this.blocks = new WeakHashMap();
// Update gui when table changes.
this.pcl = new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent pce)
{
// Update gui.
BlocksworldPanel.this.invalidate();
BlocksworldPanel.this.repaint();
// Add listener for new blocks.
BlocksworldPanel.this.observeNewBlocks();
}
};
table.addPropertyChangeListener(pcl);
// Add listener to blocks.
observeNewBlocks();
}
//-------- methods --------
/**
* Set the size of the blocks.
*/
public void setBlockSize(int blocksize)
{
this.blocksize = blocksize;
this.revalidate();
this.repaint();
}
/**
* Get the size of the blocks.
*/
public int getBlockSize()
{
return this.blocksize;
}
/**
* Get the preferred size of the panel.
*/
public Dimension getPreferredSize()
{
Dimension grid = getGridDimension();
Insets insets = getInsets();
return new Dimension(
(int)Math.ceil(grid.width*blocksize*(1+XVARIANCE))
+ insets.left + insets.right + 2*blocksize/5,
grid.height*blocksize + insets.top + insets.bottom + blocksize/2);
}
/**
* Determine grid dimension (numx, numy).
*/
public Dimension getGridDimension()
{
Dimension dim = new Dimension(table.blocks.size(), 0);
Block[] baseblocks = (Block[])table.blocks.toArray(new Block[dim.width]);
for(int x=0; x