⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 block.java

📁 进入cmd 执行java WindowGrid进入游戏界面
💻 JAVA
字号:
/******************************************************************
 *  超级俄罗斯方块-- 小方块控件				
 *版本: 1.0
 *作者:金永哲
 *最后修改日期:2005-9-18
 *
 *说明:方块为游戏中最小组织单位,且宽度与高度值相等
 *显示在WindowGrid面板中, 移动时由BlockGroup(方块组)统一管理
 *
 ******************************************************************/
 
import java.awt.*;
import javax.swing.*;

public class Block extends JPanel
{
	private int width;
	private int x, y;
	private Color color;
	private Image image = null;
	
	public Block(int w, Color color) 
	{
 
		this.x = 0;
		this.y = 0;		
		this.width = w;
		this.color = color;
		this.setSize(w, w);	
		this.setLocation(0, 0);
		this.setBackground(color);
	}
 
	//初始化位置====================================================
	public void setLocation(int x, int y)
	{
		this.x = x;
		this.y = y;
		super.setLocation(x, y);
	}
	public Point getLocation()
	{
		Point pt = new Point(x, y);
		return pt;
	}
	
	public Color getColor()
	{
		return color;
	}
 	
	public void setImage(Image image)
	{
		this.image = image;
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		if(image != null)
		{
			g.drawImage(image, 0, 0, 30, 30, null);
		}
	}
	
	//得到方格宽度, 宽度与高度的值相等============================
	public int getWidth()
	{
		return width;
	}
	
	//上移step步====================================================
	public void moveUp(int step)
	{	
		int x = this.getLocation().x;
		int y = this.getLocation().y;
		int h = this.getSize().height;
		
		this.setLocation(x, y - step * h);
	}
	
	//下移step步====================================================
	public void moveDown(int step)
	{
		moveUp(-step);
	}
	
	//左移step步====================================================
	public void moveLeft(int step)
	{
		int x = this.getLocation().x;
		int y = this.getLocation().y;	
		int w = this.getSize().width;
		this.setLocation(x - step * w, y);
	}
	
	//右移step步====================================================
	public void moveRight(int step)
	{
		moveLeft(-step);	
	}
	
	//左上移step步==================================================
	public void moveLUp(int step)
	{
		moveLeft(step);
		moveUp(step);	
	}
	
	//右上移step步==================================================
	public void moveRUp(int step)
	{
		moveLeft(-step);
		moveUp(step);
	}
	
	//左下移step步==================================================
	public void moveLDown(int step)
	{
		moveLeft(step);
		moveUp(-step);	
	}
	
	//右下移step步====================================================
	public void moveRDown(int step)
	{
		moveLeft(-step);
		moveUp(-step);
	}
	
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -