A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 廉伟 中级黑马   /  2012-8-29 13:14  /  1538 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我定义了一个继承自panel的类,重写了它的paint()方法画了一个棋盘,并载入一幅水墨画作为棋盘的背景图;又定义了两个继承自Canvas的类用来表示黑子和白子。但是在下棋过程中出现了一个现象就是我的两个表示棋子的类是继承自Canvas的,显示棋子的时候其后的矩形边界也被显示出来了,而且矩形边界和棋子圆之间的空隙的颜色是当前面板的颜色,显示在水墨画上很难看。求解释!!!!

  1. import java.awt.*;
  2. import javax.swing.JFrame;

  3. class testChess
  4. extends JFrame {

  5. chessPad chesspad = new chessPad();

  6. public testChess() {
  7. setSize(450, 450);
  8. setLayout(new BorderLayout());
  9. add(chesspad, BorderLayout.CENTER);

  10. chessPoint_black chesspoint_black = new chessPoint_black(this.chesspad);
  11. chessPoint_white chesspoint_white = new chessPoint_white(this.chesspad);

  12. chesspad.add(chesspoint_white);
  13. chesspad.add(chesspoint_black);

  14. chesspoint_white.setBounds(163, 163, 16, 16);
  15. chesspoint_black.setBounds(103, 103, 16, 16);
  16. setDefaultCloseOperation(this.EXIT_ON_CLOSE );
  17. setVisible(true);
  18. }

  19. public static void main(String[] args) {
  20. testChess testchess = new testChess();
  21. }

  22. }

  23. class chessPad
  24. extends Panel {
  25. Image im = this.getToolkit().getImage("3.jpg");
  26. public chessPad() {
  27. setSize(420, 420);
  28. setLayout(null);
  29. setBackground(new Color(204, 204, 204));
  30. }

  31. public void paint(Graphics g) {

  32. g.drawImage(im, 0, 0, this); //将图片设置为棋盘背景

  33. for (int i = 40; i <= 400; i = i + 20) {
  34. g.drawLine(40, i, 400, i);
  35. }
  36. for (int j = 40; j <= 400; j = j + 20) {
  37. g.drawLine(j, 40, j, 400);
  38. }
  39. g.fillOval(97, 97, 6, 6);
  40. g.fillOval(337, 97, 6, 6);
  41. g.fillOval(97, 337, 6, 6);
  42. g.fillOval(337, 337, 6, 6);
  43. g.fillOval(217, 217, 6, 6);
  44. }

  45. public void update(Graphics g) {
  46. paint(g);
  47. }
  48. }

  49. /**
  50. * 表示黑子的类
  51. */
  52. class chessPoint_black
  53. extends Canvas {
  54. chessPad chesspad = null;
  55. chessPoint_black(chessPad p) {
  56. setSize(20, 20);
  57. chesspad = p;
  58. }

  59. public void paint(Graphics g) {
  60. g.setColor(Color.black);
  61. g.fillOval(0, 0, 14, 14);
  62. }
  63. }

  64. /*
  65. * 表示白子的类
  66. */
  67. class chessPoint_white
  68. extends Canvas {
  69. chessPad chesspad = null;

  70. chessPoint_white(chessPad p) {
  71. setSize(20, 20);
  72. chesspad = p;
  73. }

  74. public void paint(Graphics g) {
  75. g.setColor(Color.white);
  76. g.fillOval(0, 0, 14, 14);
  77. }
  78. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
田建 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马