2010-09-25 112 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