HELLO guys IM beginner using java IM asking what is my problem of my codes?? can you fix it??

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class invalid3 extends Applet implements Runnable {
/**
* 
*/
private static final long serialVersionUID = 1L;
private int x0 , y0 , incx , incy,height0, width0;
private int x1, y1,width1,height1;
private Thread boxOne;
private Thread boxTwo;
public void init() {
    resize(800,1000);
    // box one;
    x0=0;
    y0=0;
    width0=300;
    height0=300;
    boxOne = new Thread(this);
    boxOne.start();
 // box two
    x1=900;
    y1=0;
    width1=300;
    height1=300;

    boxTwo = new Thread(this);
    boxTwo.start();

}
public void update(Graphics g) {
    g.setColor(getBackground());
    g.fillRect(0, 0, 800, 1000);
    g.fillRect(900,0, 800,1000);
    paint(g);
}
public void paint(Graphics g) {
    g.setColor(Color.BLACK);
    g.drawRect(x0, y0, width0, height0);
    g.setColor(Color.BLUE);
    g.drawRect(x1, y1, width1, height1);
}
@Override
public void run(){
    // TODO Auto-generated method stub
        if(Thread.currentThread() == boxOne)    {
        while(true){
            if(x0 < width0 && y0 == 0){
                incx = 5;
                incy = 0;
            }

            if(x0 == width0 && y0 < height0){
                incx = 0;
                incy = 5;
            }

            if(x0 <= width0 && y0 == height0){
                incx = -5;
                incy = 0;
            }

            if(x0 == 0 && y0 > 0){
                incx = 0;
                incy = -5;
            }
            x0 = x0 + incx;
            y0 = y0 + incy;

        try{
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        repaint();
    }
    }

        if(Thread.currentThread() == boxTwo)    {
        while(true){

            if(x1 < 600 && y1 == 0){
                incx = -5;
                incy = 0;
            }
            if(x1 == 600 && y1 < height1){

                incx = 0; 
                incy = -5;

            }
            if(x1 >= 900 && y1 == height1){
                incx = 5;
                incy = 0;
            }

               if(x1 == 600 && y1 < 0){
                incx = 0;
                incy = 5;
            }

            x1 = x1 - incx;
            y1 = y1 - incy;


    try{
        Thread.sleep(100);
        } catch (InterruptedException e) {
        e.printStackTrace();
        }
        repaint();
    }
    }
}}

My problem is the boxTwo. the fourth if, is getting false since the right to left and up to down and left to right is reading but the down to up is not reading can anyone help me fix my code???

sorry for my english if i have mistake....

Recommended Answers

All 5 Replies

Just comparing box2 with box1, box1 has y0 > 0, but box2 has y1 < 0. Is that right?

if i put > on y1 the output of the box is going to right to left and reapting up down up down.. can you help me fix it??

It's not obvious to me what this code is supposed to be doing. Can you describe in English what these two boxes should do?

the output of the the two box is the box 1 must rotate from her x = 0 to right , and then down , left and up back to the x = 0 and my problem is the boxTwo from x = 900 must be left down right and up back to x = 900 ...

you must run it so it easy to understand my point..

sorry for my english..

OK. Yes, I understand that.

The box2 if tests seem confused. Did you start by copying the box1 code and trying to edit it?
I would start again, following the requirements, in simple pseudo-code eg
(x is 900, y is 0) -> move left
(x is 600, y is 0) -> move down
etc

... then translate those into Java code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.