www.pudn.com > campusleader.rar > Client.java, change:2012-01-05,size:9419b


package Client; 
import GraphPackage.*; 
import java.awt.Graphics; 
import java.awt.Toolkit; 
import java.*; 
 
import org.eclipse.jface.dialogs.MessageDialog; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.events.PaintEvent; 
import org.eclipse.swt.events.PaintListener; 
import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Canvas; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.graphics.Image; 
import org.eclipse.swt.graphics.GC; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.custom.CLabel; 
import org.eclipse.swt.layout.FormLayout; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.widgets.Label; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.FormData; 
import org.eclipse.swt.layout.FormAttachment; 
import org.eclipse.swt.graphics.Rectangle; 
import org.eclipse.swt.widgets.Text; 
 
 
public class Client { 
 
	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="38,2" 
	private CLabel cLabel = null; 
	private Label label = null; 
	private Canvas canvas = null; 
	private Button button1 = null; 
	private Button button2 = null; 
	private Button button = null; 
	private Button button21 = null; 
	private Button button22 = null; 
	private Button button23 = null; 
	private Text text = null; 
	private Text text1 = null; 
	private Button button24 = null; 
	private String[] s1 = new String[7]; 
	private int start,end; 
	private GraphInterface graph; 
	private Image[] pImage = new Image[11]; 
	private PaintListener[] pLis = new PaintListener[11];  
	private boolean[] used = new boolean[11]; 
	private Button button3 = null; 
	private Button button31 = null; 
	/** 
	 * This method initializes canvas	 
	 * 
	 */ 
	public Client(){ 
		s1[0] = null; 
		s1[1] = new String("东区田径场"); 
		s1[2] = new String("美食园"); 
		s1[3] = new String("图书馆"); 
		s1[4] = new String("行政楼"); 
		s1[5] = new String("宝琛广场"); 
		s1[6] = new String("青春剧场"); 
		start = 0; 
		end = 0; 
		graph = new Graph<Integer>(); 
 
		graph.addVertex(1); 
		graph.addVertex(2); 
		graph.addVertex(3); 
		graph.addVertex(4); 
		graph.addVertex(5); 
		graph.addVertex(6); 
		graph.addEdge(2,1,3,2); 
		graph.addEdge(2,5,14,8); 
		graph.addEdge(1,5,15,9); 
		graph.addEdge(1,4,5,4); 
		graph.addEdge(5,3,4,3); 
		graph.addEdge(3,4,2,1); 
		graph.addEdge(5,6,13,7); 
		graph.addEdge(3,6,6,5); 
		graph.addEdge(4,6,7,6); 
		graph.addEdge(1,3,5,10); 
 
	} 
	 
	private void load(){ 
		for(int i=1;i<=10;i++){ 
			String s = "pic/"+i+".png"; 
			pImage[i] = new Image(sShell.getDisplay(),s); 
			final int flag=i; 
			pLis[i] = new PaintListener() { 
	            public void paintControl(PaintEvent e) { 
	                if(pImage[flag]!=null) 
	                    e.gc.drawImage(pImage[flag], 0, 0); 
	                	 
	            } 
	        }; 
	        used[i] = false; 
		} 
	} 
	 
