Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Problem with for each cicles

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with for each cicles

    Hi guys
    i have a problem, first i have two cicles for each to print a line like a cross to an object, thats actualize the line when the object moves, my problem is when i got two objects pointing to the same object, the cross line of the first targeting him, dont desapears from the map and i dont know why.

    sorry for my bad english
    Thanks
    Entertainer


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with for each cicles

    Please post the code you are having problems with, post what it prints out and explain what is wrong with the printout.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with for each cicles

    bug.jpg

    for (SimpleImage tower : towers) {
    for (MovingImage x : invaders) {
    if (Math.abs(tower.getPosition().distance(x.getPositi on())) < RANGE) {

    SimpleShape s = new SimpleShape(
    new Line2D.Double(tower.getPosition().getX()
    + tower.getDimension().width / 2, tower
    .getPosition().getY()
    + tower.getDimension().height / 2, x
    .getPosition().getX()
    + x.getDimension().width / 2, x
    .getPosition().getY()
    + x.getDimension().height / 2), Color.blue, 50);

    shot = s;
    window.getDisplayer().add(s);



    }
    }
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with for each cicles

    How can the code by compiled and executed for testing? Can you make a small simple program that compiles, executes and shows the problem.

    Be sure to wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Entertainer (March 26th, 2013)

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with for each cicles

    if (shot != null) window.getDisplayer().remove(shot);  
            for (SimpleImage tower : towers) {  
            for (MovingImage x : invaders) {  
     
                    if (Math.abs(tower.getPosition().distance(x.getPosition())) < RANGE) {  
     
                        // if (shooting == true) { // to target one target at once   
     
     
                        System.out.println(shot);  
                        SimpleShape s = new SimpleShape(  
                                new Line2D.Double(tower.getPosition().getX()  
                                        + tower.getDimension().width / 2, tower  
                                        .getPosition().getY()  
                                        + tower.getDimension().height / 2, x  
                                        .getPosition().getX()  
                                        + x.getDimension().width / 2, x  
                                        .getPosition().getY()  
                                        + x.getDimension().height / 2),  
                                Color.green, 100);                  window.getDisplayer().refresh();  
                                        shot = s;  
     
                            window.getDisplayer().remove(shot);  
     
                                                    shooting = false;  
     
                        window.getDisplayer().add(s);  
     
                        }  
     
                    }  
                }
    My objective is target two objects at the same time( two or more towers pointing at objects if they are in range), i think the problem is when we change the tower s(variable) turns null but i dont know why...

    Image:bug.jpg

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with for each cicles

    Can you make a small simple program that compiles, executes and shows the problem.

    Please edit the post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    when we change the tower s(variable) turns null
    Where does the null value come from?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with for each cicles

    i think when two objects tower change the value of variable s, my ideia is dont let second tower change s value but i dont know how can i do it ;/

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with for each cicles

    dont let second tower change s value
    How do you know whether it is the first tower or the second tower? Can you use that information to stop second tower from making any changes.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with for each cicles

    thats is the problem i dont know which tower acess to variable s and i dont know how to restrict that ;/

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with for each cicles

    Without a SSCCE there isn't much more I can do.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  2. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM