简体   繁体   中英

PaintComponent disrupting grid drawing

I'm new to Java as well as user interfaces, and I have a problem with Java Graphics. What I'm trying to achieve is drawing a grid on a JPanel, and then paint custom components into the grid.

Here is the class I want to draw the grid on (its base extends JPanel).

public class RectGridPanel extends GridPanel
{

    List<Rectangle> rects;  
    
    public RectGridPanel(Simulator sim)
    {
        super(sim);
        
        this.rects = new ArrayList<Rectangle>();        
        
        this.setLayout(new GridLayout(20,20));
        
        for(int x = 1; x < 801; x += 40)
        {
            for(int y = 2; y < 801; y += 40)
            {
                Cell newCell = new RectCell(x, y, sim);
                this.add(newCell);
            }
        }
        
    }
    
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(Color.BLACK);
        for(int x = 1; x < 801; x += 40)
        {
            for(int y = 2; y < 801; y += 40)
            {
                Rectangle rect = new Rectangle(x, y, 40, 40);
                g2.draw(rect);
                rects.add(rect);
            }
        }
    }
}

Here are the cells I want to draw inside the grid:

public class RectCell extends Cell
{
    Rectangle shape;
    
    public RectCell(int x, int y, Simulator sim)
    {
        super(x, y, sim);
        shape = new Rectangle(x, y, CELL_SIZE, CELL_SIZE);
    }
    
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(Color.BLACK);
        g2.fill(shape);
    }
}

So the grid on its own draws out fine, but as soon as I try to create cells inside it it gets disrupted like that: 在此处输入图像描述

It is not clear what you try to achive. What is the reason for RectCell? Why not paint the shapes direct in the RectGridPanel? The nested loop in the paintComponent it not a good idea. Be aware that the paintComponent is called very often and every time you in crease the rects list with new objects. The former painted rectangles get lost (qraphicaly) and new rectangles with the same parameters are blowing the list.

public class RectCell extends Cell
{
    Rectangle shape;
    
    public RectCell(int x, int y, Simulator sim)
    {
        super(x, y, sim);
        shape = new Rectangle(x, y, CELL_SIZE, CELL_SIZE);
    }
    
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(Color.BLACK);
        g2.fill(shape);
    }
}

The Graphics object that is passed to this paintComponent() function defines the screen space where the current RectCell can draw. The coordinate sysetem is relative to the upper left corner of the RectCell , so drawing the background should always start at x = 0 and y = 0 or some other meaningful value. The coordinates here are not relative to the parent component as you seem to assume.

Better yet, set the background color and let Swing take care of painting it rather than painting a rectangle yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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