我有JPanel
用paintComponent
方法绘制表格:
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
中的绘图中重新绘制一个矩形。