Results 1 to 2 of 2
  1. #1
    adam405 is offline Member
    Join Date
    Mar 2008
    Posts
    7
    Rep Power
    0

    Default bouncing ball issue

    I am trying to create a bouncing ball. At the momene the ball just travels off the applet screen instead of bouncing back. Any help is appreciated.

    Java Code:
    // import necessary packages
    import java.applet.*;
    import java.awt.*;
    import java.util.Random;
    
    
    public class FirstApplet extends Applet implements Runnable
    {
        //position of the ball
        int x_pos = 1;
        int y_pos = 1;
        int first_x =1;
        int first_y =1;
       
        //ball size
        int radius = 5;
        
        //direction
        int x_speed = 1;
        int y_speed = 1;
        
        //applet size
        int appletsize_top = 10;
        int appletsize_bottom = 370;  
        int appletsize_left = 10;  
        int appletsize_right = 370;  
        //random movement
        Random rnd = new Random ();
        
        //Drawing image off screen
        private Image dbImage;
        private Graphics dbg;
        
        int maxspeed = 1;
    
    // start - method is called every time you enter the HTML 
    public void start ()
    {
    // define a new thread 
    Thread th = new Thread (this);
    // start this thread
    th.start ();
    }
    
    	
    	
    //run- start the applet game
    public void run () 
    { 
        while (true)
        {
        // lower ThreadPriority 
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        //change direction if ball hits right side
     
        if (x_pos > appletsize_left)
    	{
                // Setzen der x - Position
    		x_pos += first_x;
    		y_pos += first_y;
                    
    		x_speed = (rnd.nextInt ()) % maxspeed;
            }
    
        else if (x_pos < appletsize_right)
    	{
                // Setzen der x - Position
    		x_pos += first_x;
    		y_pos += first_y;
                    
    		x_speed = (rnd.nextInt ()) % maxspeed;
            }      
        
        else if (y_pos > appletsize_top)
    	{
                // Setzen der x - Position
    		x_pos += first_x;
    		y_pos += first_y;
                    
    		x_speed = (rnd.nextInt ()) % maxspeed;
            } 
         else if (y_pos < appletsize_bottom)
    	{
                // Setzen der x - Position
    		x_pos += first_x;
    		y_pos += first_y;
                    
    		x_speed = (rnd.nextInt ()) % maxspeed;
            } 
        
       
         //repaint applet
         repaint();
    
        try
        {
            // Stop thread for the speed
            Thread.sleep (10);
        }
        catch (InterruptedException ex)
        {
           
        }
    
            // set ThreadPriority to maximum value
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        
    }
    }
    
    //create an image off screen and loads it into the applet
    public void update (Graphics g)
    {
    // initialize buffer
    if (dbImage == null)
    {
    dbImage = createImage (this.getSize().width, this.getSize().height);
    dbg = dbImage.getGraphics ();
    }
    
    // clear screen in background
    dbg.setColor (getBackground ());
    dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
    // draw elements in background
    dbg.setColor (getForeground());
    paint (dbg);
    
    // draw image on the screen
    g.drawImage (dbImage, 0, 0, this);
    }
    
    
    
    
    /** paint - method allows you to paint into your applet. This method is called e.g. if you move your browser window or if you call repaint() */
    public void paint (Graphics g) 
    
    { 
        // set color
        g.setColor (Color.red);
    
        // paint a filled colored circle
        g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
    
    
    }
    
    // Method to handle mouse down events 
    public boolean mouseDown (Event e, int x, int y) 
    {
        // Change direction, put speed into a minus number 1 now become -1
        x_speed = - (x_speed); 
        return true;
    }
    
    
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Here's how you deal with deprecation warnings:
    Java Code:
    C:\jexp>javac firstappletrx.java
    Note: firstappletrx.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    
    C:\jexp>javac -Xlint:deprecation firstappletrx.java
    firstappletrx.java:141: warning: [deprecation] mouseDown(java.awt.Event,int,int) in java.a
    wt.Component has been deprecated
        public boolean mouseDown (Event e, int x, int y)
                       ^
    1 warning
    Java Code:
    //  <applet code="FirstAppletRx" width="400" height="400"></applet>
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    
    public class FirstAppletRx extends Applet implements Runnable
    {
        //position of the ball
        int x_pos = 12;
        int y_pos = 12;
    //    int first_x = 1;
    //    int first_y = 1;
       
        //ball size
        int radius = 5;
        
        //direction
        int x_speed = 3;
        int y_speed = 2;
        
        //applet size
        int appletsize_top = 10;
        int appletsize_bottom = 370;  
        int appletsize_left = 10;  
        int appletsize_right = 370;  
        //random movement
        Random rnd = new Random ();
        
        //Drawing image off screen
        private Image dbImage;
        private Graphics dbg;
        
        int maxspeed = 5;
    
        Thread thread;
        boolean running = false;
    
        public void init()
        {
            addMouseListener(ma);
        }
    
        // start - method is called after [i]init[/i]
        // and every time the applet is maximized 
        public void start ()
        {
            // define a new thread 
            running = true;
            thread = new Thread (this);
            // start this thread
            thread.start ();
            System.out.println("start");
        }
    
        // A well–behaved Applet stops
        // its animation when minimized.
        public void stop()
        {
            running = false;
            if(thread != null)
                thread.interrupt();
            thread = null;
            System.out.println("stop");
        }
    
        //run- start the applet game
        // Implementation of the Runnable interface.
        public void run () 
        {
            while (running)
            {
                // lower ThreadPriority 
                Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
                // change direction if ball hits any side
                 if (x_pos < appletsize_left || x_pos > appletsize_right)
    	    {
                    x_speed *= -1;
                }
    
                if (y_pos < appletsize_top || y_pos > appletsize_bottom)
    	    {
                    y_speed *= -1;
                }
    
                x_pos += x_speed;
                y_pos += y_speed;
    
                //repaint applet
                repaint();
    
                try
                {
                    // Stop thread for the speed
                    Thread.sleep (50);
                }
                catch (InterruptedException ex)
                {
                    running = false;
                    break;
                }
    
                // set ThreadPriority to maximum value
                Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            }
        }
    
        //create an image off screen and loads it into the applet
        public void update (Graphics g)
        {
            // initialize buffer
            if (dbImage == null)
            {
                dbImage = createImage (this.getSize().width, this.getSize().height);
                dbg = dbImage.getGraphics ();
            }
    
            // clear screen in background
            dbg.setColor (getBackground ());
            dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
            // draw elements in background
            dbg.setColor (getForeground());
            paint (dbg);
    
            // draw image on the screen
            g.drawImage (dbImage, 0, 0, this);
        }
    
        /**
         *  paint - method allows you to paint into your applet.
         *  This method is called e.g. if you move your browser
         *  window or if you call repaint()
         */
        public void paint (Graphics g) 
        { 
            // set color
            g.setColor (Color.red);
    
            // paint a filled colored circle
            g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
        }
    
        private MouseListener ma = new MouseAdapter()
        {
            // Method to handle mousePressed events        
            public void mousePressed(MouseEvent e) 
            {
                // Preserve the direction of x_speed [-:left +:right].
                //int sign = (int)Math.signum(x_speed);  // j2se 1.5+
                // or we can try:
                int sign = (x_speed > 0) ? 1 : -1;
                x_speed = 1 + rnd.nextInt(maxspeed);  // [1 - maxspeed]
                // Restore the sign.
                x_speed *= sign;
            }
        };
    }

Similar Threads

  1. Multiple bouncing balls
    By Algar in forum AWT / Swing
    Replies: 2
    Last Post: 04-24-2008, 08:35 PM
  2. How do I make My ball to move randomly?
    By whdbstjr90 in forum New To Java
    Replies: 4
    Last Post: 12-31-2007, 05:32 PM
  3. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 09:08 PM
  4. Bouncing Ball Just Suddenly Stops Mid Bounce
    By adlb1300 in forum Java 2D
    Replies: 1
    Last Post: 12-03-2007, 02:58 PM
  5. Replies: 2
    Last Post: 12-01-2007, 07:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •