paintComponent() 没有正确绘制矩形

paintComponent() not painting rectangles correctly

我在 JPanel 中绘制了自定义矩形网格。我将矩形存储在矩形列表中。我在 JPanel 中添加了一个 MouseMotionListener 和一个 MouseListener;这两个组件都将监听用户点击或点击并按住,找出鼠标标线所在的位置,循环遍历我的矩形列表并相应地更新其枚举值。然后我调用 repaint() 来重绘 JPanel 列表中的矩形。唯一的问题是,在重新绘制列表时,它不是只绘制那个矩形,而是绘制整个列。请帮助。

我的初始化矩形列表

for(int i = 0; i < 10; i++)
        {
            for(int j = 0; i < 10; i++)
            {
                initList.add(new PathRectangle(i*49, j*49, 49, 49));
            }
        }
        
        mainPanel = new MainPanel(10, 10, initList);
        mainFrame.add(mainPanel);

我的矩形Class

import  java.awt.*;

public class PathRectangle extends Rectangle
{

    private recType recVal = recType.UNDECLARED;

    //double pythHeurVal, manhattanHeurVal;

    public PathRectangle(int x, int y, int width, int height)
    {
        super(x, y, width, height);
    }

    public enum recType{WALL, STARTNODE, ENDNODE, UNDECLARED}

    public recType getRecVal() {
        return this.recVal;
    }

    public void setRecVal(recType wall) {
        this.recVal = wall;
    }


}

添加鼠标动作监听器

addMouseMotionListener(new MouseAdapter()
        {
            @Override
            public void mouseDragged(MouseEvent e) 
            {
                   //System.out.println(e.getPoint());
                    for(int i = 0; i < paintedRectangles.size(); i++)
                    {
                        if(paintedRectangles.get(i).contains(e.getPoint()))
                        {
                            //This will have to change in the future just testing it right now
                            paintedRectangles.get(i).setRecVal(PathRectangle.recType.WALL);
                            
                            
                            repaint();
                        }
                    }
            }
        });

添加 MouseListener

addMouseListener(new MouseAdapter()
        {
           
            @Override
            public void mousePressed(MouseEvent e) {
                
                for(int i = 0; i < paintedRectangles.size(); i++)
                    {
                        if(paintedRectangles.get(i).contains(e.getPoint()))
                        {
                            //This will have to change in the future just testing it right now
                            paintedRectangles.get(i).setRecVal(PathRectangle.recType.WALL);
                            
                            
                            repaint();
                        }
                    }
            }
        });

paintComponent 方法

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

        Graphics2D g2D = (Graphics2D) g;

        for(int i = 0; i < rows; i++)
        {
            for(int j = 0; j < cols; j++)
            {
                
                switch(paintedRectangles.get(i).getRecVal())
                {
                    case UNDECLARED:
                    g2D.drawRect(i*rectLenWidSize, j*rectLenWidSize, rectLenWidSize, rectLenWidSize);
                    break;

                    case WALL:
                    g2D.setColor(Color.BLACK);
                    g2D.fillRect(i*rectLenWidSize, j * rectLenWidSize, rectLenWidSize, rectLenWidSize);
                    break;

                    case STARTNODE:
                    g2D.setColor(Color.GREEN);
                    g2D.fillRect(i*rectLenWidSize, j*rectLenWidSize, rectLenWidSize, rectLenWidSize);
                    break;

                    case ENDNODE:
                    g2D.setColor(Color.RED);
                    g2D.fillRect(i*rectLenWidSize, j*rectLenWidSize, rectLenWidSize, rectLenWidSize);
                    break;
                }
            }
        }
    }

要执行此类操作,您将需要二维 ArrayList。一维 ArrayList 不能正确表示网格。

我还注意到我在第一次初始化 ArrayList 时犯了几个错误

我的初始化矩形列表

for(int i = 0; i < 10; i++)
        {
            ArrayList<PathRectangle> initColList = new ArrayList<PathRectangle>();

            for(int j = 0; j < 10; j++)
            {
                initColList.add(new PathRectangle(i*49, j*49, 49, 49));
            }

            initList.add(initColList);
        }

paintComponent方法

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    
    Graphics2D g2D = (Graphics2D) g;

    for(int i = 0; i < paintedRectangles.size(); i++)
    {
        for(int j = 0; j < paintedRectangles.get(0).size(); j++)
        {
            
            //System.out.println(paintedRectangles.get(i).get(j).getRecVal());
            switch(paintedRectangles.get(i).get(j).getRecVal())
            {
                case UNDECLARED:
                g2D.drawRect(i*rectLenWidSize, j*rectLenWidSize, rectLenWidSize, rectLenWidSize);
                break;

                case WALL:
                g2D.setColor(Color.BLACK);
                g2D.fillRect(i*rectLenWidSize, j * rectLenWidSize, rectLenWidSize, rectLenWidSize);
                break;

                case STARTNODE:
                g2D.setColor(Color.GREEN);
                g2D.fillRect(i*rectLenWidSize, j*rectLenWidSize, rectLenWidSize, rectLenWidSize);
                break;

                case ENDNODE:
                g2D.setColor(Color.RED);
                g2D.fillRect(i*rectLenWidSize, j*rectLenWidSize, rectLenWidSize, rectLenWidSize);
                break;
            }
        }
    }
}