2016-05-30 7 views
0

4x4ボードを描画して美しく表示するクラスがあります。私が必要とするのは、これらの矩形にラベルやテキストを追加することですが、drawStringを試してみましたが、何もしませんでした。 2次元配列のその位置にある数に応じて、矩形にラベルを追加するにはどうすればよいですか?3D Rectangleにラベルやテキストを追加する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

enter image description here

をあなたのコードは次のようになりますか?おそらくあなたは何か間違ったことをしたでしょうか。 – fhofmann

+0

どうしたのですか?矩形がグリッドで描かれているのはなぜですか?しかし、文字列は最初の矩形だけに描かれていますか? – Paw

+0

'drawString(" testString "、25、25);'は位置(25,25)で描画するだけです... –

答えて

0

あなたは同じ位置25,25に常にテキストを描画しています。

レビューする必要があるその他のものは、マトリックスからx、y値を得る部分です(より良い表示のためにはyが最初の配列に格納されています)。

だからこれ持っている:あなたはコード `drawString`コールに置くことができます

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); 
    } 
} 
関連する問題