Block.java

package jadex.examples.blocksworld;

import jadex.util.SimplePropertyChangeSupport;

import java.awt.*;
import java.beans.PropertyChangeListener;


/**
* A block in the blocks-world.
*/
public class Block
{
//-------- static part --------

/** The block counter. */
protected static int counter = 0;

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

/** The number of the block. */
protected int number;

/** The color of the block. */
protected Color color;

/** The block where this block is located on. */
protected Block lower;

/** The block located on upper of this block. */
protected Block upper;

/** The helper object for bean events. */
public SimplePropertyChangeSupport pcs;

/** The x translation for drawing (0-1). */
protected double dx;

/** The y translation for drawing (0-1). */
protected double dy;

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

/**
* Create a new block.
* @param color The color of the block.
* @param lower The block where this block is located on.
*/
public Block(Color color, Block lower)
{
this(++counter, color, lower);
}

/**
* Create a new block.
* @param number The number of the block.
* @param color The color of the block.
* @param lower The block where this block is located on.
*/
public Block(int number, Color color, Block lower)
{
this.number = number;
this.color = color;
this.pcs = new SimplePropertyChangeSupport(this);
stackOn(lower);
}

//-------- methods --------

/**
* Get the color of the block.
* @return The color of the block.
*/
public Color getColor()
{
return color;
}

/**
* Get the block where this block is located on.
* @return The block where this block is located on.
*/
public Block getLower()
{
return lower;
}

/**
* Check if this block is clear.
*/
public boolean isClear()
{
return upper==null;
}

/**
* Move this block on top of another block.
*/
public void stackOn(Block lower)
{
// Check if block can be moved.
if(!isClear())
{
throw new RuntimeException("Can only move clear blocks: "+this);
}
else if(lower==this)
{
throw new RuntimeException("Cannot move block on itself: "+this);
}

// Remove this block from old lower block.
if(this.lower!=null)
this.lower.removeBlock(this);

// Move to new block.
if(lower!=null)
{
// Check if there is space on block.
if(!lower.isClear())
{
throw new RuntimeException("Can only stack on clear blocks: "+lower);
}
lower.addBlock(this);
this.dx = Math.random();
this.dy = Math.random();
}
setLower(lower);
}

/**
* Set the lower block, where this block is located on.
* @param lower The lower block.
*/
protected void setLower(Block lower)
{
Block old = this.lower;
this.lower = lower;
this.pcs.firePropertyChange("lower", old, this.lower);
}

//-------- helper methods --------

/**
* Add a block to this block.
*/
protected void addBlock(Block block)
{
Block old = this.upper;
this.upper = block;
this.pcs.firePropertyChange("upper", old, this.upper);
}

/**
* Remove a block from this block.
*/
protected void removeBlock(Block block)
{
this.upper = null;
this.pcs.firePropertyChange("upper", block, null);
}

/**
* Create a string representation of this block.
*/
public String toString()
{
return "Block "+number;
}

/**
* Check for equality.
*/
public boolean equals(Object o)
{
return o instanceof Block
&& ((Block)o).number==number
&& ((Block)o).getColor().equals(getColor());
}

//-------- property methods --------

/**
* Add a PropertyChangeListener to the listener list.
* The listener is registered for all properties.
* @param listener The PropertyChangeListener to be added.
*/
public void addPropertyChangeListener(PropertyChangeListener listener)
{
pcs.addPropertyChangeListener(listener);
}

/**
* Remove a PropertyChangeListener from the listener list.
* This removes a PropertyChangeListener that was registered
* for all properties.
* @param listener The PropertyChangeListener to be removed.
*/
public void removePropertyChangeListener(PropertyChangeListener listener)
{
pcs.removePropertyChangeListener(listener);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值