setLocation,setColor

setLocation:

   size(600,500);

    int w = Toolkit.getDefaultToolkit().getScreenSize().width;
    int h = Toolkit.getDefaultToolkit().getScreenSize().height;
    setLocation((w -600)/2,(h-500)/2);

setColor:

g.setColor(Color.RED);

g.setColor(new Color(0,0,0);// RGB模式 0-255

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
怎么在Cellcomponent中实现格子随鼠标移动而高亮,且不与selected相冲突public class AnimalComponent extends ChessComponent { private PlayerColor owner; private boolean selected; /* private ChessboardPoint position; public void setPosition(ChessboardPoint position) { this.position = position; } public ChessboardPoint getPosition(){ return position; } */ private static int bluesEaten = 0;//记录被吃掉的蓝色动物数量 public static void setBluesEaten(int bluesEaten){AnimalComponent.bluesEaten = bluesEaten; System.out.println("蓝棋被吃数量" + AnimalComponent.bluesEaten); } public static int getBluesEaten(){return bluesEaten;} private static int redsEaten = 0;//记录被吃掉的红色动物数量 public static void setRedsEaten(int redsEaten){AnimalComponent.redsEaten = redsEaten; System.out.println("红棋被吃数量" + AnimalComponent.redsEaten); } public static int getRedsEaten(){return redsEaten;} public AnimalComponent(PlayerColor owner, int size) { super(owner , size); this.owner = owner; this.selected = false; setSize(size/2, size/2); setLocation(0,0); setVisible(true); } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("楷体", Font.PLAIN, getWidth() / 2); g2.setFont(font); g2.setColor(owner.getColor()); g2.drawString(" ", getWidth() / 4, getHeight() * 5 / 8); // FIXME: Use library to find the correct offset. if (isSelected()) { // Highlights the model if selected. g.setColor(Color.RED); g.drawOval(0, 0, getWidth() , getHeight()); } } }public class CellComponent extends JPanel { private Color background; public CellComponent(Color background, Point location, int size) { setLayout(new GridLayout(1,1)); setLocation(location); setSize(size, size); this.background = background; } @Override protected void paintComponent(Graphics g) { super.paintComponents(g); g.setColor(background); g.fillRect(1, 1, this.getWidth()-1, this.getHeight()-1); } }
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值