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

📄 appjumpchess.java

📁 一款JAVA款的跳棋
💻 JAVA
字号:
package org.yushang.jumpchess.app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.yushang.jumpchess.Interface.*;
import org.yushang.jumpchess.image.ImageLoader;
import org.eclipse.swt.widgets.Canvas;

public class AppJumpChess {

	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	
	private JumpChessControl jumpChessControl = null;

	private Canvas canvas = null;	
	private Label lblClose = null;
	private Label lblTitle = null;
	private Label lblBegin = null;
	private Label lblExit = null;	
	
	private Canvas setCanvas = null;
	private Label lblOK = null;
	private Label lblCancel = null;
	
	private Label[] lblType = new Label[6];
	private Text[] txt = new Text[6];
	
	private boolean mouseDown = false;
	private Point lastMousePoint = null;

	/**
	 * This method initializes canvas	
	 *
	 */
	private void createCanvas() {
		canvas = new Canvas(sShell, SWT.NO_BACKGROUND);
		canvas.setBounds(new org.eclipse.swt.graphics.Rectangle(0,0,800,600));
	}
	
	private void createClose(Composite parent) {
		final ImageLoader imageClose1 = new ImageLoader(sShell.getDisplay(), "Close1.jpg");
		final ImageLoader imageClose2 = new ImageLoader(sShell.getDisplay(), "Close2.jpg");
		final ImageLoader imageClose3 = new ImageLoader(sShell.getDisplay(), "Close3.jpg");
		
		lblClose = new Label(parent, SWT.NO_BACKGROUND);
		
        lblClose.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
            	if (e.button == 1) {
            		lblClose.setImage(imageClose3.getImage());                	
				}            	
            }
            
