jpanel二维对象数组绘制问题

Jpanel 2d array of objects drawing problem

我正在尝试用不同颜色绘制两个随机框的二维数组(动态), 这是代码:

Main.java

public class Main {
    public static void main(String[] args) {
     CustomPanel f = new CustomPanel (4, 5);
        JFrame frame = new JFrame("Testing");
        frame.add(f);
        frame.setSize(1000, 1000);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

CustomPanel.java:

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class CustomPanel extends JPanel {
    Drawable [][]boxes;

    public CustomPanel (int rows, int cols)
    {
        this.setLayout(null);
        boxes = new Drawable[rows][cols];
        Random rand = new Random();
        for(int i = 0 ; i < rows ; i ++)
        {
            for(int j = 0 ; j  < cols ; j++)
            {
                switch(rand.nextInt(3))
                {
                    case 0:
                        boxes [i][j] = new Box1();
                        break;
                    case 1:
                        boxes [i][j] = new Box2();
                        break;
                    case 2:
                        boxes [i][j] = new Box3();
                        break;
                }
            }
        }
    }

    public void paintComponent (Graphics g) {
        super.paintComponent(g);

        Rectangle t = g.getClipBounds();
        int box_width = t.width/ this.boxes[0].length;
        int box_heigt = t.height/ this.boxes.length;

        for(int i = 0 ; i < this.boxes.length; i ++)
        {
            for(int j = 0 ; j < this.boxes[0].length; j++)
            {
                System.out.println("" + i + ":" + j);
                boxes [i][j].draw(i * box_width, j * box_heigt, box_width, box_heigt, g);
            }
        }
    }

}

Drawable.java:

import java.awt.Graphics;

public interface Drawable {
    public abstract void draw(int x, int y, int width, int height, Graphics g);
}

Box1(Box2,Box3一样,只是颜色不同):

import java.awt.Color;
import java.awt.Graphics;

public class Box1 implements Drawable{
    public Box1 () { //default constructor
        
    }

    @Override
    public void draw(int x, int y, int width, int height, Graphics g) {
        g.setColor(Color.CYAN);
        g.fillRect(x, y, width, height);
    }
}

问题是有些框根本没有出现在面板上(虽然我确实遍历了行和列)。 我调试了它但无法找出它发生的原因(这可能很愚蠢 - 我知道)

Box1(Box2, Box3 are the same, just different colors):

不要创建单独的 classes,只需将颜色作为参数传递即可。

do you mean at paintComponent ? how? I guess this.getParent().getSize().width ?

是的,paintComponent()。

不,你没有得到父级。您正在 JPanel 上进行自定义绘画。您希望面板的 width/height 使用我在评论中建议的方法。

The problem is that some of the boxes do not appear at the panel at all

在绘制每个 Box 时,您的 x/y 值会反转。 “i”变量代表行(或 y 值),“j”变量代表列(或 x 值)。

所以你的逻辑应该是:

for(int i = 0 ; i < this.boxes.length; i ++)
{
    for(int j = 0 ; j < this.boxes[0].length; j++)
    {

        //boxes [i][j].draw(i * box_width, j * box_heigt, box_width, box_heigt, g);
        boxes [i][j].draw(j * box_width, i * box_heigt, box_width, box_heigt, g);
    }
}

与其使用数组长度 属性 来控制 rows/columns,不如将 row/column 参数保存为 class 中的变量,这可能有助于使您的代码更易于阅读。