I've changed some of the code but the animation plays when you keep pressing the space bar. I want it to animate my bullet across the screen just by pressing the space bar(not by pressing the space bar mulitple times). Thank for your help!

*Note: the old code was posted under "Bullet Animation" by my classmate. if you can't fix this code, try the old code.

// The "SpaceShootewr" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class SpaceShooter extends Applet implements KeyListener
{
    int x = 10, dx, y = 10, dy, a = 0, by = 47, bx = 95;
    char m;
    private Image dbImage;
    private Graphics dbg;
    Thread thread;
    Image img, tile, background, bullet, blank, mon1, mon2, mon3, mon4, mon5, mon6, mon7;
    Graphics backg;
    boolean triMove = false;
    // Place instance variables here

    public void init ()
    {
        img = getImage (getDocumentBase (), "Shoot.jpg");
        bullet = getImage (getDocumentBase (), "bullet.jpg");
        blank = getImage (getDocumentBase (), "Blank.jpg");
        mon1 = getImage (getDocumentBase (), "mon1.jpg");
        mon2 = getImage (getDocumentBase (), "mon2.jpg");
        mon3 = getImage (getDocumentBase (), "mon3.jpg");
        mon4 = getImage (getDocumentBase (), "mon4.jpg");
        mon5 = getImage (getDocumentBase (), "mon5.jpg");
        mon6 = getImage (getDocumentBase (), "mon6.jpg");
        mon7 = getImage (getDocumentBase (), "mon7.jpg");

        addKeyListener (this);

        /*tile = createImage (1280, 690);
        backg = tile.getGraphics ();
        backg.setColor (Color.blue);
        */

        // Place the body of the initialization method here
    } // init method


    public void paint (Graphics g)
    {



        g.drawImage (img, x, y, 100, 100, this);

        if (triMove == true)
        {
            for (int i = 0 ; i < 50 ; i++)
            {

                bx += 1;

                for (int q = 0 ; q < 1000000 ; q++)
                {
                }

                g.drawImage (blank, bx - 1, y + 37, 40, 10, this);
                
                g.drawImage (bullet, bx + i, y + 37, 40, 10, this);

            }

            triMove = false;


        }

        if (bx >= 250 + 95)
        {
            bx = 95;
        }
        //backg.drawImage(background,0,0,this);

        //Place the body of the drawing method here
    } //paintmethod




    public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();
        /*if(key==KeyEvent.VK_RIGHT)
        {
        //backg.drawImage(background,0,0,1280, 960, this);
        dx=2;
        dy=0;
        x=x+dx;
        repaint();
        }
        elseif(key==KeyEvent.VK_LEFT)
        {
        //backg.drawImage(img,0,0,1280, 960, this);
        dx=-2;
        dy=0;
        x=x+dx;
        repaint();
        }*/
        if (key == KeyEvent.VK_UP)
        {

            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = -10;
            dx = 0;
            y = y + dy;
            repaint ();
        }
        else if (key == KeyEvent.VK_DOWN)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = 10;
            dx = 0;
            y = y + dy;
            repaint ();
        }
        else if (key == KeyEvent.VK_SPACE)
        {
            triMove = true;

            repaint ();
        }
    }


    public void keyReleased (KeyEvent e)
    {

    }


    public void keyTyped (KeyEvent e)
    {

    }


    public void update (Graphics g)
    {
        // initialize doublebuffers

        dbImage = createImage (this.getSize ().width, this.getSize ().height);
        dbg = dbImage.getGraphics ();


        // save background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw foreground on background
        dbg.setColor (getForeground ());
        paint (dbg);

        // Now indicate ready drawn picture Offscreen on the right screen
        g.drawImage (dbImage, 0, 0, this);
    }
} // SpaceShooter class

wait, so what happens when you press the space bar one time?

plz send old code

This is the old code I posted

