Hey first time posting about code so please say if you need more information than what i give you...

I'm having trouble with a timer, am trying to wait 2 secs then add 5 to Defender.Bullets but they are a delay on this and it bugs out if i keep clicking this. It works properly if i wait a couple of seconds after its been implemented.

Here is the code

new java.util.Timer().schedule( 
        new java.util.TimerTask() {
        @Override
        public void run() {

             Defender.bullets = 5;

        }
    }, 2000 (I have tried to put a delay here but didn't seem to work either)
);

The code has supposed to reload bullets which is up the right hand side of the screen but looks like the "Defender.bullets = 5;" keep going even after the 2 secs...

This is my first time using Timer, so not sure if this is done correctly.

Any information is appreciated
Thanks for your time

1. For GUI -related timing you should use a java.swing.Timer rather than a java.util.Timer because it integrates with the whole swing event queue/ thread environment.
http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html
2. If you are updating some thing that affects the GUI you need to call repaint() or one of those methods to let the GUI know that it needs to be updated.

Defender.bullets is an array, this is being updated so i can fire more objects after 5 secs...

The GUI is constantly being updated under this...
Should i still use the java.swing over this, ive tried different ways with this and i couldn't find a way to do it for what am wanting.

Here is the full set of code for this part:

if(Defender.health > 0 && Defender.bullets > 0){
			
				for (Defender_Bullet defend: Dbullet){
					defend.draw(g);
					defend.update(this, 0);
				}
			
		}
		else if(Defender.health <= 0){
			
			Font font = new Font("OCR A Extended", Font.BOLD | Font.ITALIC, 70);
			
			g.setFont(font);
			g.setColor(Color.RED);
			g.drawString("You Have Lost!", 85, 300);
			
		}
		else if(Defender.bullets == 0) {
			
			for (Defender_Bullet defend: Dbullet){
				defend.draw(g);
				defend.update(this, 0);
			}
			Font font = new Font("OCR A Extended", Font.BOLD | Font.ITALIC, 12);
			
			g.setFont(font);
			g.setColor(Color.RED);
			g.drawString("Reloading", 35, 50);
		
			
				new java.util.Timer().schedule(
				        new java.util.TimerTask() {
				            @Override
				            public void run() {

				            	Defender.bullets = 5;
				         
				            }
				        }, 3000
				); 
		}
		
		Font font = new Font("OCR A Extended", Font.BOLD | Font.ITALIC, 20);
		
		g.setFont(font);
		g.setColor(Color.YELLOW);
		g.drawString(Defender.bullets + "" , 15, 50);
		
		for (Enemy_Bullet Attacker: EBullet){
			Attacker.draw(g);
			Attacker.update(this, 0);	
		}

		Defender.draw(g);
		Defender.update(this, 1);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.