Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    Lrjavier's Avatar
    Lrjavier is offline Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    13
    Rep Power
    0

    Default

    Printlns aren't helping me figure out why the remove button is erasing all my shapes at once after a while. If I go back and comment on the purpose of everything, can you take a look at it again?

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Printlns aren't helping me figure out why
    When you code a program you expect the variables to change in a certain way and for the execution flow to go here and then there. Printlns show all that.

    I can look at it again, but I'm not going to tell you what is wrong with your code, unless you ask a specific question.For example; "Why is this statement not working: if (booleanVar = true)" I'll explain that you need to use == to test, the = assignment operator will set the booleanVar to true and it also returns true to the if ().
    When I find the problem and it is a logic problem I'll tell you how (mostly using prints) and maybe where to look.
    If its a misuse of some class, I'll explain how to use it.

  3. #23
    Lrjavier's Avatar
    Lrjavier is offline Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    13
    Rep Power
    0

    Default

    In my drawShapes method

    Java Code:
    	public void drawShapes() {
    		int i = 0;
    		checkNum = (int) (100 * Math.random());
    		while (found == false) {
    			if (checkNum >= 1 && checkNum <= 16) {
    				newNum = checkNum;
    				found = true;
    			} else {
    				checkNum = (int) (100 * Math.random());
    			}
    		}
    		while (i < newNum) {
    			number = Math.random();
    			if (number >= 0 && number <= .5) {
    				shapes[i] = new Square();
    			} else if (number > .5) {
    				shapes[i] = new Circle();
    			}
    			i++;
    		}		
    	}
    I use the newNum variable to determine the amount of shapes that will be displayed, because I need to check against it in the paint method, I have to declare it as an instance variable, but this doesn't allow for it to change every time I click the button so that it displays a different amount of shapes, I'm looking for a way around that!

    In my check method, the remove button is supposed to erase the elements of the array in a random order one by one, but eventually it erases everything as I press the button, I can't figure out why it's doing that.

    Java Code:
    int randomNumber = (int)(100 * Math.random());
    			while(randomNumber < 0 || randomNumber > newNum) {
    				randomNumber = (int)(100 * Math.random());
    			}
    That's the code for the remove button inside the check method.

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    You have a lot of conditional logic in the drawShapes method. For example: (i < newNum)
    Do you know what the values of the variable are each time you execute that method? Add some printlns to print them ALL out so you can see what they are and then you can see where the problem in your logic is.

  5. #25
    Lrjavier's Avatar
    Lrjavier is offline Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    13
    Rep Power
    0

    Default

    I figured it out! The problem was caused by a boolean variable that I used to control the loop that generated random numbers between 1 and 16, this variable wasn't being reset to false so the number wasn't generated again since the loop wasn't accessed. As for the erasing, I decided not to do it randomly, but to start at the last index of the array and move down! Thanks for the help!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. MouseListener problem
    By s3ib in forum New To Java
    Replies: 10
    Last Post: 01-03-2011, 01:20 PM
  2. Strategy Game: MouseListener problem
    By Controloler in forum Java Gaming
    Replies: 0
    Last Post: 12-10-2010, 03:11 AM
  3. Problem with mouseListener
    By js91723 in forum AWT / Swing
    Replies: 12
    Last Post: 05-31-2010, 07:49 PM
  4. Problem with mouseListener (REPOST)
    By js91723 in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2010, 05:15 PM
  5. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 03:30 AM

Posting Permissions

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