纸牌显示的方向
打牌一般有几个人,分坐东南西北,暂叫:东家、南家、西家、北家。不同家出的牌显示的方向是不同的。
用一个PlayerOrientation类来封装不同方向的玩家,先写出它的测试类TestPlayerOrientation。
TestPlayerOrientation的代码:
package yxc.card.common.test;
import yxc.card.common.PlayerOrientation;
import junit.framework.TestCase;
public class TestPlayerOrientation extends TestCase {
public void testInit(){
assertEquals("1","东家",PlayerOrientation.EAST.toString());
assertEquals("2","南家",PlayerOrientation.SOUTH.toString());
assertEquals("3","西家",PlayerOrientation.WEST.toString());
assertEquals("4","北家",PlayerOrientation.NORTH.toString());
}
public void testGet(){
assertEquals("1",PlayerOrientation.EAST,PlayerOrientation.getOrientation("东家"));
assertEquals("2",PlayerOrientation.SOUTH,PlayerOrientation.getOrientation("南家"));
assertEquals("3",PlayerOrientation.WEST,PlayerOrientation.getOrientation("西家"));
assertEquals("4",PlayerOrientation.NORTH,PlayerOrientation.getOrientation("北家"));
}
}
PlayOrientation类的代码:
package yxc.card.common;
public final class PlayerOrientation {
private final String name;
public static final PlayerOrientation EAST=new PlayerOrientation("东家");
public static final PlayerOrientation SOUTH=new PlayerOrientation("南家");
public static final PlayerOrientation WEST=new PlayerOrientation("西家");
public static final PlayerOrientation NORTH=new PlayerOrientation("北家");
private PlayerOrientation(final String name) {
super();
this.name=name;
}
@Override
public String toString() {
return name;
}
public static PlayerOrientation getOrientation(String orientationName) {
if(orientationName.equals("东家")){
return EAST;
}else if(orientationName.equals("南家")){
return SOUTH;
}else if(orientationName.equals("西家")){
return WEST;
}else if(orientationName.equals("北家")){
return NORTH;
}
return null;
}
}
运行TestPlayOrientation测试,测试条绿色,OK。
修改CardFace类,使它能根据PlayerOrientation改变其方向。
在CardFace类中增加三个属性:
private PlayerOrientation playerOrientation;
private int coordinateX;
private int coordinateY;
增加playerOrientation的get和set方法,其中setPlayerOrientation方法会根据PlayerOrientation改变纸牌的显示方向:
public PlayerOrientation getPlayerOrientation() {
return playerOrientation;
}
public void setPlayerOrientation(PlayerOrientation playerOrientation) {
this.playerOrientation = playerOrientation;
int width=71;
int height=96;
int angle=0;
coordinateX=0;
coordinateY=0;
if(playerOrientation==null){
angle=0;
}else if(playerOrientation==PlayerOrientation.EAST){
width=96;
height=71;
angle=270;
coordinateY=71;
}else if(playerOrientation==PlayerOrientation.SOUTH){
angle=0;
}else if(playerOrientation==PlayerOrientation.WEST){
width=96;
height=71;
angle=90;
coordinateX=96;
}else if(playerOrientation==PlayerOrientation.NORTH){
angle=180;
coordinateX=71;
coordinateY=96;
}
setSize(new Dimension(width,height));
AffineTransform transform=AffineTransform.getRotateInstance(Math.toRadians(angle));
op=new AffineTransformOp(transform,AffineTransformOp.TYPE_BILINEAR);
repaint();
}
修改paintComponent方法,黑体字部分做了修改:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
if(image!=null){
g2d.drawImage(image, op, coordinateX, coordinateY);
}
}
好了,CardFace类可以根据所设置的PlayerOrientation自动改变它的显示方向了。