JApplet DoubleBuffering

时间:2015-04-28 21:04:10

标签: java image japplet doublebuffered

我的JApplet有问题,我使用它的全部意义是由于默认情况下Buffered Reader已经存在。

所以我的程序会移动一个球,但它会在不同的位置创建许多不同的图片,因为它可以做,但我不知道如何在之后删除它们。这就是它的样子:

enter image description here

这是我的代码:

private Image img;
double bollx = 150, bolly = 690;
double dy = 30, dx = 30;
double x , y;
int diameter = 23;
int click = 0, varjeClick;
JPanel panel;
int yBounce = 0, xBounce = 0;

public void init(){
    setSize(1900, 900);
    panel = new JPanel();
    panel.setLocation(50, 50);
    panel.setSize(1800, 700);
    //panel.setLayout(null);
    panel.addMouseListener(this);
    panel.setBackground(Color.white);
    this.add(panel);
    img = getImage(getCodeBase(), "basketboll.png");        
}
public void start(){
    Thread t = new Thread(this);
    t.start();
}
public void paint(Graphics g){
    //super.paintComponents(g);

    g.drawImage(img, (int)bollx, (int)bolly, panel); 
}
public void flyttaBoll(){
    //om stilla eller inte
    if(click == 0){
        bollx = 150;
        bolly = 690;
    }
    else{
        gravity();
        bollx += dx*0.02;
        bolly += dy*0.02;
    }
    if(bollx >= (panel.getWidth() - diameter*2.2)){
        bollx = panel.getWidth() - diameter*2.200000001;
        dx *= -0.9 ; 
    }
    if(bollx <= 0 + diameter){
        bollx = diameter + 1;
        dx *= -0.9 ;
    }
    if(bolly >= panel.getHeight() - diameter*2.2){
        bolly = panel.getHeight() - diameter*2.200000001;
        dy *= -0.7;
        yBounce++;
        if(yBounce >= 130){
            click = 0;
            yBounce = 0;
            varjeClick = 0;
        }
    }
    if(bolly <= 0 + diameter){
        bolly = diameter + 1;
        dy *= -0.7;
    }
} 
public void gravity(){
    if(varjeClick <= 7){
        dy += 9.82;
    }
    else{
        dy -= 9.82;
    }
}
public void run() {
     while(true){
         flyttaBoll();
         repaint();
         try{Thread.sleep(8);}catch(Exception e){}
     }
}
public void mousePressed(MouseEvent e){
    PointerInfo a = MouseInfo.getPointerInfo();
    Point b = a.getLocation();
    x = b.getX() - diameter/2;
    y = b.getY() - diameter/2;
    dx = x - bollx;
    dy = y - bolly;
    flyttaBoll();

}

我试图删除一些不相关的代码,以减少混乱,我很抱歉我使用瑞典语,但我希望你能看到过去。

0 个答案:

没有答案