// The "SpaceShoot" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 
public class SpaceShoot extends Applet implements KeyListener
{
    int x = 10, dx, y = 10, dy, a = 0, by = 47, bx = 95;
    char m;
    private Image dbImage;
    private Graphics dbg;
    Thread thread;
    Image img, tile, background, bullet, blank, mon1, mon2, mon3, mon4, mon5, mon6, mon7;
    Graphics backg;
    boolean triMove = false;
    // Place instance variables here
 
    public void init ()
    {
	img = getImage (getDocumentBase (), "Shoot.jpg");
	bullet = getImage (getDocumentBase (), "bullet.jpg");
	blank = getImage (getDocumentBase (), "Blank.jpg");
	mon1 = getImage (getDocumentBase (), "mon1.jpg");
	mon2 = getImage (getDocumentBase (), "mon2.jpg");
	mon3 = getImage (getDocumentBase (), "mon3.jpg");
	mon4 = getImage (getDocumentBase (), "mon4.jpg");
	mon5 = getImage (getDocumentBase (), "mon5.jpg");
	mon6 = getImage (getDocumentBase (), "mon6.jpg");
	mon7 = getImage (getDocumentBase (), "mon7.jpg");
	addKeyListener (this);
 
	/*tile = createImage (1280, 690);
	backg = tile.getGraphics ();
	backg.setColor (Color.blue);
	*/
 
	// Place the body of the initialization method here
    } // init method
 
 
    public void paint (Graphics g)
    {
 
 
	//backg.drawImage(background, 0, 0, this);
	g.drawImage (img, x, y, 100, 100, this);
 
	if (triMove == true)
	{
	    for (int i = -50 ; i < 350 ; i++)
	    {
 
		bx += 1;
 
		for (int q = 0 ; q < 1000000 ; q++)
		{
		}
		g.drawImage (blank, bx , y + 37, 20, 10, this);
		g.drawImage (bullet, bx, y + 37, 20, 10, this);
 
	    }
 
	    triMove = false;
 
 
	}
 
	if (bx >= 250 + 95)
	{
	    bx = 95;
	}
	//backg.drawImage(background,0,0,this);
 
	//Placethebodyofthedrawingmethodhere
    } //paintmethod
 
 
 
 
    public void keyPressed (KeyEvent e)
    {
	int key = e.getKeyCode ();
	/*if(key==KeyEvent.VK_RIGHT)
	{
	//backg.drawImage(background,0,0,1280, 960, this);
	dx=2;
	dy=0;
	x=x+dx;
	repaint();
	}
	elseif(key==KeyEvent.VK_LEFT)
	{
	//backg.drawImage(img,0,0,1280, 960, this);
	dx=-2;
	dy=0;
	x=x+dx;
	repaint();
	}*/
	if (key == KeyEvent.VK_UP)
	{
 
	    //backg.drawImage (img, 0, 0, 1280, 960, this);
	    dy = -10;
	    dx = 0;
	    y = y + dy;
	    repaint ();
	}
	else if (key == KeyEvent.VK_DOWN)
	{
	    //backg.drawImage (img, 0, 0, 1280, 960, this);
	    dy = 10;
	    dx = 0;
	    y = y + dy;
	    repaint ();
	}
	else if (key == KeyEvent.VK_SPACE)
	{
	    triMove = true;
 
	    repaint ();
	}
    }
 
 
    public void keyReleased (KeyEvent e)
    {
 
    }
 
 
    public void keyTyped (KeyEvent e)
    {
 
    }
 
 
    public void update (Graphics g)
    {
	// initialize doublebuffers
 
	dbImage = createImage (this.getSize ().width, this.getSize ().height);
	dbg = dbImage.getGraphics ();
 
 
	// save background
	dbg.setColor (getBackground ());
	dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);
 
	// draw foreground on background
	dbg.setColor (getForeground ());
	paint (dbg);
 
	// Now indicate ready drawn picture Offscreen on the right screen
	g.drawImage (dbImage, 0, 0, this);
    }
} // SpaceShoot class

