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

jadex.bdibpmn.examples.puzzle.BoardPanel Maven / Gradle / Ivy

Go to download

The Jadex BDI-BPMN applications package contain several example applications, benchmarks and testcases using BDI agents that have BPMN plans.

There is a newer version: 2.4
Show newest version
package jadex.bdibpmn.examples.puzzle;

import jadex.commons.gui.SGUI;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
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.beans.PropertyChangeEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIDefaults;

/**
 *  Display the board.
 */
public class BoardPanel extends JPanel
{
	//-------- constants --------

	/** The image icons. */
	public static UIDefaults	icons	= new UIDefaults(new Object[]
	{
		"white_piece",	SGUI.makeIcon(BoardPanel.class, "/jadex/bdibpmn/examples/puzzle/images/white_piece.png"),
		"red_piece",	SGUI.makeIcon(BoardPanel.class, "/jadex/bdibpmn/examples/puzzle/images/red_piece.png"),
		"empty_field", SGUI.makeIcon(BoardPanel.class, "/jadex/bdibpmn/examples/puzzle/images/empty_field.png")
	});

	//-------- attributes --------

	/** The board to visualize. */
	protected IBoard board;

	/** Indicates if an image rescaling is necessray. */
	protected boolean rescale;

	/** The white piece image. */
	protected Image	wp_image;

	/** The red piece image. */
	protected Image	rp_image;

	/** The empty field image. */
	protected Image	ef_image;

	/** The component to display white pieces. */
	protected JLabel white_piece;

	/** The component to display white pieces. */
	protected JLabel red_piece;

	/** The component to display white pieces. */
	protected JLabel empty_field;

	/** The listeners. */
	protected java.util.List listeners;
	
	/** The move count (to detect takebacks). */
	protected int movecnt;

	/** The last move (if any). */
	protected Move	last;

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

	/**
	 *  Create a new board panel.
	 */
	public BoardPanel(IBoard board)
	{
		this.listeners = new ArrayList();
		this.board = board;
		this.wp_image	= ((ImageIcon)icons.getIcon("white_piece")).getImage();
		this.rp_image	= ((ImageIcon)icons.getIcon("red_piece")).getImage();
		this.ef_image	= ((ImageIcon)icons.getIcon("empty_field")).getImage();
		this.white_piece	= new JLabel(new ImageIcon(wp_image), JLabel.CENTER);
		this.red_piece	= new JLabel(new ImageIcon(rp_image), JLabel.CENTER);
		this.empty_field	= new JLabel(new ImageIcon(ef_image), JLabel.CENTER);

		// Trigger rescaling of images.
		this.addComponentListener(new ComponentAdapter()
		{
			public void	componentResized(ComponentEvent ce)
			{
				rescale	= true;
			}
		});

		this.addMouseListener(new MouseAdapter()
		{
			/**
			 * Invoked when the mouse has been clicked on a component.
			 */
			public void mouseClicked(MouseEvent e)
			{
				//System.out.println("Mouse clicked: "+e.getX()+" "+e.getY());
				int size = BoardPanel.this.board.getSize();
				Rectangle r = BoardPanel.this.getBounds();
				int x = (int)(e.getX()/(r.getWidth()/(double)size));
				int y = (int)(e.getY()/(r.getHeight()/(double)size));
				int m = size/2;
				if(!(xm || x>m && y display inverse of old last move.
			else
			{
				g.setColor(Color.red);
				Move tb = new Move(last.getEnd(), last.getStart());
				drawArrow(g, tb, cellw, cellh);
				last	= board.getLastMove();
			}

			movecnt	= board.getMoves().size();
		}
	}

	/**
	 *  Draw an arrow for visulizing the move.
	 */
	protected void drawArrow(Graphics g, Move move, double cellw, double cellh)
	{
		int xs = move.getStart().getX();
		int ys = move.getStart().getY();
		int xe = move.getEnd().getX();
		int ye = move.getEnd().getY();
		int xms = (int)(xs*cellw+cellw/2);
		int yms = (int)(ys*cellh+cellh/2);
		int xme = (int)(xe*cellw+cellw/2);
		int yme = (int)(ye*cellh+cellh/2);
		//g.drawLine(xms, yms, xme, yme);
		int asize = Math.max((int)(Math.min(cellw, cellh)/8), 1);
		int thick = Math.max(asize/4, 1);
		if(xsxe)
		{
			// Arrow left.
			g.fillRect(xme+asize, yms-thick/2, xms-xme-asize, thick);
			g.fillPolygon(new int[]{xme+asize, xme+asize, xme}, new int[]{yme-asize, yme+asize, yme}, 3);
		}
		else if(ys




© 2015 - 2024 Weber Informatics LLC | Privacy Policy