how to make button move in a sigle row

import java.awt.*;
import java.applet.*;


public class fi extends Applet implements Runnable
{
Button b[] = new Button[1];
int x1=0;
Thread t;
public void init()
{
for(int i=0;i<1;i++)
{
setLayout(new FlowLayout());
b = new Button();
add(b);
}
}
public void start()
{
t = new Thread(this);
t.start();
}
public void run()
{
Thread t1=Thread.currentThread();
while(t==t1)
{

try
{
repaint();
Thread.sleep(1000);
setBackground(Color.blue);
}
catch(Exception e){}
}
}
public void paint(Graphics g)
{
for (int j=0;j<1;j++)
{
b[j].setSize(15,6);
x1=(x1+3)%200;
b[j].setLocation(x1,100);
}

}
}
/**/

Here is my code for moving button in a single row , but the problem i m facing is that it is moving very fast i want that to move per second means button move forward after each second...
Is this my code correct or need some more implementation or us there any another way to approach this...

Replies

  • Manish Goyal
    Manish Goyal
    Check it out and say cheese

    You had used many unwanted things ,
    Always try to make the code as simple as possible

    import java.awt.*;
    import java.applet.*;
    import javax.swing.*;
    /**/
    public class fi extends Applet implements Runnable
    {
        Button b[] = new Button[1];
        int x1=0;
        Thread t;
        public void init()
        {
            setLayout(new FlowLayout());
            b[0] = new Button();
            add(b[0]);
            setBackground(Color.blue);
        }
    public void start()
        {    
            t = new Thread(this);
            t.start();
        }
    public void run()
        {
    
        try    
            {
                for(;;)
                {
                    repaint();
                    
                    x1=(x1+3)%200;
                    b[0].setLocation(x1,100);
                    Thread.sleep(1000);
                }
            }
            catch(Exception e){}
        }
    
    
    }
    
  • sushant005
    sushant005
    Thanks goyal for your coding

    actually i want to set the size of button so i have used paint method here.
    actually i am practicing many things in a single code thats why many unnecessary things are left.
    Here i found my mistake just i have to use the break statement.

    import java.awt.*;
    import java.applet.*;


    public class fi extends Applet implements Runnable
    {
    Button b[] = new Button[1];
    int x1=0;
    Thread t;
    public void init()
    {
    for(int i=0;i<1;i++)
    {
    setLayout(new FlowLayout());
    b = new Button();
    add(b);
    }
    }
    public void start()
    {
    t = new Thread(this);
    t.start();
    }
    public void run()
    {
    Thread t1=Thread.currentThread();
    while(t==t1)
    {

    try
    {
    repaint();
    Thread.sleep(1000);
    setBackground(Color.blue);
    }
    catch(Exception e){}
    }
    }
    public void paint(Graphics g)
    {
    for (int j=0;j<1;j++)
    {
    b[j].setSize(15,6);
    x1=(x1+3)%200;
    b[j].setLocation(x1,100);
    break;
    }

    }
    }
    /**/

You are reading an archived discussion.

Related Posts

TechCrunch broke out news yesterday that world's most popular social network, Facebook is currently building their own phone just like Google & Apple. Today, they more or less confirmed the...
Easy Ways to Improve The analytical writing section is important and different from most of thebecause it is *marked by a human rather than a computer*! These human...
Hello..i am new to this forums..i am here to searching trainings in bio tech
i have a doubt that though 8085 is a 8-bit processors why do we give input as a two 4-bit values?😒
Hi i have made an ADC using a microcontroller such that if i change the resistance the digital value changes from 0 to 255. i have interfaced this to a...