and instead of it being a bullet it is kind of a laser bullet. The bullet just keeps moving forward without getting erased and it leaves a gray line behind the bullet itself as a trail.

I want it to animate my bullet across the screen just by pressing the space bar(not by pressing the space bar mulitple times).

On KeyPressed start a Swing Timer that fires every 1/n secs and moves the bullet each time it fires. On KeyReleased, stop the Timer.

ps what on earth is this code supposed to achieve?

for (int q = 0 ; q < 1000000 ; q++)
{
}

it is supposed to be a time delay so the bullet shows up and moves across the screen, instead of it just appearing as a laser.

OK - that's also something that is better handled with a Swing Timer. Loops like that execute at very different speeds depending on the hardware, and lock up the CPU thus interfering with GUI response etc. Swing Timer is the way to go.

OK - that's also something that is better handled with a Swing Timer. Loops like that execute at very different speeds depending on the hardware, and lock up the CPU thus interfering with GUI response etc. Swing Timer is the way to go.

O.K.... None of us (in my class) know how to make a swing timer??? How would I make one and put it into my code?
Here is my (newer)code

// The "SpaceShootewr" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;

public class SpaceShooter extends Applet implements KeyListener
{
    int x = 10, dx, y = 10, dy, a = 0, by = 47, bx = 95;
    char m;
    private Image dbImage;
    private Graphics dbg;
    Thread thread;
    Image img, tile, background, bullet, blank, mon1, mon2, mon3, mon4, mon5, mon6, mon7;
    Graphics backg;
    boolean triMove = false;
    // Place instance variables here

    public void init ()
    {
        img = getImage (getDocumentBase (), "Shoot.jpg");
        bullet = getImage (getDocumentBase (), "bullet.jpg");
        blank = getImage (getDocumentBase (), "Blank.jpg");
        mon1 = getImage (getDocumentBase (), "mon1.jpg");
        mon2 = getImage (getDocumentBase (), "mon2.jpg");
        mon3 = getImage (getDocumentBase (), "mon3.jpg");
        mon4 = getImage (getDocumentBase (), "mon4.jpg");
        mon5 = getImage (getDocumentBase (), "mon5.jpg");
        mon6 = getImage (getDocumentBase (), "mon6.jpg");
        mon7 = getImage (getDocumentBase (), "mon7.jpg");

        addKeyListener (this);

        /*tile = createImage (1280, 690);
        backg = tile.getGraphics ();
        backg.setColor (Color.blue);
        */

        // Place the body of the initialization method here
    } // init method


    public void paint (Graphics g)
    {



        g.drawImage (img, x, y, 100, 100, this);

        if (triMove == true)
        {
            for (int i = 0 ; i < 50 ; i++)
            {

                bx += 1;

                for (int q = 0 ; q < 1000000 ; q++)
                {
                }

                g.drawImage (blank, bx - 1, y + 37, 40, 10, this);
                
                g.drawImage (bullet, bx + i , y + 37, 40, 10, this);

            }

            triMove = false;


        }

        if (bx >= 250 + 95)
        {
            bx = 95;
        }
        //backg.drawImage(background,0,0,this);

        //Place the body of the drawing method here
    } //paintmethod




