简体   繁体   English

paintComponent() 没有正确绘制矩形

[英]paintComponent() not painting rectangles correctly

I have a painted grid of custom rectangles inside a JPanel.我在 JPanel 中有一个自定义矩形的绘制网格。 I have rectangles stored in a list of rectangles.我将矩形存储在矩形列表中。 I have added a MouseMotionListener and a MouseListener to the JPanel;我在 JPanel 中添加了一个 MouseMotionListener 和一个 MouseListener; Both of these components will listen to either the users click or their click and hold, find out the position of where the mouse reticle is, loop through my list of rectangles and update its enum value accordingly.这两个组件都会监听用户单击或单击并按住,找出鼠标标线所在位置的 position,循环遍历我的矩形列表并相应地更新其枚举值。 I then call repaint() to redraw the rectangles in the list on JPanel.然后我调用 repaint() 来重绘 JPanel 列表中的矩形。 The only thing is that on repainting the list, instead of just painting that one rectangle, it paints the entire column.唯一的问题是,在重新绘制列表时,它不仅绘制了一个矩形,还绘制了整个列。 Please Help.请帮忙。

在此处输入图像描述

My Initialization of the initial rectangle list我的初始矩形列表的初始化

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);

My Rectangle Class我的矩形 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;
    }


}

Adding Mouse Motion Listener添加鼠标运动监听器

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();
                        }
                    }
            }
        });

Adding 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 method油漆组件方法

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;
                }
            }
        }
    }

To do something like this you are going to need two-dimensional ArrayLists.要执行此类操作,您将需要二维 ArrayList。 One-dimensional ArrayLists don't represent the Grid correctly.一维 ArrayList 不能正确表示 Grid。

I also noticed I made a couple mistakes when making the first initialization ArrayList我还注意到我在第一次初始化 ArrayList 时犯了几个错误

My Initialization of the initialization of the rectangle list我的初始化矩形列表的初始化

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 method油漆组件方法

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;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM