So I added a background Image to an applet and now whenever I try to run
the Applet the background image starts to flicker, I have added more
images and button on top of the background is that causing the flickers
or is there something wrong with my code?
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
import java.awt.Color;
public class game extends Applet implements Runnable {
int x = 350, y = 10,z=10,n=0;
Image bal,a,bg;
Boolean flag=true,arr=false;
Button b;
String str="SCORE : ";
Label l;
public void start() {
try
{
MediaTracker tracker = new MediaTracker (this);
bg= getImage(getDocumentBase(),"bg.jpg");
tracker.addImage(bg, 0);
} catch (Exception e) {}
bg= getImage(getDocumentBase(),"bg.jpg");
bal=getImage(getDocumentBase(),"balloon.png");
a=getImage(getDocumentBase(),"arrow.png");
new Thread(this).start();
b=new Button("SHOOT");
add(b);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
arr=true;
}});
setSize(500, 500);
setBackground(Color.white);
}
public void run() {
while (true) {
if(arr)
{
updates();
}
update();
try {
Thread.sleep(25);
} catch (InterruptedException ex) {}
if(z>300)
{
arr=false;
z=10;
}
if(z+70>=x && (y>=200 && y<=300))
{
n=n+10;
showStatus("HIT!");
public void updates(){
z+=25;
repaint();
}
public void update() {
if(flag)
{ y += 6; }
else
{ y -= 6; }
if(y>450) flag=false;
if(y<10) flag=true;
repaint();
}
public void paint(Graphics g) {
g.drawImage(bg,0,0,500,500,this);
g.drawImage(bal,x, y, 75, 75,this);
g.drawImage(a,z+10,250,70,50,this);
g.drawString(str+n,220,480);
}}
What I have tried:
I have no idea what is causing this..