    public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();
        /*if(key==KeyEvent.VK_RIGHT)
        {
        //backg.drawImage(background,0,0,1280, 960, this);
        dx=2;
        dy=0;
        x=x+dx;
        repaint();
        }
        elseif(key==KeyEvent.VK_LEFT)
        {
        //backg.drawImage(img,0,0,1280, 960, this);
        dx=-2;
        dy=0;
        x=x+dx;
        repaint();
        }*/
        if (key == KeyEvent.VK_UP)
        {

            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = -10;
            dx = 0;
            y = y + dy;
            repaint ();
        }
        else if (key == KeyEvent.VK_DOWN)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = 10;
            dx = 0;
            y = y + dy;
            repaint ();
        }
        else if (key == KeyEvent.VK_SPACE)
        {
            triMove = true;

            repaint ();
        }
    }


    public void keyReleased (KeyEvent e)
    {

    }


    public void keyTyped (KeyEvent e)
    {

    }


    public void update (Graphics g)
    {
        // initialize doublebuffers

        dbImage = createImage (this.getSize ().width, this.getSize ().height);
        dbg = dbImage.getGraphics ();


        // save background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw foreground on background
        dbg.setColor (getForeground ());
        paint (dbg);

        // Now indicate ready drawn picture Offscreen on the right screen
        g.drawImage (dbImage, 0, 0, this);
    }
} // SpaceShooter class

You create and start a javax.swing.Timer to run every "n" millisecs.
It has a reference to some class with an ActionPerformed method, and it calls that method every "n" miliisecs.
eg:

import javax.swing.Timer;
...
// in main program initialisation...
int milliSecsBetweenRuns = 100;
Timer timer = new Timer(milliSecsBetweenRuns , new ThingToRunRepeatedly());
...
// in rsponse to space key pressed...
timer.start();
...
// in response to space key released...
timer.stop();

... and the thing that gets called is as simple as this...

// gets called buy the timer every 100 mSec as long as space key is down...
class ThingToRunRepeatedly implements ActionListener {
  public void actionPerformed(ActionEvent e) {
     // update x,y coords etc 
     // call repaint for the main window 
  }
}

You create and start a javax.swing.Timer to run every "n" millisecs.
It has a reference to some class with an ActionPerformed method, and it calls that method every "n" miliisecs.
eg:

import javax.swing.Timer;
...
// in main program initialisation...
int milliSecsBetweenRuns = 100;
Timer timer = new Timer(milliSecsBetweenRuns , new ThingToRunRepeatedly());
...
// in rsponse to space key pressed...
timer.start();
...
// in response to space key released...
timer.stop();

... and the thing that gets called is as simple as this...

// gets called buy the timer every 100 mSec as long as space key is down...
class ThingToRunRepeatedly implements ActionListener {
  public void actionPerformed(ActionEvent e) {
     // update x,y coords etc 
     // call repaint for the main window 
  }
}

I'm sorry... But what do you mean by "Update x,y coords etc"? Why do I need to update my x,y coordinates?

Never mind! I got it working! so far....

I was referring to the coordinates at which you will draw the bullets or whatever when you repaint() causes your paint method to be called - so each time the timer fires you update the variables that control the position of the bullet, then your paint repaints it (just once each call) at that position.

To clarify: the program structure goes like this:

You have some moving objects to draw in your window.
Each object has a current position, speed & direction.
You have a paint method that paints all the objects at their current positions.
When the user presses certain keys you change the speed/direction
Each time the timer runs you update each object's position according to its speed & direction.
Each time you change an objects position you call repaint so that the window gets updated (via the paint method).

This overall design will allow you to have almost unlimited scope for many objects, complex movements, varying speeds, etc.

O.K... It animates half way across the screen.. How would I make it go across the entire screen?

Here is the new code.

// The "SpaceShooter" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;

public class SpaceShooter extends Applet implements KeyListener
{
    int x = 10, dx, y = 10, dy, a = 0, by = 47, bx = 95;
    char m;
    private Image dbImage;
    private Graphics dbg;
    Thread thread;
    Image img, tile, background, bullet, blank, mon1, mon2, mon3, mon4, mon5, mon6, mon7;
    Graphics backg;
    boolean triMove = false;
    Timer timer;
    // Place instance variables here

