Results 1 to 7 of 7
  1. #1
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default if statements not working

    I'm trying to have an if statement control what is drawn with this code:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class DrawingLines extends Applet {
    
       int width, height;
       boolean wait = true;
    
       public void init() {
          width = getSize().width;
          height = getSize().height;
          setBackground( Color.black );
       }
    
       public void paint( Graphics g ) {
          g.setColor( Color.green );
          if (wait == true) {
              g.drawLine(10,10,40,40);
          }
          else {
              g.drawRect(20, 20, 100, 100);
          }
       }
    }
    I have no clue why it just keeps drawing a rectangle because I'm not changing the wait value from true and it would have to be false to make the rectangle. Is there something wrong with my if statement, or is there another way I should be doing this?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code posted above is in fact drawing a line, not a rectangle, so the only conclusion I can come up with is that the code you are running is not the code you are showing. Can happen.

  3. #3
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    When I run it, it doesn't draw a line, this is the exact code I have been using. try and compile and run it in an applet, you'll see it draws a rectangle that isn't filled in.

  4. #4
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Never mind, now it seems to be working for some reason, maybe I never saved it.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by pizzadude223 View Post
    Never mind, now it seems to be working for some reason, maybe I never saved it.
    As Fubarable says, your are ruining a different code and showing a different here. Code you've posted here not drawing a rectangle, just a line on top-left corner of the applet window.

    Anyway, if you've solved the problem then please mark the thread as solved.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pizzadude223 View Post
    Never mind, now it seems to be working for some reason, maybe I never saved it.
    What surprises me is that the sudden change in behaviour doesn't surprise you. Computer science must be a bag of miracles for you ...

    kind regards,

    Jos

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 08:02 PM
  2. Replies: 6
    Last Post: 11-12-2009, 11:50 AM
  3. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 05:24 PM
  4. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •