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

📄 drawer.java

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

// Download:http://www.codefans.net
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.Display;
import org.yushang.jumpchess.image.ImageLoader;
import org.yushang.jumpchess.pub.Color;
import org.yushang.jumpchess.pub.Position;

public class Drawer {
	
	final static private double PX1_5 = 70;   //Position(1, 5)的X坐标	
	final static private double PY5_17 = 55;  //Position(5, 17)的Y坐标
	final static private double DIAMETERX = (513.0 - PX1_5) / 12;
	final static private double DIAMETERY = (573 - PY5_17) / 16;
	final static private double CHESS_WIDTH = 32;
	final static private double CHESS_HEIGHT = 32;	
	
	private ImageLoader[] imageChess = null;
	private ImageLoader imagePosition = null;
	private ImageLoader imageBack = null;
	
	public Drawer(Display display) {
		imageChess = new ImageLoader[6];
		imageChess[0] = new ImageLoader(display, "Brown.ico");
		imageChess[1] = new ImageLoader(display, "Yellow.ico");
		imageChess[2] = new ImageLoader(display, "Red.ico");
		imageChess[3] = new ImageLoader(display, "Blue.ico");
		imageChess[4] = new ImageLoader(display, "Purple.ico");
		imageChess[5] = new ImageLoader(display, "Green.ico");
		imagePosition = new ImageLoader(display, "Position.gif");
		imageBack = new ImageLoader(display, "Back.jpg");
	}
	

	public Position MouseXYToPosition(Point point) {
		int y = 17 - (int) ((point.y - PY5_17) / DIAMETERY + 0.5);		
		int x = (int) ((point.x - PX1_5) / DIAMETERX - 1.0 + y * 0.5);
		try {
			return new Position(x, y);
		} catch (RuntimeException e) {
			///System.out.println(e.toString());
			return null;
		}				
	}
	
	public Point PositionToMouseXY(Position position) {
		double x = PX1_5 + DIAMETERX * (position.getx() - 1) - 
		           DIAMETERX / 2 * (position.gety() - 5) ;
		double y = PY5_17 + DIAMETERY * (17 - position.gety());
		
		return new Point((int) x, (int) y);
	}
	
	public void DrawChessBoard(GC gc) {
		imageBack.Draw(gc, 0, 0, 0);		
	}
	
	public void DrawChess(GC gc, Color color, Position position, int index) {
		Point point = PositionToMouseXY(position);
		DrawChess(gc, color, point, 0);
		if (index != -1) {
			gc.drawText("[" + index + "]", point.x, point.y);
		}		
	}
	
	public void DrawChess(GC gc, Color color, Point point, int Offset) {
		if (point == null) {
			return;
		}
		Point P = new Point(point.x, point.y);
		ImageLoader image = null;
		
		if (color == Color.Brown) {
			image = imageChess[0];			
		} else if (color == Color.Yellow) {
			image = imageChess[1];
		} else if (color == Color.Red) {
			image = imageChess[2];
		} else if (color == Color.Blue) {
			image = imageChess[3];
		} else if (color == Color.Purple) {
			image = imageChess[4];
		} else if (color == Color.Green) {
			image = imageChess[5];
		}
		P.x -= CHESS_WIDTH / 2;
		P.y -= CHESS_HEIGHT / 2;
		image.Draw(gc, P.x, P.y, Offset);
	}	
	
	public void DrawPosition(GC gc, Position position) {
		Point point = PositionToMouseXY(position);
		point.x -= CHESS_WIDTH / 2 - 1;
		point.y -= CHESS_HEIGHT / 2 - 1;
		imagePosition.Draw(gc, point.x, point.y, 0);
	}	
	
	public void DrawCoordinate(GC gc, Position position) {
		if (position != null) {
			Point p = PositionToMouseXY(position);
			String s = "[" + position.getx() + "," + position.gety() + "]";
			gc.drawText(s, p.x - 10, p.y - 5, true);
		}
	}

}

⌨️ 快捷键说明

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