CodeForge QQ客服 CodeForge 400电话 客服电话 4006316121
首页 » 源代码 » 一块拼图 » MainDialog.java

MainDialog.java ( 文件浏览 )

文件源自:一块拼图
  • 发布于2016-05-18
  • 浏览次数:0
  • 下载次数:0
  • 下载需 1 积分
  • 侵权举报
			package com.tamal.tiles.ui;

import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.tamal.tiles.IMonitor;
import com.tamal.tiles.Piece;
import com.tamal.tiles.Puzzle;

public class MainDialog extends Dialog implements IMonitor {


	private static final int MAX_TILE = 20;

	public static final Color[] COLORS = new Color[MAX_TILE + 1];

	static {

		Device device = Display.getCurrent();
		COLORS[0] = new Color(device, 255, 255, 255);
		COLORS[1] = new Color(device, 255, 255, 0);
		COLORS[2] = new Color(device, 153, 76, 0);
		COLORS[3] = new Color(device, 127, 0, 204);
		COLORS[4] = new Color(device, 255, 0, 127);
		COLORS[5] = new Color(device, 0, 127, 0);
		COLORS[6] = new Color(device, 255, 127, 0);
		COLORS[7] = new Color(device, 204, 204, 230);
		COLORS[8] = new Color(device, 204, 204, 0);
		COLORS[9] = new Color(device, 255, 25, 25);
		COLORS[10] = new Color(device, 102, 0, 0);
		COLORS[11] = new Color(device, 0, 255, 0);
		COLORS[12] = new Color(device, 102, 127, 230);
		COLORS[13] = new Color(device, 255, 89, 127);
		COLORS[14] = new Color(device, 76, 76, 255);
		COLORS[15] = new Color(device, 25, 0, 127);
		COLORS[16] = new Color(device, 255, 166, 179);
		COLORS[17] = new Color(device, 76, 255, 76);
		COLORS[18] = new Color(device, 76, 76, 255);
		COLORS[19] = new Color(device, 255, 127, 51);
		COLORS[20] = new Color(device, 0, 0, 0);
	
}

	private Puzzle puzzle = new Puzzle();
	private Shell shell;
	private Display display;

