调用repaint()后为什么这个paintComponent()没有执行
import javax.swing.*;
import java.awt.*;
public class BoxCanvas extends JPanel
{
private Color foreground = Color.ORANGE;
private Color background = Color.DARK_GRAY;
private Box[][] boxes;
private int row;
private int column;
private int totalScore = 0; //the total score from the beginning of game on
private int scoreForCurrentLevel = 0;
// the total score from the beginning of current level on
private int boxWidth, boxHeight;
/**
* construct a canvas by specified row and column
* @param row int. the row of canvas
* @param column int. the column of canvas
* */
public BoxCanvas(int row, int column)
{
this.row = row;
this.column = column;
boxes = new Box[this.row][this.column];
for(int i = 0; i < boxes.length; i++)
for(int j = 0; j < boxes[i].length; j++)
boxes[i][j] = new Box(false);
this.setBorder(RussanSquare.DEFAULT_BORDER);
boxWidth = this.getSize().width / this.column;
boxHeight = this.getSize().height / this.row;
}
/**
* to get the foreground of canvas
* @return foreground of canvas
* */
public Color getForeground()
{
return this.foreground;
}
/**
* to get the background of canvas
* @return background of canvas
* */
public Color getBackground()
{
return this.background;
}
/**
* to get the row of canvas
* @return row of canvas
* */
public int getRow()
{
return this.row;
}
/**
* to get the column of canvas
* @return column of canvas
* */
public int getColumn()
{
return this.column;
}
/**
* to get the total score from the beginning of game on
* @return total score
* */
public int getScore()
{
return this.totalScore;
}
/**
* to get the total score from the beginning of current level game on
* @return total score for current level
* */
public int getScoreForCurrentLevel()
{
return this.scoreForCurrentLevel;
}
/**
* to get the reference of the box in the specified row and column
* @return refernce of box in the specified row and column. null is returned if the specified
* row or column is invalid
* */
public Box getBoxIn(int row, int column)
{
Box box = null;
if((row < 0 ) || (row >= this.row) || (column < 0) || (column >= this.column))
{
//do nothing but null will be returned
}
else
box = boxes[row][column];
return box;
}
/**
* to check whether there is a full line in canvas or not
* @return row of the full line. -1 indicates there is not a full line
* */
public int checkFullLine()
{
int row, column;
for(row = 0; row < boxes.length; row++)
{
for(column = 0; column < boxes[row].length; column++)
//if there is a box is nucolored then check the next row
if(boxes[row][column].isColored() == false) break;
//if all the boxes in this row is colored then this is a full line
if(column >= boxes[row].length) break;
}
if(row >= boxes.length) row = 0;
return (row - 1);
}
/**
* to remove the full line and modify the score
* @param row. the number of which line is full
* */
public synchronized void removeFullLine(int row)
{
for(int i = row; i >= 1; i--)
for(int j = 0; j < boxes[row].length; j++)
boxes[i][j] = (Box)boxes[i - 1][j].clone();
//modify the score
this.totalScore += RussanSquare.SCORE_PER_LINE;
this.scoreForCurrentLevel += RussanSquare.SCORE_PER_LINE;
//repaint the canvas
this.repaint();
}
/**
* to customize the paintComponent() method
* */
public void paintComponent(Graphics g)
{
System.out.println("In BoxCanvas paintComponent()");
super.paintComponent(g);
g.setColor(this.foreground);
for(int i = 0; i < boxes.length; i++)
for(int j = 0; j < boxes[i].length; j++)
{
if(boxes[i][j].isColored()) System.out.println("In BoxCanvas paintComponent()");
//if current box is colored then show this box by foreground of canvas
g.setColor(boxes[i][j].isColored() ? this.foreground : this.background);
g.fill3DRect(j * boxWidth, i * boxHeight,
boxWidth, boxHeight, true);
}
}
/**
* to detect whether game is over or not
* @return true indicates the game is over
* */
public boolean isGameOver()
{
for(int i = 0; i < this.column; i++)
if(boxes[0][i].isColored()) return true;
return false;
}
/**
* to reset the canvas and score
* */
public void reset()
{
this.totalScore = 0;
this.scoreForCurrentLevel = 0;
for(int i = 0; i < boxes.length; i++)
for(int j = 0; j < boxes[i].length; j++)
boxes[i][j].setColor(false);
//repaint the canvas
repaint();
}
/**
* a flash when game is over
* */
public void gameOver()
{
for(int i = boxes.length - 1; i >= 0; i--)
for(int j = boxes[i].length - 1; j>= 0; j--)
boxes[i][j].setColor(true);
this.repaint();
for(int i = 0; i < boxes.length; i++)
for(int j = 0; j < boxes[i].length; j++)
boxes[i][j].setColor(false);
this.repaint();
}
public static void main(String[] args) {
}
}