import java.awt.*;
import javax.swing.JPanel;
class TetrisPanel extends JPanel implements Runnable//绘图线程类
{
public int ypos=-80;//小球左上角的纵坐标
//在类中添加如下两个私有成员
private Image iBuffer;
private Graphics gBuffer;
public TetrisPanel()
{
//创建一个新线程
Thread t=new Thread(this);
//启动线程
t.start();
}
public void paint(Graphics g)//重载绘图方法
{
if(iBuffer==null)
{
iBuffer=createImage(this.getWidth(),this.getHeight());
gBuffer=iBuffer.getGraphics();
}
gBuffer.setColor(getBackground());
gBuffer.fillRect(0,0,this.getWidth(),this.getHeight());
gBuffer.setColor(Color.RED);
gBuffer.fillOval(90,ypos,80,80);
//
g.drawImage(iBuffer,0,0,this);
}
public void update(Graphics g)
{
paint(g);
}
public void run()//重载run()方法
{
while(true)//线程中的无限循环
{
try{
Thread.sleep(30);//线程休眠30ms
}catch(InterruptedException e){}//抛出异常
ypos+=5;//修改小球左上角的纵坐标
if(ypos>300)//小球离开窗口后重设左上角的纵坐标
{
ypos=-80;
}
repaint();//窗口重绘
}
}
}
1、paint()方法能不能实现双缓冲?2、paint()方法中有很多this,每个this都代表什么?3、update()方法中paint(g)画的是什么?