2013-12-10 47 views
0

我有一個橢圓形,我想標記它是什麼,爲我的國際象棋遊戲。我想用它們上的棋子名稱作爲字符串,但我似乎無法讓它工作。在形狀上畫一個字符串

它甚至可以在形狀上繪製字符串嗎?

我的代碼

public void drawPieces(Graphics2D g2d){ 
    for(int x = 0; x < 8; x++) { 
     for(int y = 0; y < 8; y++) { 
     //reds 
     if(board[x][y]==24){ 
      g2d.setColor(Color.red); 
      g2d.fillOval(x*80, y*80, 80, 80); 
      //drawstring goes here 
          g2d.setColor(Color.blue); 
          g2d.drawString("test", x*80, y*80); 
     } 

任何建議,歡迎

編輯,萬一我的格法它幫助。

public void drawGrid(Graphics2D g2d){ 


    g2d.drawLine(0, 0, 0, 639); 
    g2d.drawLine(0, 0, 639, 0); 
    g2d.drawLine(0, 639, 639, 639); 
    g2d.drawLine(639, 0, 639, 639); 

    // draw the horizontal lines using a loop from one to 7, coordiates of each line is (0, x*80, 640, x*80) also 
    // draw vertical lines with coordinates of (x*80, 0, x*80, 640) 
    for(int i = 1; i < 8; i++) { 
     g2d.drawLine(0, i*80, 640, i*80); 
     g2d.drawLine(i*80, 0, i*80, 640); 
    } 
    //drawing the black and white squares 
    for (int row = 0; row < 8; row++) 
     for (int col = 0; col < 8; col++) { 
      if ((row % 2 == 0 && col % 2 == 0) || (row % 2 == 1 && col % 2 == 1) ){ 
       g2d.setColor(black); 
       g2d.fillRect(row*80,col*80,80,80); 

      } 
     } 
} 
+0

你能展示_exactly_你試過了嗎? – kviiri

+0

當然,我會立即進行編輯。 – Strobes

+0

編輯,我的網格方法,以防它的幫助。 - 不,爲了更好地更快地發佈[SSCCE](http://sscce.org/),簡短,可運行,可編譯 – mKorbel

回答

3

我只能說可以在橢圓上繪製一個字符串,而我在我自己的遊戲中就是這樣做的。最上面的繪圖代碼應該沒問題。你只需要檢查你傳遞給繪圖方法和if條件的參數。下面是我的代碼,我使用略有不同的方法來繪製 在橢圓形的摘錄,但你應太:

public void draw(Graphics g){ 
    Graphics2D g2d = (Graphics2D) g; 
    g2d.fill(new Ellipse2D.Double(center.x, center.y, itemSize, itemSize)); 
    g2d.setColor(Color.white); 
    g2d.setFont(new Font("Arial", Font.BOLD, 14)); 
    g2d.drawString(itemName, (int)center.x, (int)center.y+18); 
} 

ITEMNAME是一些字符串,只是不要混淆我的事情是前兩個參數應用到

g2d.fill(...( - , - ,itemSize,itemSize))不是elipse的中心,而是它的sourounding矩形的左上角。

+0

哦,哇,謝謝你的工作!我正在嘗試字體,但你的方式似乎已經完成了這個把戲:) – Strobes

+0

Np,很高興幫助 – croraf