Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

Java
import java.applet.Applet;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.Color;

    //<applet code="game" width="500" height="500">

    public class game extends Applet implements Runnable {  //interface for thread

    	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"); //setting background
            tracker.addImage(bg, 0);
        } catch (Exception e) {}
        bg= getImage(getDocumentBase(),"bg.jpg");   //get background
        bal=getImage(getDocumentBase(),"balloon.png");  //get balloon
        a=getImage(getDocumentBase(),"arrow.png"); //get arrow
        new Thread(this).start();
        b=new Button("SHOOT");
        add(b);
        b.addActionListener(new ActionListener(){ //perform this when clicked
        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);           //making thread sleep
            } 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(){           //increments the arrow
      	z+=25;
      	repaint();
      	}
      	public void update() {          //increments the balloon
        if(flag)
       		{ y += 6; }
        else
       		{ y -= 6; }                  //decrements the balloon
        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..
Posted
Updated 6-Oct-19 4:26am
v2
Comments
Richard MacCutchan 6-Oct-19 10:25am    
It could be that you are refreshing the images too much or too fast.
SagarRawat 6-Oct-19 11:23am    
what change should I make on my code then?
And the flickering occurs only when there is an background image.
Thanks.
Richard MacCutchan 6-Oct-19 11:37am    
I still think it is the frequency with which you are repainting the screen. You need to check how often it is happening, and whether you can reduce that to a number that prevents the flicker.
SagarRawat 6-Oct-19 11:43am    
Well I think I got it, I was skewing the bgImage to a smaller size that was causing the flicker. I changed the applet's height and width to the image resolution and now there are very few flickers...
Appreciate the help tho.
[no name] 6-Oct-19 10:56am    
I agree with Richard. You got a while loop that's probably doing redundant refreshes, which causes "flicker", AND impacts performance (because UI updates are slower than general processing).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900