随笔“敢“想

展开阅读全文
博主设置当前文章不允许评论。

挑战的——进!!!!!!

06-11

下面是两个类,即棋盘与棋子,如何将它们组合在一起以实现下棋的操作?这在面向对象设计中,有个专门的关系,就叫组合吧。还有,如何分别对棋盘和棋子设置颜色,以及如何实现“悔棋”的效果,即点击鼠标生成的圈,如何擦除??rn我问题的关键是如何用java实现这些效果,也就是程序算法我知道,不需要啰嗦了。rn请回答问题的各位大虾注意一下,这里有三个问题和一个强调,不要做无用功!!rn[code=Java]//DrawChessman类rnclass DrawChessman extends JPanel rn private static final long serialVersionUID = 4785452373598819719L;rn private int x, y; //位置rn final int ChessSize = 30;//棋子大小rn rn public void paintComponent(Graphics g) //画棋子rn g.drawOval(x-ChessSize/2, y-ChessSize/2, ChessSize, ChessSize); rn rn rn public DrawChessman() rn this.addMouseListener(new MouseAdapter() rn public void mouseClicked(MouseEvent e) rn x = e.getX(); rn y = e.getY(); rn repaint(); rn rn ); rn rn rnrn//ChessBoard类rnclass ChessBoard extends JPanelrn private static final long serialVersionUID = 4785452373598819719L;rn private int x, y; //位置rn final int ChessSize = 30;//棋子大小rn private int width=600, height=600;//rn private int d = width / 20;rnrn public ChessBoard ()rn setBackground(new Color(235, 157, 80));rn rn rn public void paint(Graphics g) //画棋盘 rn for(int i = 0; i<=width; i+= d)rn if(i==0 || i==width)rn g.drawLine(i, 0, i, height);rn g.drawLine(0, i, width, i);rn elsern g.drawLine(i, d, i, height-d);rn g.drawLine(d, i, width-d, i);rn rn rn rnrn[/code] 论坛

没有更多推荐了,返回首页