help me to solve it

import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Color;
public class Main extends Applet implements Runnable
{
 
Thread t;
int x=500;
int y=745;
 
@Override
public void init()
{
setBackground(Color.black);
}
 
@Override
public void start()
{
if(t==null)
{
t=new Thread(this);
t.start();
}
}
@Override
public void stop()
{
if(t!=null)
t.stop();
}
 
public void run()
{
while(true)
{
 
if(y<250) //
{ //
y=y+5; //
 
} //
 
else
{
y=y-5;
}
repaint();
 
try
{
Thread.sleep(40);
}
catch(InterruptedException ex)
{}
}
}
 
@Override
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(x,y,50,50);
}
}
i have a problem in the commented line wham am i trying to do is first display the circle at the location and make it move upwards by reducing it by five and that is working fine...so than what do i want is after reaching certain position am making 250 i want the ball to return back so i just made it opposite by adding 5 now why is the logic not working ......please explain me the logic.....every part of the code is working fine beside that thanks

Replies

  • sookie
    sookie
    Hi #-Link-Snipped-#,

    Please check the below code if this is what you were expecting for the desired o/p
    import java.awt.Graphics;
    import java.applet.Applet;
    import java.awt.Color;
    public class Main extends Applet implements Runnable
    {
     
    Thread t;
    int x=500;
    int y=745;
    boolean goup=true;
    int count=0;
     
    @Override
    public void init()
    {
    setBackground(Color.black);
    }
     
    @Override
    public void start()
    {
    if(t==null)
    {
    t=new Thread(this);
    t.start();
    }
    }
    @Override
    public void stop()
    {
    if(t!=null)
    t.stop();
    }
     
    public void run()
    {
    while(true)
    {
     
        if(goup){
        y=y-5;
        }else{
      y=y+5;
        } 
       
        /* It initializes the y to 250 and circle moves in downward direction */
        if(y==250){
      System.out.println("Value reached 250");
      y=250;
      goup=false;
        }
        /* It initializes the y to 745 and circle moves in upward direction */
        if(y==745){
      System.out.println("Value reached 745");
      y=745; 
      goup=true;
        }
    repaint();
     
    try
    {
    Thread.sleep(40);
    }
    catch(InterruptedException ex)
    {}
    }
    }
     
    @Override
    public void paint(Graphics g)
    {
    g.setColor(Color.red);
    g.fillOval(x,y,50,50);
    }
    }
    
    The problem that I found in your logic was you were updating the 'y' value in while loop and at the same time it is used in "paint" . Moreover when your value of 'y' reaches 245 (<250) it goes in "if" block and again changes the value to 250 and again next time it comes in else loop and it decrements the value to 245. In this way your program keeps on looping between value 245 and 250.

    In order to avoid this I have controlled upward and downward journey using a boolean variable "goup" default is true means circle will move in "upward" direction" and when y reaches to 250, its value is set as "false" so that circle moves in "downward direction from 250 on wards.
  • TaraJava
    TaraJava
    ok thank you for the reply but i found a problem in running your program its having some bugs ,any way i got some ideas from your program .....I want to ask a next question how would i make a multiple balls moving up down in the same program ...waiting for your reply
  • sookie
    sookie
    TaraJava
    ok thank you for the reply but i found a problem in running your program its having some bugs ,any way i got some ideas from your program .....I want to ask a next question how would i make a multiple balls moving up down in the same program ...waiting for your reply
    1. Can you Please post the bugs found in my program. It is working fine at my end. I have just changed while loop and added two variables('count' can be removed..😳) in same program of yours.
    2. Program for multiple balls -> First try writing it yourself.
  • TaraJava
    TaraJava
    am confused why you been doing this
    if(y==250){
    System.out.println("Value reached 250");
    y=250;
    goup=false;

    why are you assigning 250 to y the value of y is 250 only.when i run ur program first the ball
    moves upward but reaching the point it cant come back..

    2.making the multiple balls let me not be lazy yea am trying ...thanks anyway

You are reading an archived discussion.

Related Posts

The system deals with security during transmission of data. Commonly used technology is cryptography. This system deals with implementing security-using Steganography. Abstract: In this technology, the end user identifies an...
The idea is to perform various activities in an organization such as maintaining employees where about's, bulletin board, reservation facility for training and meeting rooms. Abstract: Virtual Office Management displays...
The idea is to create an application on android based smart phone that enables access to a twitter account and provide with its functionalities. Abstract: To develop an Android client...
Water Desalination Using Graphene Possible – Motivation For Chemical Engineers? Leap Second Bug Proves That One Extra Second Can Create Havoc IBM Labs Create Augmented Reality App For Shopping Solar...
This Implementation and validating environmental and health is intended for the medical professionals like Doctors, optometrists etc who need to communicate frequently to take quick decisions. This is an internet...