	public MainDialog(Shell parent) {

		super(parent);
		this.shell = parent;
		this.display = parent.getDisplay();
		createDialogArea(parent);

		puzzle.addMonitor(this);
		// puzzle.addMonitor(new IMonitor() {

		//
		// @Override
		// public void update(int event) {

		// switch (event) {

		// case IMonitor.NEW_SOL:
		// puzzle.printLastSolution();
		// break;
		// default:
		// break;
		// 
}
		// 
}
		// 
});
	
}

	protected void createDialogArea(Composite parent) {

		Composite diagContainer = new Composite(parent, SWT.NULL);

		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		diagContainer.setLayout(layout);

		createNewPuzzleArea(diagContainer);
		createConfigArea(diagContainer);
		createPieceBar(diagContainer);
	
}

	private void createNewPuzzleArea(Composite container) {

		final Group newPuzzleGroup = new Group(container, SWT.NONE);
		newPuzzleGroup.setText("New Puzzle");
		newPuzzleGroup.setLayoutData(new GridData(GridData.FILL,
				GridData.CENTER, true, false));

		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		newPuzzleGroup.setLayout(layout);

		new Label(newPuzzleGroup, SWT.NULL).setText("Puzzle file:");
		final Text pathText = new Text(newPuzzleGroup, SWT.BORDER | SWT.SINGLE
				| SWT.READ_ONLY);
		GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false);
		pathText.setLayoutData(gd);

		Button browseButton = new Button(newPuzzleGroup, SWT.NULL);
		browseButton.setText("...");
		gd = new GridData(GridData.BEGINNING, GridData.CENTER, false, false);
		gd.widthHint = 30;
		browseButton.setLayoutData(gd);
		browseButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				FileDialog dialog = new FileDialog(shell, SWT.NULL);
				String path = dialog.open();
				if (path != null) {

					try {

						pathText.setText(path);
						puzzle.parse(path);

						solCount = 0;
						curSol = -1;
						pieceComposite = null;
						prevButton = null;
						solLabel = null;
						nextButton = null;
						solContainer = null;
						pieceContainer = null;

						for (int i = 0; i < 3; i++) {

							if (pieceItem[i] != null) {

								pieceItemContainer[i].dispose();
								pieceItemContainer[i] = null;
								pieceItem[i].dispose();
								pieceItem[i] = null;
							
}
						
}
						createSolutionItem();
						createBoardItem();
						createTileItem();
						pieceItem[0].setExpanded(true);
						pieceItem[1].setExpanded(true);
						pieceItem[2].setExpanded(false);
					
} catch (IOException ex) {

						ex.printStackTrace();
					
}
				
}
			
}
		
});
	
}

	private Button chkSingleSol;
	private Button chkRotate;
	private Button chkReflection;
	private Button solveButton;

	private void createConfigArea(Composite container) {

		Group configGroup = new Group(container, SWT.NONE);
		configGroup.setText("Solver Configuration");
		configGroup.setLayoutData(new GridData(GridData.FILL, GridData.CENTER,
				true, false));

		GridLayout layout = new GridLayout();
		layout.numColumns = 4;
		configGroup.setLayout(layout);

		chkSingleSol = new Button(configGroup, SWT.CHECK);
		chkSingleSol.setText("All Solutions");

		chkRotate = new Button(configGroup, SWT.CHECK);
		chkRotate.setText("Use Rotation");
		// chkRotate.setSelection(true);

		chkReflection = new Button(configGroup, SWT.CHECK);
		chkReflection.setText("Use Reflection");
		GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false);
		chkReflection.setLayoutData(gd);
		// chkReflection.setSelection(true);

		solveButton = new Button(configGroup, SWT.NULL);
		solveButton.setText("Solve Puzzle");

		solveButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				if (puzzle.getBoard() == null)
					return;
				final boolean singleSolution = !chkSingleSol.getSelection();
				final boolean useRotation = chkRotate.getSelection();
				final boolean useReflection = chkReflection.getSelection();

				solCount = 0;
				curSol = -1;

				pieceItem[1].setExpanded(false);
				pieceItem[2].setExpanded(true);
				pieceItem[2].setText("Solutions");
				if (pieceComposite != null) {

					pieceComposite.dispose();
					pieceComposite = null;
				
}

				Thread solverThread = new Thread() {

					@Override
					public void run() {

						puzzle
								.solve(singleSolution, useRotation,
										useReflection);

						display.syncExec(new Runnable() {

							@Override
							public void run() {

								pieceItem[2]
										.setText(String
												.format(
														"Solution: (%s solutions in %s min %s sec)",
														puzzle
																.getSolutionCount(),
														puzzle.getTotalTime() / 60000,
														(puzzle.getTotalTime() % 60000) / 1000.0));
							
}
						
});
					
}
				
};
				solverThread.setDaemon(true);
				solverThread.start();
			
}
		
});
	
}

	private ExpandBar pieceBar;
	private ExpandItem[] pieceItem = new ExpandItem[3];
	private Composite[] pieceItemContainer = new Composite[3];

	private void createPieceBar(Composite container) {

		pieceBar = new ExpandBar(container, SWT.V_SCROLL);
		pieceBar.setSpacing(8);
		pieceBar.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true,
				true));
	
}

	private void createTileItem() {

		createExpandItem(0, String
				.format("Tile (%s)", puzzle.getTiles().length), puzzle
				.getTiles());
	
}

	private void createBoardItem() {

		createExpandItem(1, "Board", puzzle.getBoard());
	
}

	private void createExpandItem(int index, String title, Piece... pieces) {

		ScrolledComposite sc = new ScrolledComposite(pieceBar, SWT.H_SCROLL
				| SWT.V_SCROLL);

		Composite wrapper = new Composite(sc, SWT.NULL);
		sc.setContent(wrapper);
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		layout.marginWidth = layout.marginHeight = 0;
		wrapper.setLayout(layout);

		Composite container = new Composite(wrapper, SWT.NULL);
		layout = new GridLayout();
		layout.numColumns = pieces.length;
		container.setLayout(layout);
		for (Piece p : pieces) {

			new PieceCompos
...
...
(完整源码请下载查看)
			
...
展开> <收缩

下载源码到电脑,阅读使用更方便

1 积分

快速下载
还剩0行未阅读,继续阅读
云测速

源码文件列表

温馨提示: 点击源码文件名可预览文件内容哦 ^_^
...
名称 大小 修改日期
Puzzle0.00 B2010-05-09|20:37
Puzzle516.00 B2008-04-14|03:27
Puzzle252.00 B2008-04-18|23:36
Puzzle268.00 B2008-04-18|23:36
Puzzle392.00 B2008-04-14|18:24
Puzzle304.00 B2008-04-14|22:08
Puzzle309.00 B2008-04-19|01:56
Puzzle340.00 B2008-04-19|02:32
Puzzle316.00 B2008-04-19|02:32
Puzzle322.00 B2008-04-23|23:33
Puzzle429.00 B2008-04-23|23:33
Puzzle431.00 B2008-04-23|23:33
Puzzle429.00 B2008-04-23|23:34
Puzzle428.00 B2008-04-23|23:34
Puzzle427.00 B2008-04-23|23:34
Puzzle428.00 B2008-04-23|23:34
Puzzle322.00 B2008-04-23|23:34
Puzzle321.00 B2008-04-23|23:34
Puzzle323.00 B2008-04-23|23:34
Puzzle337.00 B2008-04-14|18:30
Puzzle56.00 B2008-04-13|22:31
LICENSE-2.0.txt11.09 kB2010-05-11|11:33
readme.txt521.00 B2010-05-11|11:32
01.96 kB
.classpath2.14 kB2010-05-06|13:08
.project385.00 B2010-05-06|13:06
01.96 kB
org.eclipse.jdt.core.prefs629.00 B2010-05-06|13:06
01.96 kB
application_view_tile.png465.00 B2006-03-12|21:48
readme.txt626.00 B2006-03-12|22:19
logging.properties1.29 kB2010-04-19|01:37
01.96 kB
01.96 kB
01.96 kB
01.96 kB
Board.java3.20 kB2010-05-09|22:26
IMonitor.java137.00 B2010-05-06|13:09
IntChPair.java676.00 B2010-05-06|13:09
IntPair.java662.00 B2010-05-06|13:09
MergedBoard.java2.89 kB2010-05-06|13:45
Piece.java3.13 kB2010-05-07|14:42
Point.java652.00 B2010-05-06|13:09
Program.java6.29 kB2010-05-11|03:07
Puzzle.java12.63 kB2010-05-10|03:05
RawPiece.java1.94 kB2010-05-06|14:49
Solution.java1.31 kB2010-05-10|02:56
Square.java661.00 B2010-05-06|13:23
Tile.java3.99 kB2010-05-07|13:43
01.96 kB
ImageUtil.java1.54 kB2010-05-06|13:10
MainDialog.java12.50 kB2010-05-07|14:32
PieceComposite.java1.80 kB2010-05-06|16:19
云测速

MainDialog.java (29.94 kB)

需要 1 积分
您持有 积分

CodeForge积分(原CF币)全新升级,功能更强大,使用更便捷,不仅可以用来下载海量源代码马上还可兑换精美小礼品了 了解更多

您的积分不足

支付宝优惠套餐快速获取 30 积分

订单支付完成后,积分将自动加入到您的账号。以下是优惠期的人民币价格,优惠期过后将恢复美元价格。

更多付款方式:网银PayPal

上传代码,免费获取

您本次下载所消耗的积分将转交上传作者。

同一源码,30天内重复下载,只扣除一次积分。

登录 CodeForge

还没有CodeForge账号? 立即注册
关注微博
联系客服

Switch to the English version?

Yes
CodeForge 英文版
No
CodeForge 中文版

完善个人资料,获价值¥30元积分奖励!

^_^"呃 ...

Sorry!这位大神很神秘,未开通博客呢,请浏览一下其他的吧
好的