0

I have the following code, everything seems to be working except for the while loop, here is the code:

JLabel img = new JLabel(loadingScreens.getImageIcon(0));
    loadingFrame.setUndecorated(true);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    double width = screenSize.getWidth();
    double height = screenSize.getHeight();
    int wid = (int) width;
    int hgt = (int) height;
    wid = wid/2;
    hgt = hgt/2;
    wid -=350;
    hgt -=350;
    loadingFrame.setLocation(wid, hgt);

    loadingFrame.setSize(700, 700);
    loadingFrame.setBackground(new Color(0,0,0,0));
    loadingFrame.add(img);
    loadingFrame.setIconImage(loadingScreens.getImage(0));
    loadingFrame.setVisible(true);
    System.out.println("Done 1");
    try{
    Thread.sleep(500);
    System.out.println("Done 2");
    }catch(Exception e){
        System.out.println("exception caught");
    }

    Integer lo = 0;
    System.out.println("Done 3");
    while(lo.equals(256)){

        System.out.println("Started 4");

        loadingFrame.setBackground(new Color(lo, lo, lo, lo));

        loadingFrame.repaint();
        try{
            Thread.sleep(10);
        }catch(Exception e2){

        }
        lo++;
    }

loadingFrame being a basic JFrame.

Any help is useful

5
  • What is lo.equals(256) ? 0.equals(256) ? Commented Jun 18, 2013 at 18:40
  • 2
    Well, it's pretty obvious. Your lo contains the value 0, and it's not equal to 256. Commented Jun 18, 2013 at 18:40
  • but add the end I have lo++ which should add one Commented Jun 18, 2013 at 18:41
  • @user2317720, you are setting that value inside the loop -- that very loop you've pointed out never starts. Commented Jun 18, 2013 at 18:42
  • It's a "while" loop, not an "until" loop. Commented Jun 18, 2013 at 18:43

2 Answers 2

5

A while loop loops while the specified condition is true. You've initialized lo to 0, which is not equal to 256, so the loop body is never entered.

Because you increment lo in the loop, perhaps you meant the opposite:

while(!lo.equals(256)){

The ! operator in Java negates the boolean condition, so that it reads: "while lo is not equal to 256".

Sign up to request clarification or add additional context in comments.

Comments

1

This because while always start with a truth statement not a false and your lo.equals("256") is giving false because lo = 0 and zero never ever equals 256.
If you want to start the loop you have to negation the condition like this :

while(!lo.equals("256") 
//then start the loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.