help me to solve it

TaraJava

TaraJava

@tarajava-fqIQFu Oct 22, 2024
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • sookie

    sookie

    @sookie-T06sFW Jul 8, 2012

    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

    @tarajava-fqIQFu Jul 9, 2012

    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

    @sookie-T06sFW Jul 9, 2012

    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

    @tarajava-fqIQFu Jul 9, 2012

    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