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)
}
}
}
}
你可以把代碼中的'drawString'打電話?可能你做錯了什麼事 – fhofmann
有什麼不對?爲什麼在網格中繪製矩形,而僅在第一個矩形中繪製字符串? ''只需要在位置(25,25)繪製...' – Paw
'drawString(「testString」,25,25);'只在位置繪製(25,25)... –