    public void init ()
    {
        int milliSecsBetweenRuns = 1;
        timer = new Timer (milliSecsBetweenRuns, new ThingToRunRepeatedly ());
        img = getImage (getDocumentBase (), "Shoot.jpg");
        bullet = getImage (getDocumentBase (), "bullet.jpg");
        blank = getImage (getDocumentBase (), "Blank.jpg");
        mon1 = getImage (getDocumentBase (), "mon1.jpg");
        mon2 = getImage (getDocumentBase (), "mon2.jpg");
        mon3 = getImage (getDocumentBase (), "mon3.jpg");
        mon4 = getImage (getDocumentBase (), "mon4.jpg");
        mon5 = getImage (getDocumentBase (), "mon5.jpg");
        mon6 = getImage (getDocumentBase (), "mon6.jpg");
        mon7 = getImage (getDocumentBase (), "mon7.jpg");

        addKeyListener (this);

        // Place the body of the initialization method here
    } // init method


    public void paint (Graphics g)
    {

        g.drawImage (img, x, y, 100, 100, this);

        if (triMove == true)
        {
            for (int i = 0 ; i < 50 ; i++)
            {

                bx += 1;

                g.drawImage (blank, bx - 1, y + 37, 40, 10, this);

                g.drawImage (bullet, bx, y + 37, 40, 10, this);

            }

            triMove = false;
        }

        if (bx >= 450 + 95)
        {
            bx = 95;
        }


        //Place the body of the drawing method here
    } //paintmethod




    public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();

        /*if(key==KeyEvent.VK_RIGHT)
        {
        //backg.drawImage(background,0,0,1280, 960, this);
        dx=2;
        dy=0;
        x=x+dx;
        repaint();
        }
        elseif(key==KeyEvent.VK_LEFT)
        {
        //backg.drawImage(img,0,0,1280, 960, this);
        dx=-2;
        dy=0;
        x=x+dx;
        repaint();
        }*/
        if (key == KeyEvent.VK_UP)
        {

            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = -10;
            dx = 0;
            y = y + dy;
            repaint ();
        }
        else if (key == KeyEvent.VK_DOWN)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = 10;
            dx = 0;
            y = y + dy;
            repaint ();
        }
        else if (key == KeyEvent.VK_SPACE)
        {
            triMove = true;
            timer.start ();
            repaint ();

        }


    }


    public void keyReleased (KeyEvent e)
    {
        int keyRELEASED = e.getKeyCode ();


        if (keyRELEASED == KeyEvent.VK_SPACE)
        {
            triMove = true;

            timer.stop ();
            repaint ();


        }


    }



    //gets called buy the timer every 100 mSec as long as space key is down...
    class ThingToRunRepeatedly implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {

            repaint ();
            // update x,y coords etc
            // call repaint for the main window
        }
    }


    public void keyTyped (KeyEvent e)
    {

    }


    public void update (Graphics g)
    {
        // initialize doublebuffers

        dbImage = createImage (this.getSize ().width, this.getSize ().height);
        dbg = dbImage.getGraphics ();


        // save background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw foreground on background
        dbg.setColor (getForeground ());
        paint (dbg);

        // Now indicate ready drawn picture Offscreen on the right screen
        g.drawImage (dbImage, 0, 0, this);
    }
} // SpaceShooter class

1 mSec timer is crazy - that's 1000 screen updates/sec. Start with something slow enough to see exactly what's happening (eg 1000 mSec), then speed it up until the movement is smooooth (eg 60 mSec).
I'm off now until tomorrow. good luck.

Well, It turns out that the bullet only draws three times then it stops...
I'm still trying to figure out how to animate the bullet across the entire screen just by pressing the spacebar (I don't want people to hold the spacebar to animate the bullet across the screen) I would like something like space invaders.. you press the fire button and the bullet travels across the screen (no button holding!)

Am I asking too much?

P.S: New code!

Thank-You!

// The "SpaceShooter" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;

public class SpaceShooter extends Applet implements KeyListener
{
    int x = 10, dx, y = 10, dy, a = 0, by = 47, bx = 95, mx = 510, my = 20;
    private Image dbImage;
    private Graphics dbg;
    Thread thread;
    Image img, tile, background, rocketBullet,bullet, blank, sam, mon1, mon2, mon3, mon4, mon5, mon6, mon7;
    Graphics backg;
    boolean triMove = false;
    Timer timer;
    int count = 0;
    //JLabel credit;
    // Place instance variables here

    public void init ()
    {
        //JLabel credit = new JLabel ("fgdfg");
        int milliSecsBetweenRuns = 2147483647;
        timer = new Timer (milliSecsBetweenRuns, new ThingToRunRepeatedly ());
        img = getImage (getDocumentBase (), "Shoot.jpg");
        bullet = getImage (getDocumentBase (), "bullet.jpg");
        rocketBullet = getImage (getDocumentBase (), "rocketbullet.jpg");
        blank = getImage (getDocumentBase (), "Blank.jpg");
        sam = getImage (getDocumentBase (), "Samus.jpg");
        mon1 = getImage (getDocumentBase (), "mon1.gif");
        mon2 = getImage (getDocumentBase (), "mon2.gif");
        mon3 = getImage (getDocumentBase (), "mon3.jpg");
        mon4 = getImage (getDocumentBase (), "mon4.gif");
        mon5 = getImage (getDocumentBase (), "mon5.gif");
        mon6 = getImage (getDocumentBase (), "mon6.jpg");
        mon7 = getImage (getDocumentBase (), "mon7.jpg");
        addKeyListener (this);

        // Place the body of the initialization method here
    } // init method


    public void paint (Graphics g)
    {

        g.drawImage (img, x, y, 100, 100, this);
        g.drawImage (sam, mx + 350, my + 150, 100, 100, this);

        g.drawImage (mon1, mx, my, 50, 50, this);
        g.drawImage (mon1, mx + 175, my + 20, 50, 50, this);

        g.drawImage (mon2, mx + 50, my + 210, 50, 50, this);
        g.drawImage (mon2, mx + 175, my + 260, 50, 50, this);

        g.drawImage (mon3, mx + 100, my + 85, 50, 50, this);
        g.drawImage (mon3, mx + 200, my + 185, 50, 50, this);

        g.drawImage (mon4, mx + 25, my + 110, 50, 50, this);
        g.drawImage (mon4, mx + 300, my + 85, 50, 50, this);

        g.drawImage (mon5, mx - 25, my + 210, 50, 50, this);
        g.drawImage (mon5, mx - 50, my - 10, 50, 50, this);

        g.drawImage (mon6, mx - 10, my + 310, 50, 50, this);
        g.drawImage (mon6, mx - 70, my + 60, 50, 50, this);

        g.drawImage (mon7, mx + 10, my + 410, 50, 50, this);
        g.drawImage (mon7, mx + 150, my + 360, 50, 50, this);


        if (triMove == true)
        {
            count = count + 1;

            if (count == 20)
            {
                mx = mx - 7;
                my = my - 10;
                count = count + 1;
            }
            if (count == 45)
            {
                mx = mx - 5;
                my = my + 10;
                count = 0;
            }

            if (count == 60)
            {
                mx = mx - 7;
                my = my - 10;

                count = count + 1;
            }
            if (count == 85)
            {
                mx = mx - 5;
                my = my + 10;
                count = 0;
            }

            for (int i = -25 ; i < 50 ; i++)
            {

                bx += 1;

                g.drawImage (blank, bx - 1, y + 37, 40, 10, this);

                g.drawImage (bullet, bx, y + 37, 40, 10, this);
                
                //g.drawImage (rocketBullet, bx, y + 37, 40, 10, this);
            }

            triMove = false;
        }

        if (bx >= 450 + 95)
        {
            bx = 95;
        }

        //Place the body of the drawing method here
    } //paintmethod




    public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();

