how to make button move in a sigle row

sushant005

sushant005

@sushant005-tyt4WK Oct 23, 2024
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);
}

}
}
/*<applet code="fi.class" width="300" height="400"></applet>*/

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

Welcome, guest

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

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Sep 19, 2010

    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.*;
    /*<applet code="fi.class" width="300" height="400"></applet>*/
    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

    @sushant005-tyt4WK Sep 19, 2010

    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;
    }

    }
    }
    /*<applet code="fi.class" width="300" height="400"></applet>*/