我想画两个物体,一个从屏幕上往下移动,一个从左往右移动,可是闪的厉害~
你们帮我看看代码,我实现不了双缓冲!囧!!!
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class step1 extends Applet implements Runnable{
Thread thread1 ,thread2;
int y1=-100,y2=-50;
Image img[]=new Image[2];
public void init()
{
Image imageBuffer;                              //这里问题比较大
imageBuffer=createImage(getWidth(),getHeight());//  对吗你们帮我
Graphics imgobject=imageBuffer.getGraphics();
paint(imgobject);                                // 看看      setSize(800,600);
img[0]=getImage(getCodeBase(),"1.png");
img[1]=getImage(getCodeBase(),"2.png");
}
public step1()
{

setBackground(Color.WHITE);
thread1=new Thread(this);
thread2=new Thread(this);
thread1.start();
thread2.start();
}
public void paint(Graphics g)
{
//Graphics2D img2D=(Graphics2D)g;
if(thread1!=null)
{
g.drawImage(img[0], y1, 50, this);
}
if(thread2!=null)
{
g.drawImage(img[1],50,y2,115,y2+50,0,0,65,50,this);
}
}
public void run()
{
while(true)
{
if(Thread.currentThread()==thread1)
{
y1+=6;
if(y1>=400)
y1=-100;
repaint();
pause(100);
}
else if(Thread.currentThread()==thread2)
{
y2+=4;
if(y2>=400)
y2=-50;
repaint();
pause(120);
}
}
}
private void pause(int time)
{
try
{
Thread.sleep(time);
}catch(InterruptedException e){}
}}

解决方案 »

  1.   

    public void paint(Graphics g) {
        Image imageBuffer = createImage(getWidth(), getHeight());
        Graphics imgobject = imageBuffer.getGraphics();
    if(thread1!=null)
    {
    imgobject.drawImage(img[0], y1, 50, this);
    }
    if(thread2!=null)
    {
    imgobject.drawImage(img[1],50,y2,115,y2+50,0,0,65,50,this);
    }    g.drawImage(0,0,imageBuffer);
    }
      

  2.   

    public void paint(Graphics g) {
        Image imageBuffer = createImage(getWidth(), getHeight());
        Graphics imgobject = imageBuffer.getGraphics();
    if(thread1!=null)
    {
    imgobject.drawImage(img[0], y1, 50, this);
    }
    if(thread2!=null)
    {
    imgobject.drawImage(img[1],50,y2,115,y2+50,0,0,65,50,this);
    }    g.drawImage(imageBuffer,0,0,null);

    drawImage也不知道对不对。自己试吧。
      

  3.   

    我一阵乱改,居然不闪了,谁给我讲下为什么?
    代码如下:
    import java.awt.*;
    import java.applet.*;
    import javax.swing.*;
    public class step1 extends Applet implements Runnable{
    Thread thread1 ,thread2;
    int y1=-100,y2=-50;
    Image img[]=new Image[2];
    public void init()
    {

    setSize(800,600);
    img[0]=getImage(getCodeBase(),"plane2.png");
    img[1]=getImage(getCodeBase(),"boss.png");
    }
    public step1()
    {

    setBackground(Color.WHITE);
    thread1=new Thread(this);
    thread2=new Thread(this);
    thread1.start();
    thread2.start();
    }
    public void update(Graphics g)   //添了个这个方法
    {
    Image imageBuffer;
    imageBuffer=createImage(getWidth(),getHeight());
    Graphics imgobject=imageBuffer.getGraphics();
    paint(imgobject);
    g.drawImage(imageBuffer,0,0,null);//虽然不闪了,可是我还是不懂,谁给我讲讲
    }
    public void paint(Graphics g)
    {

    if(thread1!=null)
    {
    g.drawImage(img[0], y1, 50, this);
    }
    if(thread2!=null)
    {
    g.drawImage(img[1],50,y2,115,y2+50,0,0,65,50,this);
    }

    }
    public void run()
    {
    while(true)
    {
    if(Thread.currentThread()==thread1)
    {
    y1+=6;
    if(y1>=400)
    y1=-100;
    repaint();
    pause(100);
    }
    else if(Thread.currentThread()==thread2)
    {
    y2+=4;
    if(y2>=400)
    y2=-50;
    repaint();
    pause(120);
    }
    }
    }
    private void pause(int time)
    {
    try
    {
    Thread.sleep(time);
    }catch(InterruptedException e){}
    }}