Solved

buttons to increase and decrease variables at runtime

Posted on 2000-03-26
5
256 Views
Last Modified: 2006-11-17
Bellow is my code which shows the movement of the ball. Its bouncing with the parameters of gravity.

I want to add a button that should increase its movement and another button to decrease its movement.

==> to summarize, place two buttons: one should increase the value of gravity and another should decrease the value of gravity



import java.applet.Applet;
import java.applet.*;
import java.lang.*;
import java.awt.*;

//Creating a Main class Moving Ball
public class BouncingBall extends Applet implements Runnable {
 

   //Variable Initialization  
   int Direction_On_X = 0;  
   int Direction_On_Y = 0;
   int x_position     = 0;    
   int y_position     = 0;
   int oldx_position  = 0;  
   int oldy_position  = 0;
   int Squashed      = 0;       
   //For movemnt Calculation
   double gravity         = 9.83;
   double dist            = 0.0;
   double velocity        = 0.0;  
   double BouncingFactor  = 0.4;  
   double Temporaray;
   Double Doublenum;
   String bname;
   //Initializing a thread
   Thread thread_Instance;
 
   
//Method to squashed the ball
public void SquashedTheBall()
{
  Squashed = 1;
  repaint();  
}

//Method to beginthe animation , calling by start method
 public void Begin()
  {
     if (thread_Instance == null)
     {
      //
      thread_Instance = new Thread(this);
      thread_Instance.start();
     }
  }


//It will satrt the animation
  public void start()  
  {
      Begin();      
  }
   
  public void run()  
  {
     Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
     while (true)
     {  
      motion();  
      try {
      Thread.sleep(50);  
     }
    catch (Exception sleepProblem)
     {}
 }
}

//Painting the ball , normaly and Squashed
public void paint(Graphics g)  
 {      

     g.setColor(getBackground());  
     g.fillOval(oldx_position,oldy_position,40,40);
     g.setColor(Color.red);       
     if(Squashed == 1 )
     {
      g.fillOval(x_position, y_position, 33, 25);
      Squashed = 0;
     }
     else
     {
      g.fillOval(x_position, y_position, 40, 40);
     }
 }

 //It will display the ball motion
private void motion()  
   {      
     //Variables updation
     oldx_position = x_position;      
     oldy_position = y_position;      
 
        if (Direction_On_Y == 0)
       {      
        //calculate the distance
        dist = velocity + (1/2)*gravity;    
        //Calculate the velocity      
        velocity = velocity + gravity;      
        //Calculate Y posiiton
         y_position+=(int)dist/10;
        //If ball reached bottom
        if (y_position > 300)
         {
           SquashedTheBall();
           Direction_On_Y = 1;                         
          //Adjust for going below 390...  
         dist = y_position - 300;      
         velocity = Math.sqrt((velocity*velocity) -  2*gravity*dist);
           Temporaray = velocity * (BouncingFactor/50);  //Calculate the new velocity because of collision.    
           velocity = velocity - Temporaray;      
         y_position = 300;
        }
     }
      //If Ball is bouncing up, do calculations and adjust y_position accordingly      
      
      else      
      {
        
      if(velocity <= 0)
       {
            Direction_On_Y = 0;
         }
      else
       {
             dist = velocity + (1/2)*(gravity);
             velocity = velocity - gravity;           
             y_position -= dist/20;
         }       
            
               
      if(y_position <= 0)      
      {
      Direction_On_Y = 0;
      y_position = 0;
      }

  }            
       //If ball is moving on right hand
       if (Direction_On_X == 0)
          {  
            //Move the ball by the factor of 2
            x_position+=2;
         
      if (x_position > 800)
         {
           // If it is opposite
           Direction_On_X = 1;
             x_position = 390;
         }
       }
      else
          {
          x_position-=2;

         if (x_position <= 0)
         {  
            Direction_On_X = 0;
              x_position = 0;
         }
        }
       repaint();  
     }
 
   
 
 }







0
Comment
Question by:redgreenred
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 2660074
so - add two buttons, add ActionListeners and increase / decrease the 'gravity' variable value on button press.
0
 

Author Comment

by:redgreenred
ID: 2661734
Code required
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2663282
:)

I won't post code.
'no homework zone'.
0
 

Accepted Solution

by:
aniket040700 earned 100 total points
ID: 2693266
well i have done a little modification ie. used the actionListener interface in ur code.

see if this is what u wanted or anything else.



import java.applet.Applet;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;
import java.awt.*;