	private void createCanvas() { 
		final Image image = new Image(sShell.getDisplay(),"pic/map.jpg"); 
 
		load(); 
		canvas = new Canvas(sShell, SWT.NONE); 
		canvas.setLayout(null); 
		canvas.setBounds(new Rectangle(0, 0, 713, 882)); 
		canvas.setBackgroundImage(image); 
		canvas.redraw(); 
		FormData formData = new FormData(); 
		formData.height = 30; 
		formData.width = 30; 
		formData.top = new FormAttachment(0, 415); 
		formData.left = new FormAttachment(0, 408); 
		button1 = new Button(canvas, SWT.NONE); 
		button1.setText("1"); 
		button1.setBounds(new Rectangle(408, 415, 30, 30)); 
		button2 = new Button(canvas, SWT.PUSH); 
		button2.setText("2"); 
		button2.setBounds(new Rectangle(359, 289, 30, 30)); 
		button1.addMouseListener(new org.eclipse.swt.events.MouseAdapter() { 
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 
				if (start == 0){ 
					start = Integer.parseInt(button1.getText()); 
					text.setText(s1[start]); 
				} 
				else 
				{ 
					end = Integer.parseInt(button1.getText()); 
					text1.setText(s1[end]); 
				} 
			} 
		}); 
		button2.addMouseListener(new org.eclipse.swt.events.MouseAdapter() { 
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 
				if (start == 0){ 
					start = Integer.parseInt(button2.getText()); 
					text.setText(s1[start]); 
				} 
				else 
				{ 
					end = Integer.parseInt(button2.getText()); 
					text1.setText(s1[end]); 
				} 
			} 
		}); 
		button = new Button(canvas, SWT.NONE); 
		button.setBounds(new Rectangle(430, 554, 30, 30)); 
		button.setText("3"); 
		button.addMouseListener(new org.eclipse.swt.events.MouseAdapter() { 
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 
				if (start == 0){ 
					start = Integer.parseInt(button.getText()); 
					text.setText(s1[start]); 
				} 
				else 
				{ 
					end = Integer.parseInt(button.getText()); 
					text1.setText(s1[end]); 
				} 
			} 
		}); 
		button21 = new Button(canvas, SWT.NONE); 
		button21.setBounds(new Rectangle(499, 539, 30, 30)); 
		button21.setText("4"); 
		button21.addMouseListener(new org.eclipse.swt.events.MouseAdapter() { 
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 
				if (start == 0){ 
					start = Integer.parseInt(button21.getText()); 
					text.setText(s1[start]); 
				} 
				else 
				{ 
					end = Integer.parseInt(button21.getText()); 
					text1.setText(s1[end]); 
				} 
			} 
		}); 
		button22 = new Button(canvas, SWT.NONE); 
		button22.setBounds(new Rectangle(347, 616, 30, 30)); 
		button22.setText("5"); 
		button22.addMouseListener(new org.eclipse.swt.events.MouseAdapter() { 
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 
				if (start == 0){ 
					start = Integer.parseInt(button22.getText()); 
					text.setText(s1[start]); 
				} 
				else 
				{ 
					end = Integer.parseInt(button22.getText()); 
					text1.setText(s1[end]); 
				} 
			} 
		}); 
		button23 = new Button(canvas, SWT.NONE); 
		button23.setBounds(new Rectangle(563, 698, 30, 30)); 
		button23.setText("6"); 
		button23.addMouseListener(new org.eclipse.swt.events.MouseAdapter() { 
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 
				if (start == 0){ 
					start = Integer.parseInt(button23.getText()); 
					text.setText(s1[start]); 
				} 
				else 
				{ 
					end = Integer.parseInt(button23.getText()); 
					text1.setText(s1[end]); 
				} 
			} 
		}); 
		text = new Text(canvas, SWT.BORDER); 
		text.setBounds(new Rectangle(485, 19, 131, 25)); 
		text1 = new Text(canvas, SWT.BORDER); 
		text1.setBounds(new Rectangle(485, 59, 132, 23)); 
		button24 = new Button(canvas, SWT.NONE); 
		button24.setBounds(new Rectangle(560, 103, 58, 27)); 
		button24.setText("搜索"); 
		button3 = new Button(canvas, SWT.NONE); 
		button3.setBounds(new Rectangle(640, 20, 45, 23)); 
		button3.setText("清除"); 
		button3.addMouseListener(new org.eclipse.swt.events.MouseAdapter() { 
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 
				text.setText(""); 
				start = 0; 
			} 
		}); 
		button31 = new Button(canvas, SWT.NONE); 
		button31.setBounds(new Rectangle(640, 60, 45, 23)); 
		button31.setText("清除"); 
		button31.addMouseListener(new org.eclipse.swt.events.MouseAdapter() { 
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 
				text1.setText(""); 
				end = 0; 
			} 
		}); 
		button24.addMouseListener(new org.eclipse.swt.events.MouseAdapter() { 
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 
				if (end == 0 || start == 0){ 
					MessageDialog dl = new MessageDialog(sShell,"错误",null,"请补全地点信息",MessageDialog.INFORMATION, 
														new String[]{"确认"},1); 
					dl.open(); 
				} 
				else{ 
					for (int i=1;i<=10;i++){ 
						if (used[i]){ 
							used[i]=false; 
							canvas.removePaintListener(pLis[i]); 
						} 
					} 
					StackInterface<VertexInterface> path = new Stack<VertexInterface>(); 
					path = graph.getCheapestPath(start, end); 
					//输出 
					while (!path.isempty()){ 
						VertexInterface begin = path.peek(); 
						path.pop(); 
						if (path.isempty()) break; 
						final int flag = begin.getNeighborFlag(path.peek()); 
						canvas.addPaintListener(pLis[flag]); 
						canvas.redraw(); 
						used[flag]=true; 
					} 
				} 
			} 
		}); 
		 
	} 
 
	/** 
	 * @param args 
	 */ 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		/* Before this is run, be sure to set up the launch configuration (Arguments->VM Arguments) 
		 * for the correct SWT library path in order to run with the SWT dlls.  
		 * The dlls are located in the SWT plugin jar.   
		 * For example, on Windows the Eclipse SWT 3.1 plugin jar is: 
		 *       installation_directory\plugins\org.eclipse.swt.win32_3.1.0.jar 
		 */ 
			 
		 
		Display display = Display.getDefault(); 
		Client thisClass = new Client(); 
		thisClass.createSShell(); 
		thisClass.sShell.open(); 
 
		while (!thisClass.sShell.isDisposed()) { 
			if (!display.readAndDispatch()) 
				display.sleep(); 
		} 
		display.dispose(); 
	 
	} 
 
	/** 
	 * This method initializes sShell 
	 */ 
	private void createSShell() { 
		sShell = new Shell(SWT.SHELL_TRIM); 
		sShell.setText("Shell"); 
		sShell.setMinimized(true); 
		sShell.setMaximized(false); 
		sShell.setVisible(false); 
		sShell.setSize(new Point(729, 920)); 
		sShell.setLayout(null); 
		createCanvas(); 
	} 
 
}