java游戏编程中飞机的运动不流畅一卡一卡的。请教一下该如何解决
paint()函数绘制的频率我也给了很多遍。上缓冲我也用了。。卫生么还不行呢 --------------------编程问答-------------------- 那就是你的代码效率太低了,你可以把你的代码贴上来大家看看啊。 --------------------编程问答-------------------- 代码太多。写的也没注释。我就说一下过程吧线程我没用很多。。有一个敌方飞机产生的类实现了runnable接口格随机产生低级。
ArrayList<Smallplane> smallplanes;
public EnemyFactory(ArrayList<Smallplane> smallplanes) {
this.smallplanes=smallplanes;
}
public void run() {
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int y=new Random().nextInt(10)*1000;
if(y>8800)
{
int x=new Random().nextInt(40)*10;
Smallplane pl=new Smallplane(x, 0);
smallplanes.add(pl);
}
}
}
下面是绘制的函数
继承panel
public void paint(Graphics g)
{
super.paint(g);
dbimg=createImage(this.WIDTH,this.HEIGHT);
dbg=dbimg.getGraphics();
paintComponent(dbg);
g.drawImage(dbimg,0,0,this);
//g.dispose();
}
public void paintComponent(Graphics g)
{
//
ImageIcon img=ImageFactory.getBackgroundImage();
g.drawImage(img.getImage(), 0, 0, 479,853, null);
for(int i=0;i<myplane.paos.size();i++)
{
Pao pao=myplane.paos.get(i);
if(pao.isHitSmallplane(smallplanes))
{
myplane.paos.remove(pao);
}
else {
pao.drawpao(g);
}
}
myplane.drawMyplane(g);
for(int i=0;i<baozhas.size();i++)
{
baozha bao=baozhas.get(i);
if(!bao.islive)
{
baozhas.remove(bao);
}
else
{
bao.drawbaozha(g);
}
}
for(int i=0;i<smallplanes.size();i++)
{
// System.out.println(smallplanes.size());
Smallplane sp=smallplanes.get(i);
if(sp.islive==false)
{
smallplanes.remove(sp);
}
else {
sp.drawEmenyPlane(g);
}
}
}
@Override
public void run() {
while(true)
{
//
try {
Thread.sleep(100);
this.repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} --------------------编程问答-------------------- drawImage很慢的好吧 --------------------编程问答-------------------- 不drawimage那给如何画飞机呀 --------------------编程问答--------------------
图片按区域截图就可以了 --------------------编程问答-------------------- 就是按区域截图呀不然怎么画飞机呀 --------------------编程问答-------------------- 要draw的东西都先在bufferedimage里画进去,在paint里用dwaw一次过画出来 --------------------编程问答-------------------- 楼主可以试下设置个缓冲流
补充:Java , Java SE