首页 > 解决方案 > Jpanel 2d 对象数组绘制问题

问题描述

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

主.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);
    }

}

自定义面板.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);
            }
        }
    }

}

可绘制的.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);
    }
}

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

这是 4 行和 5 列的示例。 最后一列只是没有绘制

标签: javaswingjpanel

解决方案


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

不要创建单独的类,只需将 Color 作为参数传递。

你的意思是在paintComponent?如何?我猜 this.getParent().getSize().width ?

是的,paintComponent()。

不,你没有得到父母。您正在 JPanel 上进行自定义绘画。您需要使用我在评论中建议的方法来获取面板的宽度/高度。

问题是某些框根本没有出现在面板上

当您绘制每个 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);
    }
}

而不是使用数组长度属性来控制行/列,为什么不将行/列参数保存为类中的变量,这可能有助于使您的代码更易于阅读。


推荐阅读