在JPanel上绘制的网格不完全可见

时间:2019-01-01 11:52:28

标签: java swing

我有一个面板,其中extends JPanelOverride paintComponent(Graphics g)绘制了一些正方形。

public class Painter extends JPanel{

    private static final Color orange = new Color(225, 95, 0);

    private Block[][] blocks;
    private int sizeBlock;

    public Painter() {
        this.setBackground(Color.WHITE);
    }

    public void setBlocks(Block[][] blocks) {   
        this.blocks = blocks;   
    }

    public void setSizeBlock(int size) {    
        this.sizeBlock = size;  
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        for(Block[] cols : blocks) {
            for(Block block : cols) {

                switch(block.getType()) {

                    case Block.wall: {

                        paintBlockLineBorder(g, block.getX()*(sizeBlock-1),
                                                block.getY()*(sizeBlock-1), orange);
                        break;

                    }

                    default: {break;}
                }           
            }       
        }       
    }

    private void paintBlockLineBorder(Graphics g, int x, int y, Color color) {

        //background
        g.setColor(color);
        g.fillRect(x, y, sizeBlock, sizeBlock);

        //border
        g.setColor(Color.BLACK);
        g.drawRect(x, y, sizeBlock-1, sizeBlock-1);

    }
}

我将此JPanel(Painter)添加到其他JPanel(painterPanel)中,并将其添加到JFrame中,其布局为GridBagLayout

结果不是我想要的:

这就是我想要的:

以下是一个重现问题的用例:

public static void main(String[] args) {

    JFrame mainFrame = new JFrame();
    mainFrame.setLayout(new GridBagLayout());
    mainFrame.setBounds(100, 100, 500, 400);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.fill = GridBagConstraints.NONE;
    gbc.gridx = 0; gbc.gridy = 0;
    gbc.weighty = 1; gbc.weightx = 1;

    JPanel painterPanel = new JPanel();
    painterPanel.setBorder(BorderFactory.createLineBorder(Color.black));

    mainFrame.add(painterPanel, gbc);

    Painter apainter = new Painter();

    painterPanel.add(apainter);

    Block[][] ablock = new Block[2][2];
    ablock[0][0] = new Block(0, 0, Block.wall);
    ablock[0][1] = new Block(0, 1, Block.wall);
    ablock[1][0] = new Block(1, 0, Block.wall);
    ablock[1][1] = new Block(1, 1, Block.wall);

    apainter.setBlocks(ablock);
    apainter.setSizeBlock(25);

    apainter.repaint();

    mainFrame.setVisible(true);     
}

这是块类:

public class Block {

    public static final String wall = "wall";

    private int x;
    private int y;
    private String type;

    public Block(int x, int y, String type) {

        this.x = x;
        this.y = y;
        this.type = type;
    }

    //getters & setters
}

为什么我的Painter不能完全显示?

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main{

    public static void main(String[] args) throws InterruptedException{

            JFrame mainFrame = new JFrame();

            Block[][] ablock = new Block[2][2];
            ablock[0][0] = new Block(0, 0, Block.wall);
            ablock[0][1] = new Block(0, 1, Block.wall);
            ablock[1][0] = new Block(1, 0, Block.wall);
            ablock[1][1] = new Block(1, 1, Block.wall);

            Painter apainter = new Painter();
            mainFrame.add(apainter);
            apainter.setBlocks(ablock);
            apainter.setSizeBlock(25);

            mainFrame.pack();
            mainFrame.setVisible(true);
    }
}

class Painter extends JPanel{

    private static final Color orange = new Color(225, 95, 0);

    private Block[][] blocks;
    private int sizeBlock;

    public Painter() {

       setBackground(Color.WHITE);
       setBorder(BorderFactory.createLineBorder(Color.black));
       setPreferredSize(new Dimension(400, 300));
    }

    public void setBlocks(Block[][] blocks) {

        this.blocks = blocks;
    }

    public void setSizeBlock(int size) {
        sizeBlock = size;
        //you may want to update Preferred Size
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        for(Block[] cols : blocks) {
            for(Block block : cols) {

                switch(block.getType()) {

                    case Block.wall: {

                        paintBlockLineBorder(g, block.getX()*(sizeBlock-1),
                                                block.getY()*(sizeBlock-1), orange);
                        break;
                    }

                    default: {break;}

                }
            }
        }
    }

    private void paintBlockLineBorder(Graphics g, int x, int y, Color color) {

        //background
        g.setColor(color);
        g.fillRect(x, y, sizeBlock, sizeBlock);

        //border
        g.setColor(Color.BLACK);
        g.drawRect(x, y, sizeBlock-1, sizeBlock-1);

    }
}

class Block {

    public static final String wall = "wall";

    private final int x;
    private final int y;
    private final String type;

    public Block(int x, int y, String type) {

        this.x = x;
        this.y = y;
        this.type = type;
    }

    int getX() {return x;   }
    int getY() {return y;   }
    String getType() {  return type;}
}

请随时根据需要进行澄清。
还可以考虑使用GridLayout来构建网格:1 2

答案 1 :(得分:-1)

@CarlosHeuberger 的建议下,我重写了getPreferredSizegetMinimumSizegetMaximumSize,它们返回的Dimension的值基于{{ 1}}和值blocks