当前位置: 代码迷 >> Java相关 >> 双缓冲问题
  详细解决方案

双缓冲问题

热度:325   发布时间:2007-01-16 15:47:23.0
双缓冲问题

我用双缓冲写了这个程序,内容很简单,就是有一个小球,碰到壁就反弹,问题是,运行起来好象并不畅顺,虽然并不会产生闪烁的感觉,
不知道是程序问题还是电脑问题,代码如下,
package new_test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class animation extends JFrame implements Runnable,KeyListener
{
Graphics gr;
Image i;
int x=0,y=0,a=1,b=1,w,h;
public animation()
{
h=Toolkit.getDefaultToolkit() .getScreenSize().height;
w=Toolkit.getDefaultToolkit() .getScreenSize().width;
this.setUndecorated(true);
GraphicsDevice gra=GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
gra.setFullScreenWindow(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(this);
this.setVisible(true);

}
public void keyPressed(KeyEvent e)
{
System.exit(0);
}
public void keyReleased(KeyEvent e)
{

}
public void keyTyped(KeyEvent e)
{

}
public void run()
{
while(true)
{
x=x+a;
y=y+b;
if(x>w)
{
a=-1;
}else if(x<0)
{
a=1;
}else if(y>h)
{
b=-1;
}else if(y<0)
{
b=1;
}
try{
Thread.sleep(2);
}catch(Exception e)
{}
//System.out.print(x);
//System.out.println(" "+y);
repaint();
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
// super.paint(g);
i=createImage(w,h) ;
gr=i.getGraphics();
gr.setColor(Color.black);
gr.drawRect(0,0,w,h);
gr.setColor(Color.BLUE);
gr.fillOval(x,y,100,100);
// g.drawString("Hello",x,y);
g.drawImage(i,0,0,this);
//g.drawOval(x,y,10,10);
}
public static void main(String[] args) {
animation a= new animation();
new Thread(a).start();
}
}
还有,我有一个问题,就是,如果用多线程的话,好像上边那个程序,会搞到CPU的占用率很大,应该是程序本身设计问题吧,
大家请指点一下吗,谢谢了~

搜索更多相关的解决方案: import  awt  缓冲  java  

----------------解决方案--------------------------------------------------------

最好是继承自JPanel

因为JPanel是实现了双缓冲的轻量级组件,而JFrame不是


----------------解决方案--------------------------------------------------------

我都已经跟你说的那样用JPanel实现了双缓冲,不过好像还是这样
我修改了代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class animation extends JPanel implements Runnable,KeyListener
{
Graphics gr;
Image i;
int x=0,y=0,a=1,b=1,w,h;
public animation()
{
h=Toolkit.getDefaultToolkit() .getScreenSize().height;
w=Toolkit.getDefaultToolkit() .getScreenSize().width;
this.addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{
System.exit(0);
}
public void keyReleased(KeyEvent e)
{

}
public void keyTyped(KeyEvent e)
{

}
public void run()
{
while(true)
{
x=x+a;
y=y+b;
if(x>w)
{
a=-1;
}else if(x<0)
{
a=1;
}else if(y>h)
{
b=-1;
}else if(y<0)
{
b=1;
}
try{
Thread.sleep(2);
}catch(Exception e)
{}
//System.out.print(x);
//System.out.println(" "+y);
repaint();
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
// super.paint(g);
i=createImage(w,h) ;
gr=i.getGraphics();
gr.setColor(Color.black);
gr.drawRect(0,0,w,h);
gr.setColor(Color.BLUE);
gr.fillOval(x,y,100,100);
// g.drawString("Hello",x,y);
g.drawImage(i,0,0,this);
//g.drawOval(x,y,10,10);
}
public static void main(String[] args)
{
JFrame f=new JFrame() ;
animation a= new animation();
f.setUndecorated(true);
GraphicsDevice gra=GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
gra.setFullScreenWindow(f);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(a) ;
f.show() ;new Thread(a).start();
}
}
麻烦你了~谢谢了


----------------解决方案--------------------------------------------------------
在1。5就不用自己去实现双缓冲了
用paintComponent
----------------解决方案--------------------------------------------------------

韩峰说得对

继承自JPanel后,就应该重写paintComponent,而不能重写paint


----------------解决方案--------------------------------------------------------

谢谢千里冰封和韩峰两位了,问题已经解决了


----------------解决方案--------------------------------------------------------
  相关解决方案