//Creating a Main class Moving Ball
public class BouncingBall extends Applet implements Runnable, ActionListener
{
      //Variable Initialization  
      int Direction_On_X = 0;    
      int Direction_On_Y = 0;
      int x_position     = 0;      
      int y_position     = 0;
      int oldx_position  = 0;  
      int oldy_position  = 0;
      int Squashed      = 0;
      
      //For movemnt Calculation
      Button B1;
      Button B2;
      double gravity         = 9.83;
      double dist            = 0.0;
      double velocity        = 0.0;  
      double BouncingFactor  = 0.4;  
      double Temporaray;
      Double Doublenum;
      String bname;
      
      //Initializing a thread
      Thread thread_Instance;
      
      //Method to squashed the ball
      public void SquashedTheBall()
      {
            Squashed = 1;
            repaint();  
      }

      //Method to beginthe animation , calling by start method
      public void Begin()
      {
            if (thread_Instance == null)
            {
                  thread_Instance = new Thread(this);
                  thread_Instance.start();
            }
      }


      //It will satrt the animation
      public void start()    
      {
            Begin();        
      }
      
      public void run()    
      {
            B1=new Button("+ gravity");
            B2=new Button("- gravity");
            
            add(B1);
            B1.addActionListener(this);
            
            add(B2);
            B2.addActionListener(this);
            
            Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
            while (true)
            {    
                  motion();    
                  try
                  {
                        Thread.sleep(50);    
                  }
                  catch (Exception sleepProblem)
                  {}
            }
      }
      
      public void actionPerformed(ActionEvent e)
      {
            System.out.println("u r in");
            if(e.getSource()==B1)
            {
                  if(gravity>1)
                  {
                  System.out.println("u r in 1");      
                  gravity=gravity+10;
                  }
                  else
                  {
                        gravity=9.81;
                  }
            }
            
            if(e.getSource()==B2)
            {
                  if(gravity>1)
                  {
                        System.out.println("u r in 2");
                        gravity=gravity-10;
                  }
                  else
                  {
                        gravity=9.81;
                  }
            }
      }

      //Painting the ball , normaly and Squashed
      public void paint(Graphics g)  
      {
            g.setColor(getBackground());    
            g.fillOval(oldx_position,oldy_position,40,40);
            g.setColor(Color.red);
            if(Squashed == 1 )
            {
                  g.fillOval(x_position, y_position, 33, 25);
                  Squashed = 0;
            }
            else
            {
                  g.fillOval(x_position, y_position, 40, 40);
            }
      }

      //It will display the ball motion
      private void motion()  
      {
            //Variables updation
            oldx_position = x_position;
            oldy_position = y_position;        
            
            if (Direction_On_Y == 0)
            {
                  //calculate the distance
                  dist = velocity + (1/2)*gravity;    
                  
                  //Calculate the velocity
                  velocity = velocity + gravity;      
                  //Calculate Y posiiton
                  y_position+=(int)dist/10;
                  //If ball reached bottom
                  if (y_position > 300)
                  {
                        SquashedTheBall();
                        Direction_On_Y = 1;                    
                        
                        //Adjust for going below 390...  
                        dist = y_position - 300;
                        velocity = Math.sqrt((velocity*velocity) -  2*gravity*dist);
                        Temporaray = velocity * (BouncingFactor/50);  //Calculate the new velocity because of collision.      
                        velocity = velocity - Temporaray;        
                        y_position = 300;
                  }
            }

            //If Ball is bouncing up, do calculations and adjust y_position accordingly

            else
            {
                  
                  if(velocity <= 0)
                  {
                        Direction_On_Y = 0;
                  }
                  else
                  {
                        dist = velocity + (1/2)*(gravity);
                        velocity = velocity - gravity;      
                        y_position -= dist/20;
                  }

                  
                  if(y_position <= 0)
                  {
                        Direction_On_Y = 0;
                        y_position = 0;
                  }

            }
            //If ball is moving on right hand
            if (Direction_On_X == 0)
            {  
                  //Move the ball by the factor of 2
                  x_position+=2;
                  
                  if (x_position > 800)
                  {
                        // If it is opposite
                        Direction_On_X = 1;
                        x_position = 390;
                  }
            }
            else
            {
                  x_position-=2;

                  if (x_position <= 0)
                  {  
                        Direction_On_X = 0;
                        x_position = 0;
                  }
            }
            repaint();  
      }
      
      
      
}

//from redgreenred
0
 

Author Comment

by:redgreenred
ID: 2696701
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example using maven 12 77
eclipse console opening separately 2 37
how to debug htl and js pages 8 54
Eclipse with various Java releases 7 47
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question