纸牌显示的方向

纸牌显示的方向

打牌一般有几个人,分坐东南西北,暂叫:东家、南家、西家、北家。不同家出的牌显示的方向是不同的。

用一个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;

增加playerOrientationgetset方法,其中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自动改变它的显示方向了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值