调用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) {
}
}
...全文
246 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中
Java在窗口上加载显示GIF动画图像,将多个独立的GIF图像串联在一起显示,形成GIF特有的动画形式。主要代码如下:   ImageIcon[] images; //用于动画的图标数组   Timer animationTimer;   int currentImage = 0; //当前图像编号   int delay = 500; //图像切换延迟   int width; //图像宽度   int height; //图像高度   public AnimatorIcon() //构造函数   {    setBackground(Color.white);    images = new ImageIcon[2]; //初始化数组    for (int i=0;i   images[i]=new ImageIcon(getClass().getResource("image" i ".gif")); //实例化图标    width = images[0].getIconWidth(); //初始化宽度值    height = images[0].getIconHeight(); //初始化高度值   }   public void paintComponent(Graphics g) { //重载组件绘制方法    super.paintComponent(g); //调用父类函数    images[currentImage].paintIcon(this,g,70,0); //绘制图标    currentImage=(currentImage 1)%2; //更改当前图像编号   }   public void actionPerformed(ActionEvent actionEvent) {    repaint();   }   public void startAnimation() { //开始动画    if (animationTimer==null) {    currentImage=0;    animationTimer=new Timer(delay, this); //实例化Timer对象    animationTimer.start(); //开始运行    } else if (!animationTimer.isRunning()) //如果没有运行    animationTimer.restart(); //重新运行   }   public void stopAnimation() {    animationTimer.stop(); //停止动画   }   public static void main(String args[]) {    AnimatorIcon animation = new AnimatorIcon(); //实例化动画图标    JFrame frame = new JFrame("动画图标"); //实例化窗口对象    frame.getContentPane().add(animation); //增加组件到窗口上    frame.setSize(200, 100); //设置窗口尺寸    frame.setVisible(true); //设置窗口可视    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序    animation.startAnimation(); //开始动画
java项目-俄罗斯方块,供学习。 /** * File: GameCanvas.java * User: Administrator * Date: Dec 15, 2003 * Describe: 俄罗斯方块的每一个方块的绘制 */ import javax.swing.*; import javax.swing.border.EtchedBorder; //EtchedBorder为swing包中的突出或凹进的边框 import java.awt.*; /** * 画布类,内有<行数> * <列数>个方格类的实例。 * 继承JPanel类。 * ErsBlock线程类动态改变画布类的方格颜色,画布类通过 * 检查方格颜色来体现ErsBlock块的移动情况。 */ class GameCanvas extends JPanel { //默认的方块的颜色为桔黄色,背景颜色为黑色 private Color backColor = Color.black, frontColor = Color.orange; private int rows, cols, score = 0, scoreForLevelUpdate = 0; private ErsBox[][] boxes; private int boxWidth, boxHeight; //score:得分,scoreForLevelUpdate:上一次升级后的积分 /** * 画布类的第一个版本的构造函数 * @param rows int, 画布的行数 * @param cols int, 画布的列数 * 行数和列数以方格为单位,决定着画布拥有方格的数目 */ public GameCanvas(int rows, int cols) { this.rows = rows; this.cols = cols; //初始化rows*cols个ErsBox对象 boxes = new ErsBox[rows][cols]; for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { boxes[i][j] = new ErsBox(false); } } //设置画布的边界 setBorder(new EtchedBorder( EtchedBorder.RAISED, Color.white, new Color(148, 145, 140))); } /** * 画布类的第二个版本的构造函数 * @param rows 与public GameCanvas(int rows, int cols)的rows相同 * @param cols 与public GameCanvas(int rows, int cols)的cols相同 * @param backColor Color, 背景色 * @param frontColor Color, 前景色 */ public GameCanvas(int rows, int cols, Color backColor, Color frontColor) { this(rows, cols); //调用第一个版本的构造函数 this.backColor = backColor; this.frontColor = frontColor; //通过参数设置背景和前景颜色 } /** * 设置游戏背景色彩 * @param backColor Color, 背景色彩 */ public void setBackgroundColor(Color backColor) { this.backColor = backColor; } /** * 取得游戏背景色彩 * @return Color, 背景色彩 */ public Color getBackgroundColor() { return backColor; } /** * 设置游戏方块色彩 * @param frontColor Color, 方块色彩 */ public void setBlockColor(Color frontColor) { this.frontColor = frontColor; } /** * 取得游戏方块色彩 * @return Color, 方块色彩 */ public Color getBlockColor() { return frontColor; } /** * 取得画布中方格的行数 * @return int, 方格的行数 */ public int getRows() { return rows; } /** * 取得画布中方格的列数 * @return int, 方格的列数 */ public int getCols() { return cols; } /** * 取得游戏成绩 * @return int, 分数 */ public int getScore() { return score; } /** * 取得自上一次升级后的积分 * @return int, 上一次升级后的积分 */ public int getScoreForLevelUpdate() { return scoreForLevelUpdate; } /** * 升级后,将上一次升级以来的积分清0 */ public void resetScoreForLevelUpdate() { scoreForLevelUpdate -= ErsBlocksGame.PER_LEVEL_SCORE; } /** * 得到某一行某一列的方格引用。 * @param row int, 要引用的方格所在的行 * @param col int, 要引用的方格所在的列 * @return ErsBox, 在row行col列的方格的引用 */ public ErsBox getBox(int row, int col) { if (row < 0 || row > boxes.length - 1 || col < 0 || col > boxes[0].length - 1) return null; return (boxes[row][col]); } /** * 覆盖JComponent类的函数,画组件。 * @param g 图形设备环境 * paint方法实际上把绘图的主要工作委派给paintComponent方法等方法 */ public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(frontColor); for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { //用前景颜色或背景颜色绘制每个方块 g.setColor(boxes[i][j].isColorBox() ? frontColor : backColor); g.fill3DRect(j * boxWidth, i * boxHeight, boxWidth, boxHeight, true); } } } /** * 根据窗口的大小,自动调整方格的尺寸 */ public void fanning() { boxWidth = getSize().width / cols; boxHeight = getSize().height / rows; } /** * 当一行被游戏者叠满后,将此行清除,并为游戏者加分 * @param row int, 要清除的行,是由ErsBoxesGame类计算的 */ public synchronized void removeLine(int row) { for (int i = row; i > 0; i--) { for (int j = 0; j < cols; j++) boxes[i][j] = (ErsBox) boxes[i - 1][j].clone(); } score += ErsBlocksGame.PER_LINE_SCORE; scoreForLevelUpdate += ErsBlocksGame.PER_LINE_SCORE; repaint(); } /** * 重置画布,置积分为0 */ public void reset() { score = 0; scoreForLevelUpdate = 0; for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) boxes[i][j].setColor(false); } repaint(); } }
Java基础篇中的StillClock类用于画出时钟以方便Java进阶篇P228第二十九章多线程(第八版)ClockWithAudio类的调用。 package 多线程练习; import java.awt.*; import javax.swing.*; import java.util.*; public class StillClock extends JPanel { private int hour; private int minute; private int second; /** Construct a default clock with the current time */ public StillClock() { setCurrentTime(); } /** Construct a clock with specified hour, minute, and second */ public StillClock(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } /** Return hour */ public int getHour() { return hour; } /** Set a new hour */ public void setHour(int hour) { this.hour = hour; repaint(); } /** Return minute */ public int getMinute() { return minute; } /** Set a new minute */ public void setMinute(int minute) { this.minute = minute; repaint(); } /** Return second */ public int getSecond() { return second; } /** Set a new second */ public void setSecond(int second) { this.second = second; repaint(); } /** Draw the clock */ protected void paintComponent(Graphics g) { super.paintComponent(g); // Initialize clock parameters int clockRadius = (int) (Math.min(getWidth(), getHeight()) * 0.8 * 0.5); int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; // Draw circle g.setColor(Color.black); g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius); g.drawString("12", xCenter - 5, yCenter - clockRadius + 12); g.drawString("9", xCenter - clockRadius + 3, yCenter + 5); g.drawString("3", xCenter + clockRadius - 10, yCenter + 3); g.drawString("6", xCenter - 3, yCenter + clockRadius - 3); // Draw second hand int sLength = (int) (clockRadius * 0.8); int xSecond = (int) (xCenter + sLength * Math.sin(second * (2 * Math.PI / 60))); int ySecond = (int) (yCenter - sLength * Math.cos(second * (2 * Math.PI / 60))); g.setColor(Color.red); g.drawLine(xCenter, yCenter, xSecond, ySecond); // Draw minute hand int mLength = (int) (clockRadius * 0.65); int xMinute = (int) (xCenter + mLength * Math.sin(minute * (2 * Math.PI / 60))); int yMinute = (int) (yCenter - mLength * Math.cos(minute * (2 * Math.PI / 60))); g.setColor(Color.blue); g.drawLine(xCenter, yCenter, xMinute, yMinute); // Draw hour hand int hLength = (int) (clockRadius * 0.5); int xHour = (int) (xCenter + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); int yHour = (int) (yCenter - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); g.setColor(Color.BLACK); g.drawLine(xCenter, yCenter, xHour, yHour); } public void setCurrentTime() { // Construct a calendar for the current date and time Calendar calendar = new GregorianCalendar(); // Set current hour, minute and second this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); } public Dimension getPreferredSize() { return new Dimension(200, 200); } }

62,614

社区成员

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

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