如何在JPanel中重绘小部分绘图?

时间:2017-10-04 18:54:58

标签: java swing graphics2d

我有JPanelpaintComponent方法绘制表格:

public class PondPanel extends JPanel implements Observer{
    private int numberfRows = 0;
    private int numberfColumns = 0;
    private Dimension size;

    public PondPanel() {
        super();

        size = new Dimension(500, 500);
        setPreferredSize(size);
        setBackground(Color.WHITE);
    }

    /*
     * Draws initial board without characters.
     * 
     * (non-Javadoc)
     * 
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.LIGHT_GRAY);

        if (numberfColumns == 0 || numberfRows == 0) {
            return;
        }
        int cellWidth = size.width / numberfColumns;
        int cellHeight = size.height / numberfRows;

        // drawing initial board
        for (int i = 0; i < numberfRows; i++) {
            for (int j = 0; j < numberfColumns; j++) {
                int y = i * cellHeight;
                int x = j * cellWidth;
                g.drawRect(x, y, cellWidth, cellHeight);
            }
        }
    }

我需要方法只更新此绘图的一部分。其实我想这样做:

g.setColor(color);
        g.fillRect(x * width, y * height, width, height);

这就是全部。我不想致电repaint()paintComponent投放。我只想在paintComponent中的绘图中重新绘制一个矩形。

0 个答案:

没有答案