`
turingfellow
  • 浏览: 136438 次
  • 性别: Icon_minigender_1
  • 来自: 福建省莆田市
社区版块
存档分类
最新评论

Block.java

    博客分类:
  • jade
阅读更多
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);
    }

}

分享到:
评论

相关推荐

    Java开发技术大全(500个源代码).

    staticBlock.java 演示静态块的使用 staticVar.java 定义静态变量 supplyTest.java 对象作为静态成员使用示例 trySwap.java 试图交换两个形参的值 useOnlyTest.java 创建多个对象,演示this的作用 ...

    EarthForge 是一个用 Java 编写的基于块的 2D 生存游戏。它建立在 UtiliGame 引擎之上。

    EarthForge 是一个用 Java 编写的基于块的 2D 生存游戏。它建立在 UtiliGame 引擎之上。 文件 EarthForge-master.zip 具有以下条目。 EarthForge Readme.txt/* w ww ....src/com/earthforge/obj/Block.java

    JAVA课程设计报告(小游戏).pdf

    - `Block.java`: - 包含一个ImageIcon对象表示方块上的图标。 - 提供了设置和获取图标的接口`setOpenStateIcon`和`getOpenStateIcon`。 - `ShowRecordDialog.java`: - 保存了成绩文件和清除按钮等成员变量。 ...

    Java记忆测试系统报告.pdf

    - “Block.java” 可能被误识别为“BlockJbubtton” - “Record.java” 被误识别为“Record4.Record” 在实际的编程实践中,需要仔细检查代码和文档,并对上述的错误进行校正。 ### 总结 这份报告所描述的记忆...

    java扫雷游戏课程设计

    设计内容主要包括多个Java源文件,如MineGame.java、MineArea.java、Block.java、BlockView.java、LayMines.java、Record.java、ShowRecord.java、voiceShow.java、userDefine.java、autherShow.java和ruleShow.java...

    java扫雷源代码

    这里我们分析给定的四个关键文件:Block.java、LayMines.java、BlockView.java 和 MineFrame.java。 1. **Block.java**: 这个文件定义了扫雷游戏中最基本的单元——`Block`类。`Block`类代表游戏中的一个格子,它...

    魔板游戏Java课程设计报告.doc

    - 游戏的实现涉及7个Java源文件,包括PuzzleGame.java、PuzzlePad.java、Point.java、Block.java、HandleImage.java、VerifySuccess.java和HandleMove.java。 - 需要利用Java提供的关键类,如JMenuItem、JButton和...

    J2SE.programming.MemoryBlock.code.rar_java programming

    总的来说,`MemoryBlock`的设计和实现涉及了Java内存管理的高级话题,对于理解和优化Java程序的内存性能具有重要意义。通过深入研究这个代码示例,开发者可以提升对Java内存机制的理解,进而编写出更高效、更稳定的...

    java课程设计-记忆测试游戏

    #### 2.3 `Block.java` - **成员变量**:`ImageIcon openStateIcon`存储方块当前显示的图标。 - **方法**:`setOpenStateIcon(ImageIcon)`设置图标,`getOpenStateIcon()`获取图标。 #### 2.4 `ShowRecordDialog....

    AES.java.zip_AES.java_aes java

    在Java中,`java.security.Key`接口和`javax.crypto.KeyGenerator`类可用于生成和管理密钥。 2. 模式选择:AES支持多种工作模式,如ECB(Electronic Codebook)、CBC(Cipher Block Chaining)、CFB(Cipher ...

    Java_game_russia-Block.zip_java 俄罗斯方块 课程设计

    本篇文章将围绕“Java_game_russia-Block.zip”这一项目,详细介绍一个基于Java实现的网络版俄罗斯方块游戏的课程设计,以及其背后涉及的重要知识点。 首先,让我们理解项目的背景。这个课程设计旨在构建一个网络...

    ReadBlock.zip_Java编程_Java_

    在Java编程领域,"ReadBlock.zip" 文件似乎与Oracle数据库中的数据块读取操作有关。Oracle数据库是一个广泛使用的关系型数据库管理系统,它以数据块为基本的存储单元。在这个项目中,我们有一个名为 "ReadBlock.java...

    java课设(20211003221929).pdf

    系统主要由7个Java源程序构成:Memory.java、MemoryTestPane.java、Block.java、ShowRecord.java、Record.java、People.java、RandomSetIcon.java。此外,还使用了Java系统提供的关键类。系统流程图展示了这些类及...

    java课程设计(扫雷游戏)(1).doc

    3. `Block.java` - 表示游戏中的一个方块,包含雷的状态和位置信息。 4. `BlockView.java` - 方块的视图,负责显示方块的外观。 5. `LayMines.java` - 雷的布局,用于随机生成雷的位置。 6. `ShowRecord.java` - ...

    2048-JAVA版

    首先,我们来看关键的三个类:Operation.java、Block.java和My2048.java。这三个文件构成了游戏的核心逻辑和界面展示。 1. Operation.java:这个类很可能负责处理用户的所有操作,如滑动事件。在2048游戏中,滑动是...

    java记忆小游戏代码

    而`Block.java`可能是表示游戏中的每个图片块的类,包含了图片信息和状态(翻开或未翻开)。 至于图片文件,如`ani5.jpg`、`car3.jpg`、`car5.jpg`等,它们是游戏中的实际图片资源,可能被用作游戏中的卡片图案,...

    魔板游戏_java课设_含可运行程序.pdf

    4. Block.java - 表示方块,存储数值或图像信息。 5. HandleImage.java - 处理图像操作,如分割和重组。 6. VerifySuccess.java - 检查游戏是否成功,即方块或图像是否已正确排序。 7. HandleMove.java - 管理方块的...

    JAVA课程设计报告(小游戏).doc

    3. **Block.java**:`Block`类扩展自`JButton`,为`MemoryTestArea`的`ArrayList<Block>`提供实例。每个`Block`对象代表游戏中的一个可点击方块,可以设置和获取其状态图标,允许游戏逻辑检查和更新方块的状态。 4....

    俄罗斯方块java源代码

    文件名可能会进一步细分为如GameBoard.java(游戏板)、Block.java(方块类)、Player.java(玩家控制)和Controller.java(事件处理器)等,每个类负责不同的功能。 详细知识点: 1. 面向对象编程:Java是一种面向...

Global site tag (gtag.js) - Google Analytics