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: Help with code- horizontal to vertical bouncing balls

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with code- horizontal to vertical bouncing balls

    Hi all, I'm new to java and I need help changing the direction of these balls from a horizontal axis to a vertical axis bouncing in the center. I've tried reversing the integers but there seems to be something i've missed.

     package package_bouncingball;
     
    import java.applet.*; 
    import java.awt.*; 
    import javax.swing.*;
    import javax.swing.event.*;
     
    public class Class_bouncingball extends Applet implements Runnable 
     
    {
     
     
      int x_pos1 = 150; 
      int y_pos1 = 200; 
      int radius1 = 20; 
     
      int x_pos2 = 250; 
      int y_pos2 = 200; 
      int radius2 = 20;
     
      private float ballspeedx1 = -3;   
      private float ballspeedy1 = 0;
     
      private float ballspeedx2 = 3;   
      private float ballspeedy2 = 0; 
     
     
      public void init() {} 
     
      public void start() { 
      Thread th = new Thread (this); 
       th.start (); } 
       public void stop() {} 
      public void destroy() {} 
      public void run () {
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 
    while (true) 
    { 
      x_pos1 += ballspeedx1;
      y_pos1 += ballspeedy1;
      x_pos2 += ballspeedx2;
      y_pos2 += ballspeedy2;  
     
     
      repaint();
     
      if (x_pos1  < 100) {
        ballspeedx1 = -ballspeedx1; 
        x_pos1 = 100; 
      } 
      if (x_pos2  < 100) {
        ballspeedx2 = -ballspeedx2; 
        x_pos2 = 100; 
      }  
     
      if (x_pos1  > 300) {
        ballspeedx1 = -ballspeedx1; 
        x_pos1 = 300; 
      } 
      if (x_pos2  > 300) {
        ballspeedx2 = -ballspeedx2; 
        x_pos2 = 300; 
      }                                       
     
    if (Math.abs(x_pos2-x_pos1)<radius1+radius2){
        ballspeedx1 = -ballspeedx1;
        ballspeedx2 = -ballspeedx2;
      }
     
      try { Thread.sleep (20); } 
     
     
     
      catch (InterruptedException ex) {} 
     
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY); }} 
     
    public void paint (Graphics g) {
     
    g.setColor (Color.red); 
    g.fillOval (x_pos1 - radius1, y_pos1 - radius1, 2 * radius1, 2 * radius1); 
     
    g.setColor (Color.blue); 
    g.fillOval (x_pos2 - radius2, y_pos2 - radius2, 2 * radius2, 2 * radius2); 
     
     
    g.setColor(Color.black);
    g.drawLine(80,80,80,320); // left
    g.drawLine(320,80,320,320); // right
    g.drawLine(80,80,320,80);  // 
    g.drawLine(80,320,320,320); // 
      }
     
    }


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Help with code- horizontal to vertical bouncing balls

    Quick question. What happens to a number when you multiply it by -1?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with code- horizontal to vertical bouncing balls

    changing the direction of these balls from a horizontal axis to a vertical axis
    x values are for horizontal movement, y values for vertical movement. Change the code to change the y values, not the x values.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java bouncing balls flickering
    By Bananaman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2013, 06:35 PM
  2. Applying filters to immages Horizontal Flip to an image using java
    By IBeeJava in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 5th, 2013, 07:04 PM
  3. How to disable the horizontal scroll of JScrollPane
    By nicknyak in forum AWT / Java Swing
    Replies: 4
    Last Post: October 22nd, 2012, 08:48 AM
  4. Bouncing Balls
    By roza in forum Threads
    Replies: 6
    Last Post: June 7th, 2011, 10:10 AM

Tags for this Thread