        /*if(key==KeyEvent.VK_RIGHT)
        {
        //backg.drawImage(background,0,0,1280, 960, this);
        dx=2;
        dy=0;
        x=x+dx;
        repaint();
        }
        elseif(key==KeyEvent.VK_LEFT)
        {
        //backg.drawImage(img,0,0,1280, 960, this);
        dx=-2;
        dy=0;
        x=x+dx;
        repaint();
        }*/
        if (key == KeyEvent.VK_UP)
        {

            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = -10;
            dx = 0;
            y = y + dy;
            repaint ();
        }
        else if (key == KeyEvent.VK_DOWN)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = 10;
            dx = 0;
            y = y + dy;
            repaint ();
        }
        else if (key == KeyEvent.VK_SPACE)
        {
            triMove = true;
            timer.start ();
            repaint ();

        }
    }


    public void keyReleased (KeyEvent e)
    {
        int keyReleased = e.getKeyCode ();


        if (keyReleased == KeyEvent.VK_SPACE)
        {
            triMove = true;
            timer.stop ();
            repaint ();
        }
    }


    //gets called buy the timer every 100 mSec as long as space key is down...
    class ThingToRunRepeatedly implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            repaint ();
            // update x,y coords etc
            // call repaint for the main window
        }
    }


    public void keyTyped (KeyEvent e)
    {

    }


    public void update (Graphics g)
    {
        // initialize doublebuffers

        dbImage = createImage (this.getSize ().width, this.getSize ().height);
        dbg = dbImage.getGraphics ();

        // save background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw foreground on background
        dbg.setColor (getForeground ());
        paint (dbg);

        // Now indicate ready drawn picture Offscreen on the right screen
        g.drawImage (dbImage, 0, 0, this);
    }
} // SpaceShooter class

I'm now considering adding in a music file (for the battle)
any ideas on how to do that?

Thanks!

Personally I wouldn't try to add major new features until the existing parts are working. I posted some suggestions about how to re-structure your code earlier, and I'm going to stick by that advice. But it's your code, so good luck.

O.K... I'll forget about the music for now... but how about the animation I asked about earlier?

I posted some suggestions about how to re-structure your code earlier, and I'm going to stick by that advice

I agree with JamesCherrill, a code re-structure will allow you to do things much more easily. As it stands, why do you start and stop the timer when you press/release the spacebar? Do you have a player character? How does he move unless spacebar is pressed? You shouldn't need all of those repaints() either. Calling it once, in actionPerformed should be adequate. The way you have it, whenever you press something, it will repaint two times faster.

Why not have a method for each object that needs to move, and set the movement calculations inside that, and then call that method when you press the corresponding key? As it is, that triMove thing is a bad idea. You don't need to loop it. The Timer and ActionPerformed do the looping. Then use some boolean value e.g. isShot then test that for the paint method.

public void moveBullet()
{
bulletX+=mx;//or whatever

}
public void keyPressed (KeyEvent e)

{

int key = e.getKeyCode ();

 if (key == KeyEvent.VK_SPACE)
{
moveBullet();
}

Hi Akill - I think he wants to use keys to set things moving, then have them run on on their own - so the move code needs to be in the timer. Set the flags, speeds or whatever in response to the keys but update positions etc each time the timer fires. The paint method should really just re-paint the screen based on the latest positions etc. Moving things in the paint (like it does now) is bad because you don't know when or how often paint will be called.
The start/stop timer was my first suggestion, trying to keep thing s simple for a beginning. As it develops I would leave the timer running and accept that it may do little or nothing if no objects are actually moving.

Well, I want to thank everyone for posting in this thread! I finally got my bullet to animate the way I wanted!!!!!!

Special thanks goes to JamesCherrill for putting in so much time and coding tips and samples!

Thank you to everyone!!!!!! =)

javanoob101

Glad to help! Mark this as "solved"?

O.k I marked it as "Solved"!

I have a new thread too. This is for the collision detection to make the monsters disappear when the bullet hits them and I need help quickly. This is due tomorrow and I feel overwhelmed with all the coding needed to make a collision detection!

Please help!!!!!

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.