2016-05-30 79 views
0

我有一個繪製4x4板的類,並且它做得很漂亮。我需要的是添加標籤或文本到這些矩形,但我試着用drawString,它什麼也沒做。如何根據二維數組中該位置的數字向矩形添加標籤?將標籤或文本添加到3D矩形java

import java.io.*; 
import java.util.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Tablero extends JPanel{ 
    private int matriz[][]; 

    public Tablero(int m[][]) 
    { 
    matriz=m; 
    } 

    public void actualizar() 
    { 
    repaint(); 
    } 

    public void paintComponent(Graphics g) 
    { 
    super.paintComponent(g); 
    this.setBackground(new Color(0xbbada0)); 
    Color color= new Color (0xcdc1b4); 
    for(int x=0; x<4;x++) 
    { 
    for (int y=0; y<4; y++) 
    { 
     if(matriz[x][y] == 0) 
     { 
     color= new Color(0xcdc1b4); 
     } 
     else if (matriz[x][y] == 2) 
     { 
     color= new Color(0xeee4da); 
     } 
     else if(matriz[x][y] == 4) 
     { 
     color= new Color(0xede0c8); 
     } 
     else if (matriz[x][y]==8) 
     { 
     color= new Color(0xf2b179); 
     } 
     else if (matriz[x][y] == 16) 
     { 
     color= new Color(0xf59563); 
     } 
     else if(matriz[x][y]==32) 
     { 
     color= new Color(0xf67c5f); 
     } 
      else if(matriz[x][y]==64) 
     { 
     color= new Color(0xf65e3b); 
     } 
      else if(matriz[x][y]==128) 
     { 
     color= new Color(0xedcf72); 
     } 
      else if(matriz[x][y]==256) 
     { 
     color= new Color(0xedcc61); 
     } 
      else if(matriz[x][y]==512) 
     { 
     color= new Color(0xedc850); 
     } 
      else if(matriz[x][y]==1024) 
     { 
     color= new Color(0xedc53f); 
     } 
      else if(matriz[x][y]==2048) 
     { 
     color= new Color(0xedc22e); 
     } 
    g.setColor(color); 
    g.fill3DRect(y*70, x*70, 70,70, true); 
    //g.setColor(Color.BLACK); 
    //g.drawString("testString", 25, 25); (this draws the string but only in the first rectangle, and keeps drawing everything only in the first rectangle) 
    } 
    } 
    } 
} 

Picture of interface

+0

你可以把代碼中的'drawString'打電話?可能你做錯了什麼事 – fhofmann

+0

有什麼不對?爲什麼在網格中繪製矩形,而僅在第一個矩形中繪製字符串? ''只需要在位置(25,25)繪製...' – Paw

+0

'drawString(「testString」,25,25);'只在位置繪製(25,25)... –

回答

0

你總是繪製在相同的位置25,25文本。

您需要查看的其他事項是從矩陣中獲取x,y值的部分(爲更好地查看y存儲在第一個數組中)。

所以有這樣的:

enter image description here

你的代碼應該是這個樣子:

import java.io.*; 
import java.util.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Tablero extends JPanel{ 
    private int matriz[][]; 

    public Tablero(int m[][]) 
    { 
     matriz=m; 
    } 

    public void actualizar() 
    { 
     repaint(); 
    } 

    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     this.setBackground(new Color(0xbbada0)); 
     Color color= new Color (0xcdc1b4); 
     for(int x=0; x<4;x++) 
     { 
      for (int y=0; y<4; y++) 
      { 
       if(matriz[y][x] == 0) 
       { 
        color= new Color(0xcdc1b4); 
       } 
       else if (matriz[y][x] == 2) 
       { 
        color= new Color(0xeee4da); 
       } 
       else if(matriz[y][x] == 4) 
       { 
        color= new Color(0xede0c8); 
       } 
       else if (matriz[y][x]==8) 
       { 
        color= new Color(0xf2b179); 
       } 
       else if (matriz[y][x] == 16) 
       { 
        color= new Color(0xf59563); 
       } 
       else if(matriz[y][x]==32) 
       { 
        color= new Color(0xf67c5f); 
       } 
       else if(matriz[y][x]==64) 
       { 
        color= new Color(0xf65e3b); 
       } 
       else if(matriz[y][x]==128) 
       { 
        color= new Color(0xedcf72); 
       } 
       else if(matriz[y][x]==256) 
       { 
        color= new Color(0xedcc61); 
       } 
       else if(matriz[y][x]==512) 
       { 
        color= new Color(0xedc850); 
       } 
       else if(matriz[y][x]==1024) 
       { 
        color= new Color(0xedc53f); 
       } 
       else if(matriz[y][x]==2048) 
       { 
        color= new Color(0xedc22e); 
       } 
       g.setColor(color); 
       int baseX = x*70; 
       int baseY = y*70; 
       g.fill3DRect(baseX, baseY, 70,70, true); 
       g.setColor(Color.BLACK); 
       g.drawString("" + matriz[y][x], baseX + 10, baseY + 25);// (this draws the string but only in the first rectangle, and keeps drawing everything only in the first rectangle) 
      } 
     } 
    } 

    public static void main(String... a){ 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     int[][] tab = { 
       {2, 4, 8, 16}, 
       {32, 64, 128, 256}, 
       {512, 1024, 2048, 2}, 
       {4, 8, 16, 32}, 
     }; 

     f.add(new Tablero(tab)); 
     f.setBounds(0,0, 500, 350); 
     f.setVisible(true); 
    } 
} 
相關問題