调用repaint()后为什么这个paintComponent()没有执行

fjdtsm 2004-08-11 05:08:49
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) {
}
}
...全文
245 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang21cnboy 2004-08-12
  • 打赏
  • 举报
回复
paintComponents(Graphics g)
fjdtsm 2004-08-12
  • 打赏
  • 举报
回复
还是不行.
我想问题可能跟我在
http://community.csdn.net/Expert/topic/3266/3266069.xml?temp=.514538
遇到的问题是一样的.
在那里我是特意为是试验一下如何在JPanel中画矩形而写的.可是不知道为什么就是没办法画出我想要的图形,甚至是一个画图的动作一点反应也没有.
而我昨天写的另一个在JPanel中画单独一个矩形的程序又工作得很正常.可以按我的需要画出矩形来.
ztc16627 2004-08-11
  • 打赏
  • 举报
回复
关注中...
bineon 2004-08-11
  • 打赏
  • 举报
回复
repaintUI这个比较好
rainight 2004-08-11
  • 打赏
  • 举报
回复
用paint取代paintComponent,将代码挪到paint中

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