Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I need help

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help

    ok so here are the few problems i have, when i use keylistener it gives me an error and highlights "rocketship"
    next i need to know how to make a collision code so if the rocket hits the asteriod and explosion or w/e comes up.

    Thanks alot if you can help

     
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Timer;
    import java.util.TimerTask;
     
    import java.lang.*;
     
    public class rocketship extends Applet implements ActionListener
    {
        //Declare buttons and images
        Button btnFLeft, btnFRight, btnUp, btnDown;
        Image imgF1, asteroid, explode;
        int Rocket, Move[] = new int [5];
     
        int x, y, a, b, c ,d,k,e,f;
        public void init ()
        {
     
            //Load the rocket images
            imgF1 = getImage (getCodeBase (), "F1.JPG");
            asteroid = getImage (getCodeBase (), "asteroid.JPG");
            explode = getImage (getCodeBase (), "explode.JPG");
     
     
     
            btnFRight = new Button ("Right");
            btnFLeft = new Button ("Left");
     
     
     
            //Show action button
     
            setLayout (null);
            add (btnFRight);
            btnFRight.setActionCommand ("Right");
            btnFRight.addActionListener (this);
            btnFRight.setBounds (465, 650, 40, 30);
     
     
            add (btnFLeft);
            btnFLeft.setActionCommand ("Left");
            btnFLeft.addActionListener (this);
            btnFLeft.setBounds (425, 650, 40, 30);
     
     
        }
     
     
        public void actionPerformed (ActionEvent e)
        {
     
            String ACTION = e.getActionCommand ();
            //Random Variables in Moving the rocket
     
            if (ACTION.equals ("Right"))
            {
                Move [4] = Move [4] + 50;
     
            }
            if (ACTION.equals ("Left"))
            {
                Move [3] = Move [3] + 50;
     
            }
     
     
            repaint ();
        }
     
     
     
        public void paint (Graphics g)
        {
     
     
            //Background
            setBackground (Color.black);
            /*  timer = new Timer(speed, this);
            timer.setInitialDelay(pause);
            timer.start();
            */
     
            g.drawImage (imgF1, 100 + Move [4] - Move [3], 550, 90, 90, this);
     
    if (Move [4] - Move [3] == y)
    imgF1= imgF1.hide;
            y = y + 5;
            b = b + 6;
            d = d+ 4;
            g.drawImage (asteroid, x, y, e, e, this);
            g.drawImage (asteroid, a, b, f, f, this);
              g.drawImage (asteroid, c, d, k, k, this);
     
     
     
            if (y > 650)
            {
                y = 0;
                x = ((int) (1000 * Math.random ()));
                e = ((int) (100 * Math.random ())+10);
            }
            if (b > 650)
            {b= 0;
            a = ((int) (1000 * Math.random ()));
            f = ((int) (75 * Math.random ())+10);
            }
            if (d > 650)
            {
            d = 0;
            c =((int) (1000 * Math.random ()));
            k = ((int) (75 * Math.random ())+10);
            }
            repaint ();
            try
            {
                //do what you want to do before sleeping
                Thread.currentThread ().sleep (5); //sleep for 1000 ms
                //do what you want to do after sleeptig
            }
            catch (InterruptedException ie)
            {
                //If this thread was intrrupted by nother thread
            }
     
     
     
     
     
     
        }
    }

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I need help

    Quote Originally Posted by airsim15 View Post
    ok so here are the few problems i have, when i use keylistener it gives me an error and highlights "rocketship"
    I see no KeyListener in your code, so am unclear what's going on. Did you remove it? The usual errors received when implementing interfaces such as KeyListeners is a missing implementation (function) of the interface.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help

    ok so this is my code so far, the collision code is at the bottom im haveing trouble with it because it only detects collision for one of the asteriods (the one with x,y as the cooridinates). can u please help me.
    so basically the collision code is soppose to say if its in the range of 100 right or 100 left of the rocket ship than the asteriod counts as a hit and goes back to the top.


     
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.*;
    import java.lang.*;
     
    public class rocketship3 extends Applet implements ActionListener
    {
        //Declare buttons and images
        Button btnFLeft, btnFRight, btnUp, btnDown;
        Image imgF1, asteroid, explode;
        int Rocket, Move[] = new int [5];
        int l;
        int r;
        int h;
        int w, z, n;
        int x, y, a, b, c, d, k, e, f;
        Label lblPoints;
        int amount;
        public void init ()
        {
            lblPoints = new Label ("SCORE:  " + amount + " Points");
            //Load the rocket images
            imgF1 = getImage (getCodeBase (), "F1.JPG");
            asteroid = getImage (getCodeBase (), "asteroid.JPG");
            explode = getImage (getCodeBase (), "explode.JPG");
     
     
     
            btnFRight = new Button ("Right");
            btnFLeft = new Button ("Left");
     
     
     
            //Show action button
            add (lblPoints);
            setLayout (null);
            add (btnFRight);
            btnFRight.setActionCommand ("Right");
            btnFRight.addActionListener (this);
            btnFRight.setBounds (465, 650, 40, 30);
     
            lblPoints.setBackground (Color.red);
            lblPoints.setBounds (300, 15, 200, 40);
            add (btnFLeft);
            btnFLeft.setActionCommand ("Left");
            btnFLeft.addActionListener (this);
            btnFLeft.setBounds (425, 650, 40, 30);
     
     
        }
     
     
        public void actionPerformed (ActionEvent e)
        {
     
            String ACTION = e.getActionCommand ();
            //Random Variables in Moving the rocket
     
            if (ACTION.equals ("Right"))
            {
                r = r + 50;
     
            }
            if (ACTION.equals ("Left"))
            {
                l = l + 50;
     
            }
     
            repaint ();
        }
     
     
     
        public void paint (Graphics g)
        {
     
     
     
            //Background
            setBackground (Color.black);
            /*  timer = new Timer(speed, this);
            timer.setInitialDelay(pause);
            timer.start();
            */
     
     
            g.drawImage (imgF1, r - l, 550, 90, 90, this);
     
     
            y = y + 5;
            b = b + 6;
            d = d + 4;
            g.drawImage (asteroid, x, y, e + 50, e + 50, this);
            g.drawImage (asteroid, a, b, f + 50, f + 50, this);
            g.drawImage (asteroid, c, d, k + 50, k + 50, this);
     
      repaint ();
     
            if (y > 650)
            {
                y = 0;
                x = ((int) (1000 * Math.random ()));
                e = ((int) (50 * Math.random ()) + 10);
                amount = (amount + 1);
     
                lblPoints.setText ("SCORE:  " + amount + "  Points");
     
            }
            if (b > 650)
            {
                b = 0;
                a = ((int) (1000 * Math.random ()));
                f = ((int) (50 * Math.random ()) + 10);
                amount = (amount + 1);
                lblPoints.setText ("SCORE:  " + amount + "  Points");
            }
            if (d > 650)
            {
                d = 0;
                c = ((int) (1000 * Math.random ()));
                k = ((int) (50 * Math.random ()) + 10);
                amount = (amount + 1);
                lblPoints.setText ("SCORE:  " + amount + "  Points");
                }
     
            repaint ();
            try
            {
                //do what you want to do before sleeping
                Thread.currentThread ().sleep (25); //sleep for 1000 ms
                //do what you want to do after sleeptig
            }
            catch (InterruptedException ie)
            {
                //If this thread was intrrupted by nother thread
            }
     
     
     
     
     
            for (int w = 1 ; w <=100  ; w++)
     
                if ((r - l == x + w) && (y == 550) || (r - l == x - w) && (y == 550))
     
                    {
                        y = 0;
     
                        x = ((int) (1000 * Math.random ()));
     
                        amount = (amount - 100);
     
                        g.drawImage (explode, r-l, 550, 60, 60, this);
     
                        lblPoints.setText ("SCORE:  " + amount + "  Points");
     
                    }
                    else if (( r - l == a + w) && (b==550) || (r - l == a - w) && (b == 550))
     
        {
                        b = 0;
     
                        a = ((int) (1000 * Math.random ()));
     
                        amount = (amount - 100);
     
                        g.drawImage (explode, r-l, 550, 60, 60, this);
     
                        lblPoints.setText ("SCORE:  " + amount + "  Points");
     
                    }       
     
                                else if (( r - l == c + w) && (d==550) || (r - l == c - w) && (d == 550))
     
        {
                        d = 0;
     
                        c = ((int) (1000 * Math.random ()));
     
                        amount = (amount - 100);
     
                        g.drawImage (explode, r-l, 550, 60, 60, this);
     
                        lblPoints.setText ("SCORE:  " + amount + "  Points");
     
                    }   
     
     
     
     
     
     
        }
    }