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

📄 graphpaperlayout.java

📁 java开发的华容道游戏的源码
💻 JAVA
字号:
package com.abc.hrd;

import java.awt.*;
import java.util.*;

public class GraphPaperLayout implements LayoutManager2 {

	int hgap;
	int vgap;

	Dimension gridSize;

	Hashtable compTable;

	public GraphPaperLayout() {
		this(new Dimension(1, 1));
	}

	public GraphPaperLayout(Dimension dimension) {
		this(dimension, 0, 0);
	}

	public GraphPaperLayout(Dimension dimension, int i, int j) {
		if (dimension.width <= 0 || dimension.height <= 0) {
			throw new IllegalArgumentException(
					"dimensions must be greater than zero");
		} else {
			gridSize = new Dimension(dimension);
			hgap = i;
			vgap = j;
			compTable = new Hashtable();
			return;
		}
	}

	public Dimension getGridSize() {
		return new Dimension(gridSize);
	}

	public void setGridSize(Dimension dimension) {
		setGridSize(dimension.width, dimension.height);
	}

	public void setGridSize(int i, int j) {
		gridSize = new Dimension(i, j);
	}

	public void setConstraints(Component component, Rectangle rectangle) {
		compTable.put(component, new Rectangle(rectangle));
	}

	public void addLayoutComponent(String s, Component component) {
	}

	public void removeLayoutComponent(Component component) {
		compTable.remove(component);
	}

	public Dimension preferredLayoutSize(Container container) {
		return getLayoutSize(container, true);
	}

	public Dimension minimumLayoutSize(Container container) {
		return getLayoutSize(container, false);
	}

	protected Dimension getLayoutSize(Container container, boolean flag) {
		Dimension dimension = getLargestCellSize(container, flag);
		Insets insets = container.getInsets();
		dimension.width = dimension.width * gridSize.width + hgap
				* (gridSize.width + 1) + insets.left + insets.right;
		dimension.height = dimension.height * gridSize.height + vgap
				* (gridSize.height + 1) + insets.top + insets.bottom;
		return dimension;
	}

	protected Dimension getLargestCellSize(Container container, boolean flag) {
		int i = container.getComponentCount();
		Dimension dimension = new Dimension(0, 0);
		for (int j = 0; j < i; j++) {
			Component component = container.getComponent(j);
			Rectangle rectangle = (Rectangle) compTable.get(component);
			if (component != null && rectangle != null) {
				Dimension dimension1;
				if (flag)
					dimension1 = component.getPreferredSize();
				else
					dimension1 = component.getMinimumSize();
				dimension.width = Math.max(dimension.width, dimension1.width
						/ rectangle.width);
				dimension.height = Math.max(dimension.height, dimension1.height
						/ rectangle.height);
			}
		}

		return dimension;
	}

	public void layoutContainer(Container container) {
		synchronized (container.getTreeLock()) {
			Insets insets = container.getInsets();
			int i = container.getComponentCount();
			if (i == 0)
				return;
			Dimension dimension = container.getSize();
			int j = dimension.width - (insets.left + insets.right);
			int k = dimension.height - (insets.top + insets.bottom);
			int l = j / gridSize.width;
			int i1 = k / gridSize.height;
			int j1 = (j - (gridSize.width + 1) * hgap) / gridSize.width;
			int k1 = (k - (gridSize.height + 1) * vgap) / gridSize.height;
			for (int l1 = 0; l1 < i; l1++) {
				Component component = container.getComponent(l1);
				Rectangle rectangle = (Rectangle) compTable.get(component);
				if (rectangle != null) {
					int i2 = insets.left + l * rectangle.x + hgap;
					int j2 = insets.top + i1 * rectangle.y + vgap;
					int k2 = j1 * rectangle.width - hgap;
					int l2 = k1 * rectangle.height - vgap;
					component.setBounds(i2, j2, k2, l2);
				}
			}

		}
	}

	public void addLayoutComponent(Component component, Object obj) {
		if (obj instanceof Rectangle) {
			Rectangle rectangle = (Rectangle) obj;
			if (rectangle.width <= 0 || rectangle.height <= 0)
				throw new IllegalArgumentException(
						"cannot add to layout: rectangle must have positive width and height");
			if (rectangle.x < 0 || rectangle.y < 0)
				throw new IllegalArgumentException(
						"cannot add to layout: rectangle x and y must be >= 0");
			setConstraints(component, rectangle);
		} else if (obj != null)
			throw new IllegalArgumentException(
					"cannot add to layout: constraint must be a Rectangle");
	}

	public Dimension maximumLayoutSize(Container container) {
		return new Dimension(0x7fffffff, 0x7fffffff);
	}

	public float getLayoutAlignmentX(Container container) {
		return 0.5F;
	}

	public float getLayoutAlignmentY(Container container) {
		return 0.5F;
	}

	public void invalidateLayout(Container container) {
	}
}

⌨️ 快捷键说明

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