2010-09-25 62 views
1

我正在为我的班级做另一个Java项目。在这项任务中,我们必须创建一个棋盘,并使用适当数量的棋子填充棋盘。我构建的棋盘格正确显示,但我很难使用Graphics类进行绘制。如何在JPanel上用Java绘制彩色圆圈?

下面是我到目前为止的代码:

import javax.swing.*; 
import java.awt.*; 

public class Checkerboard extends JFrame { 
    //Define the default values for the separate checker pieces 
    private final int RED_PIECE = 0; 
    private final int BLACK_PIECE = 1; 

    /** Construct the default checker board */ 
    public Checkerboard() { 
     this.setSize(600, 600); 
     this.setResizable(false); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     this.setLocationRelativeTo(null); 
     this.setTitle("Checkerboard Lab"); 
     this.setLayout(new GridLayout(8, 8)); 
     this.setVisible(true); 

     for (int a=0; a<2; a++) { 
      for (int i=0; i<4; i++) { 
       add(new WhiteSpace()); 
       add(new GraySpace(RED_PIECE)); 
      } 
      for (int j=0; j<4; j++) { 
       add(new GraySpace(RED_PIECE)); 
       add(new WhiteSpace()); 
      } 
     } 
     for (int b=0; b<2; b++) { 
      for (int k=0; k<4; k++) { 
       add(new WhiteSpace()); 
       add(new GraySpace(RED_PIECE)); 
      } 
      for (int l=0; l<4; l++) { 
       add(new GraySpace()); 
       add(new WhiteSpace()); 
      } 
     } 
     for (int c=0; c<2; c++) { 
      for (int m=0; m<4; m++) { 
       add(new GraySpace()); 
       add(new WhiteSpace()); 
      } 
      for (int n=0; n<4; n++) { 
       add(new GraySpace(BLACK_PIECE)); 
       add(new WhiteSpace()); 
      } 
     } 
     for (int d=0; d<2; d++) { 
      for (int o=0; o<4; o++) { 
       add(new WhiteSpace()); 
       add(new GraySpace(BLACK_PIECE)); 
      } 
      for (int p=0; p<4; p++) { 
       add(new GraySpace(BLACK_PIECE)); 
       add(new WhiteSpace()); 
      } 
     } 
    } 

    /** White Space constructor */ 
    public class WhiteSpace extends JPanel { 
     public WhiteSpace() { 
      setBackground(Color.WHITE); //Sets the panel's background color to white 
     } 
    } 

    /** Gray Space constructor */ 
    /* GraySpace is a little different, since this color space is the only space that will be holding checker 
    * pieces. There is a default constructor to create a space without a checker piece on it, and another 
    * constructor that places either a red or black piece on the space, pending an optional parameter.*/ 
    public class GraySpace extends JPanel { 
     //Initial variable for the checker piece 
     int checkerPiece; 

     //Default GraySpace constructor 
     public GraySpace() { 
      setBackground(Color.LIGHT_GRAY); 
     } 

     //The GraySpace constructor with the optional parameter to determine if it holds a checker piece 
     public GraySpace(int piece) { 
      this.checkerPiece = piece; 
      setBackground(Color.LIGHT_GRAY); //Sets the panel's background color to white 
     } 

     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      //Default width and height variables 
      int width = getWidth() -10; 
      int height = getHeight() - 10; 

      //This switch statement determines which checker piece type appears on the square 
      switch (checkerPiece) { 
      case RED_PIECE: 
       g.setColor(Color.RED); 
       g.fillOval(5, 5, width, height); 
       break; 
      case BLACK_PIECE: 
       g.setColor(Color.BLACK); 
       g.fillOval(5, 5, width, height); 
       break; 
      } 
     } 
    } 

    /** Initiate the Checker board */ 
    public static void main(String[] args) { 
     JFrame checkerboard = new Checkerboard(); 
    } 
} 