            public void mouseUp(MouseEvent e) {
            	if (e.button == 1) {
            		lblClose.setImage(imageClose2.getImage());
            		sShell.dispose();
				}            	
            }            
        });

        lblClose.addMouseTrackListener(new MouseTrackAdapter() {
            public void mouseEnter(MouseEvent e) {
                lblClose.setImage(imageClose2.getImage());
            }
            public void mouseExit(MouseEvent e) {
            	lblClose.setImage(imageClose1.getImage());
            }
        });
        
        lblClose.setImage(imageClose1.getImage());
        lblClose.setBounds(765, 4, 26, 20);		
	}
	
	private void createTitle (Composite parent) {		
		final ImageLoader imageTitle = new ImageLoader(sShell.getDisplay(), "Title.jpg");		
		lblTitle = new Label(parent, SWT.NO_BACKGROUND);
        lblTitle.addMouseMoveListener(new MouseMoveListener() {
            public void mouseMove(MouseEvent e) {
                if (mouseDown) {
                	Point nowMousePoint = new Point(e.x, e.y);
                	if (lastMousePoint.equals(nowMousePoint)) {
						return;
					}
                	
                    Point point = sShell.getLocation();
                    point.x += (nowMousePoint.x - lastMousePoint.x);
                    point.y += (nowMousePoint.y - lastMousePoint.y);
                    sShell.setLocation(point);
                }  
            }
        });
        
        lblTitle.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
                mouseDown = true;
                lastMousePoint = new Point(e.x, e.y);
            }
            public void mouseUp(MouseEvent e) {
                mouseDown = false;
            }
        });
        lblTitle.setImage(imageTitle.getImage());
        lblTitle.setBounds(0, 0, 800, 28);		
	}
	
	private void createBeginButton (Composite parent) {
		final ImageLoader imageBegin1 = new ImageLoader(sShell.getDisplay(), "Begin1.jpg");
		final ImageLoader imageBegin2 = new ImageLoader(sShell.getDisplay(), "Begin2.jpg");
		final ImageLoader imageBegin3 = new ImageLoader(sShell.getDisplay(), "Begin3.jpg");
		lblBegin = new Label(parent, SWT.NO_BACKGROUND);
		
		lblBegin.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
            	if (e.button == 1) {
            		lblBegin.setImage(imageBegin3.getImage());                	
				}
            }   
            public void mouseUp(MouseEvent e) {
            	if (e.button == 1) {
            		lblBegin.setImage(imageBegin2.getImage());
            		setCanvas.setVisible(true);
				}            	
            }            	
            	
        });

		lblBegin.addMouseTrackListener(new MouseTrackAdapter() {
            public void mouseEnter(MouseEvent e) {
            	lblBegin.setImage(imageBegin2.getImage());
            }
            public void mouseExit(MouseEvent e) {
            	lblBegin.setImage(imageBegin1.getImage());
            }
        });        
		
		lblBegin.setImage(imageBegin1.getImage());
		lblBegin.setBounds(620, 540, 75, 38);		
	}
	
	private void createExitButton (Composite parent) {
		final ImageLoader imageExit1 = new ImageLoader(sShell.getDisplay(), "Exit1.jpg");
		final ImageLoader imageExit2 = new ImageLoader(sShell.getDisplay(), "Exit2.jpg");
		final ImageLoader imageExit3 = new ImageLoader(sShell.getDisplay(), "Exit3.jpg");		
		lblExit = new Label(parent, SWT.NO_BACKGROUND);
		
		lblExit.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
            	if (e.button == 1) {
            		lblExit.setImage(imageExit3.getImage());                		
				} 
            	if (e.button == 3) {
            		jumpChessControl.Debug();
            	}            	
            }
            public void mouseUp(MouseEvent e) {
            	if (e.button == 1) {
            		lblExit.setImage(imageExit2.getImage());	
            		sShell.dispose();
				}
            }
        });

		lblExit.addMouseTrackListener(new MouseTrackAdapter() {
            public void mouseEnter(MouseEvent e) {
            	lblExit.setImage(imageExit2.getImage());
            }
            public void mouseExit(MouseEvent e) {
            	lblExit.setImage(imageExit1.getImage());
            }
        });  
		lblExit.setImage(imageExit1.getImage());
		lblExit.setBounds(702, 540, 75, 38);		
	}
	
	private void createLable (Composite parent) {		
		//关闭按扭
		createClose(parent);
        //标题栏
        createTitle(parent);         
        //开始按钮
        createBeginButton(parent);        
        //退出按钮
        createExitButton(parent);
	}

	private void createSetCanvas (Composite parent) {
		final ImageLoader imageInit = new ImageLoader(sShell.getDisplay(), "init.jpg");
		setCanvas = new Canvas(parent, SWT.NO_BACKGROUND);
		setCanvas.setBounds(new org.eclipse.swt.graphics.Rectangle(167, 144, 266, 312));
		setCanvas.addListener(SWT.Paint, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {				
				//Rectangle rect = canvas.getClientArea();
				//Image buffer = new Image(canvas.getDisplay(), rect);
				//GC gc = new GC(buffer);
				
				imageInit.Draw(event.gc, 0, 0, 0);

				//gc.dispose();
				//event.gc.drawImage(buffer, 0, 0);		
				//buffer.dispose();
			}
		});
		setCanvas.setVisible(false);
	}
	
	private void createSetLabel (Composite parent) {
		createOKButton(parent);
		createCancelButton(parent);
		
		for (int i = 0; i < 6; i++) {
			createTxt(parent, 65, 57 + 34 * i, i);
			createType(parent, 201, 57 + 34 * i, i);	
		}		
	}
	
	private void createCancelButton (Composite parent) {
		final ImageLoader imageCancel1 = new ImageLoader(sShell.getDisplay(), "Cancel1.jpg");
		final ImageLoader imageCancel2 = new ImageLoader(sShell.getDisplay(), "Cancel2.jpg");
		final ImageLoader imageCancel3 = new ImageLoader(sShell.getDisplay(), "Cancel3.jpg");
		lblCancel = new Label(parent, SWT.NO_BACKGROUND);
		
		lblCancel.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
            	if (e.button == 1) {
            		lblCancel.setImage(imageCancel3.getImage());            		
				}
            }   
            public void mouseUp(MouseEvent e) {
            	if (e.button == 1) {
            		lblCancel.setImage(imageCancel2.getImage());
            		setCanvas.setVisible(false);
				}            	
            }            	
            	
        });

		lblCancel.addMouseTrackListener(new MouseTrackAdapter() {
            public void mouseEnter(MouseEvent e) {
            	lblCancel.setImage(imageCancel2.getImage());
            }
            public void mouseExit(MouseEvent e) {
            	lblCancel.setImage(imageCancel1.getImage());
            }
        });        
		
		lblCancel.setImage(imageCancel1.getImage());
		lblCancel.setBounds(154, 266, 70, 30);
	}
	private void Initialize() {
		String[] names = new String[6];
		int[] types = new int[6];
		int index = 0;

		for (int i = 0; i < types.length; i++) {
			int data = ((Integer) lblType[i].getData()).intValue();
			if (data != 2) {
				names[index] = txt[i].getText();
				types[index] = data;
				index++;
			}
		}
		
		if (index >= 2) {
			setCanvas.setVisible(false);
			jumpChessControl.Initialize(names, types, index);			
		} else {
			MessageBox a = new MessageBox(sShell);
			a.setMessage("至少要两个玩家才可以玩游戏");
			a.open();
		}
	}
	private void createOKButton (Composite parent) {
		final ImageLoader imageOK1 = new ImageLoader(sShell.getDisplay(), "OK1.jpg");
		final ImageLoader imageOK2 = new ImageLoader(sShell.getDisplay(), "OK2.jpg");
		final ImageLoader imageOK3 = new ImageLoader(sShell.getDisplay(), "OK3.jpg");
		lblOK = new Label(parent, SWT.NO_BACKGROUND);
		
		lblOK.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
            	if (e.button == 1) {
            		lblOK.setImage(imageOK3.getImage());
				}
            }   
            public void mouseUp(MouseEvent e) {
            	if (e.button == 1) {
            		lblOK.setImage(imageOK2.getImage());
            		Initialize();
				}            	
            }            	
            	
        });

		lblOK.addMouseTrackListener(new MouseTrackAdapter() {
            public void mouseEnter(MouseEvent e) {
            	lblOK.setImage(imageOK2.getImage());
            }
            public void mouseExit(MouseEvent e) {
            	lblOK.setImage(imageOK1.getImage());
            }
        });        
		
		lblOK.setImage(imageOK1.getImage());
		lblOK.setBounds(44, 266, 70, 30);		
	}
	
	private void createTxt (Composite parent, int x, int y, int index) {
		txt[index] = new Text(parent, SWT.NO_BACKGROUND);
		txt[index].setBounds(x, y, 120, 19);
		txt[index].setBackground(new Color(null, 115, 70, 3));
		txt[index].setForeground(new Color(null, 255, 255, 255));
		
		String name = "";
		switch (index) {
		case 0:
			name = "俞尚"; //人
			break;
		case 1:
			name = "俞蔼然"; //计算机
			break;
		default:
			name = ""; //关闭
			break;
		}
		txt[index].setText(name);
	}
	
	private void createType (Composite parent, int x, int y, int index) {
		final ImageLoader[] imageType = new ImageLoader[3];
		imageType[0] = new ImageLoader(sShell.getDisplay(), "Type1.jpg");
		imageType[1] = new ImageLoader(sShell.getDisplay(), "Type2.jpg");
		imageType[2] = new ImageLoader(sShell.getDisplay(), "Type3.jpg");

		lblType[index] = new Label(parent, SWT.NO_BACKGROUND);
		
		lblType[index].addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
            	if (e.button == 1) {
            		Label source = (Label) e.getSource();
            		int data = ((Integer) source.getData()).intValue() + 1;
            		if (data >= 3) {
						data = 0;
					}
            		source.setData(new Integer(data));            		
            		source.setImage(imageType[data].getImage());                	
				}
            }   
            public void mouseUp(MouseEvent e) {            	
            }            	
            	
        });		
		lblType[index].setBounds(x, y, 39, 17);
		
		int data = -1;
		switch (index) {
		case 0:
			data = 1; //人
			break;
		case 1:
			data = 0; //计算机
			break;
		default:
			data = 2; //关闭
			break;
		}
		lblType[index].setImage(imageType[data].getImage());
		lblType[index].setData(new Integer(data));
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/* 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();		
		AppJumpChess thisClass = new AppJumpChess();
		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.NO_TRIM);
		sShell.setText("我的跳棋");	
		
		sShell.setSize(new org.eclipse.swt.graphics.Point(800,600));
		createCanvas();
		createLable(canvas);
		createSetCanvas(canvas);
		createSetLabel(setCanvas);				

		jumpChessControl = new JumpChessControl(sShell, canvas);
	}


}

⌨️ 快捷键说明

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