2013-12-10 50 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