Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My first attempt at a basic java applet failed. i cant figure out why the rectangle does not move

Tags:

java

applet

public class Basics extends Applet{

int x = 0;

int y = 0;

    public void init(){
        setSize(500,500);
    }

    public void start(){
        Thread a = new Thread();
        a.start();
    }

    public void run(){
        while(true){
            x = 100;
            y = 100;                
            repaint();
            try{
                Thread.sleep(18);
            }
            catch(InterruptedException e){}
        }

    public void paint(Graphics g){
        g.setColor(Color.red);
        g.fillRect(this.x,this.y,25,25);
    }

}

Shouldnt incrementing x and y and then repainting allow the square to move

like image 669
Zal Soonawalla Avatar asked Jan 17 '26 14:01

Zal Soonawalla


1 Answers

You should increment your x and y value, now you are only assigning values to it. Change it like this:

public void run(){
        while(true){
            x += 100;
            y += 100;                
            repaint();
            try{
                Thread.sleep(18);
            }
            catch(InterruptedException e){}
        }
like image 53
Tom Jonckheere Avatar answered Jan 20 '26 04:01

Tom Jonckheere



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!