这是相当直截了当。我有我的棋盘类是JFrame的一个子类,我把彩色面板放在一个8x8的方格中。面板是Checkerboard类的内部类,每个类都扩展JPanel(WhiteSpace和GraySpace)。由于GraySpace是唯一必须持有检查器的类,因此我认为我只是将图形代码放入了GraySpace内部类。

无论如何,对于我的问题:我如何去使用图形绘制?我知道我必须显式声明paintComponent()方法才能绘制圆,但我不知道如何指定GraySpace的尺寸以便有效地绘制。有什么建议?

编辑:新问题!

好的,所以我想清楚我会如何将棋子添加到我的棋盘上,并且完美地工作。我的GraySpace内部类有一个可选的构造函数,其值为int,并从中决定在GraySpace面板上将显示哪些颜色片段。我测试了这一点,它的工作原理。

但是,我的问题实际上是将棋子放到棋盘上。董事会必须代表一个“默认”棋盘游戏,所有可用的棋子都在棋盘上。所以,三排红色检查员,三排黑色检查员,两排空行将它们分开。到目前为止,我有4个独立的for循环在板上一次绘制两行......但它不能正常工作。有什么建议?最新的源代码就是上面的代码,取代了我以前的问题源代码。再次感谢您的任何建议!

回答

0

呼叫getHeightgetWidth你在组件上。由于paintComponent是你JPanel类的成员,你可以叫getHeightgetWidth直接。

我对你的方法做了一些其他的修改。

  1. 不要叫this.paintComponent,调用基类(super
  2. 你需要在绘制之前设置的颜色。
  3. 我敢打赌fillOval是你想要的。

例如:

protected void paintComponent(Graphics g) { 
    int h = getHeight(); 
    int w = getWidth(); 
    super.paintComponent(g); 
    g.setColor(CHECKER_COLOR); 
    g.fillOval(w/2, h/2, w, h); 
} 

对于额外的信用,开启抗锯齿功能,让你的棋子看起来棒极了!

+0

Ooooohhh !!感谢那!我有几个关于你的代码的问题,只是为了澄清一些事情: 1)为什么我叫超级而不是这个? 2)为了使用paintComponent(),我必须创建一个类Graphics的对象吗?我知道它是Graphics类的一部分,所以这就是我被绊倒的地方。 非常感谢您的帮助!我认为我必须画出圆圈,然后填充它!谢谢!! – 2010-09-25 22:12:27

+0

1)如果你调用它,你会递归调用你的paintComponent。用super调用基类会为你绘制背景。尝试评论该行,看看会发生什么。 – Starkey 2010-09-25 22:17:32

+0

2)你的Graphics对象作为参数传递给paintComponent,所以你不必创建一个。 – Starkey 2010-09-25 22:18:17

0

有一件事关于你的循环仅仅为了将来的知识,当你定义int i = 0时,该变量只存在于该循环中,所以你不需要使用不同的变量a,b,c,d, e等在你的4 for循环中,你可以简单地使用i,j,k 4次。

此外,我认为你的循环添加更多的东西比你想要的。

for (int a=0; a<2; a++) {    //2 (
    for (int i=0; i<4; i++) {   // 4 
     add(new WhiteSpace());   //  (1 
     add(new GraySpace(RED_PIECE)); //   + 1) 
    }         // +      
    for (int j=0; j<4; j++) {   // 4 
     add(new GraySpace(RED_PIECE)); //  (1 
     add(new WhiteSpace());   //   + 1) 
    }         // ) 
}          //= 2 (4 * 2 + 4 * 2) = 32 

在这个循环中,您将添加32个方块。你做这4次。这已经是你需要的两倍。

最简单的解决方案是去除4个外环。那么你会有你想要的。

+0

是的,我在想了一会之后偶然发现了这个问题。幸运的是,我及时解决了这个问题,并且它非常完美! – 2010-09-28 01:31:40