将标签或文本添加到3D Rectangle java

时间:2016-05-30 12:45:10

标签: java graphics

我有一个画一个4x4板的课程并且做得很漂亮。我需要的是为这些矩形添加标签或文本,但我尝试使用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[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

1 个答案:

答案 0 :(得分: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);
    }
}