Hello I am coding a small araknoid type of game but am facing a problem.I have written a code to move the ball in the screen but the problem is that it does not move uniformly.The ball speeds up sometimes whereas sometimes slows down (i.e it flickers).I am using BufferedImage but no luck…Please help me…
Here is the code :
import java.awt.;
import java.awt.image.;
import java.awt.geom.*;
class interTest extends Frame implements Runnable
{int width=1368,height=760;
BufferedImage buffer=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d=buffer.createGraphics();
Imob j;
interTest()
{
super(“Game”);
setSize(width,height);
setVisible(true);
setEnabled(true);
repaint();
new Thread(this).start();
}
public void paint(Graphics g)
{
g2d.setColor(Color.WHITE);
g2d.fillRect(0,0,width,height);
if(j!=null)j.update(g2d);
g.drawImage(buffer,0,0,this);
}
public void start(){}
public void stop(){}
public void run(){
j=new Imob(2,2,10,10,this);
j.load(“balls.gif”);
while(true){
repaint();
try{Thread.sleep(5);}catch(Exception e){}
}
}
}
import java.awt.;
import java.net.;
import javax.swing.;
import java.awt.geom.;
class Imob{
Toolkit loader=Toolkit.getDefaultToolkit();
Image img;
int x,y,dx,dy;
Frame frame;
Imob(int dx,int dy,int x,int y,Frame f)
{
this.x=x;
this.y=y;
this.dx=dx;
this.dy=dy;
this.frame=f;
}
public void load(String s)
{img=loader.getImage(this.getClass().getResource(s));
}
public void draw(Graphics2D g2d)
{
g2d.setTransform(new AffineTransform());
g2d.drawImage(img,x,y,frame); }
public Rectangle getBounds()
{
int height=img.getHeight(frame);
int width=img.getWidth(frame);
return(new Rectangle(x,y,width,height));
}
public boolean hasColided(Imob x)
{
return getBounds().intersects(x.getBounds());
}
public void update(Graphics2D g2d)
{x+=dx;
y+=dy;
draw(g2d);
}
}