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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 39

Thread: Help Please

  1. #1
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help Please

    hi i have a slight problem with my code, i am looking to update all posisions in an array for example


    private int [] [] ovl = new int [maxovl][4];

    ovl [0] [0]+=dx;
    ovl [0] [1]+=dy;


    if(ovl [0] [0]-rad<0)
    {
    dx=-dx; // this reverses the ball away from the wall
    ovl [0] [0]=rad;

    }

    now i was wondering if there was a simpler way than doing
    ovl [0] [0]+=dx;
    ovl [0] [1]+=dy;
    ovl [1] [0]+=dx;
    ovl [1] [1]+=dy;
    ovl [2] [0]+=dx;
    ovl [2] [1]+=dy;
    ovl [3] [0]+=dx;
    ovl [3] [1]+=dy;

    and so on.

    thanks for reading
    Last edited by viper_07; July 10th, 2011 at 11:52 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    You could write a loop to access the elements vs having the elements individually coded.
    Have the loop variable index the first dimension of the array.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    Your post wasn't anything useful. If you need to copy the console:
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  4. #4
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    its a gui interface i pasted the section of the code, would you like me to paste bin the whole program so you can see what is going on properly
    also there is no compile errors
    Last edited by viper_07; July 10th, 2011 at 01:24 PM.

  5. #5

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    there is no compile errors
    What is the problem then? Can you explain?

    Please post any code you are asking questions here on the forum.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    Your code has very few comments describing its logic. It will take a long time to figure out what you are trying to do.
    Have you tried debugging it by adding lots of println statements to show the values of variables as they are changing. You should be able to look at the output and see where the program is NOT doing what you want it to do by seeing how the variables are being set and changed.
    That is exactly how I will attempt to find the logic problem.

  8. #8
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    yeah i had a look through the logic the for loop breaks the while as each thread needs to be running in the while at all times, the for loop runs through 1 2 3 individualy shutting off the other threads, so all threads need to be running at the same time with no break in there like

    while(true)
    {


    ovl [0] [0]+=dx;
    ovl [0] [1]+=dy;



    if(ovl [0] [0]-rad<0)// this checks to see if there is a horsontal collision
    {
    dx=-dx; // this reverses the ball away from the wall
    ovl [0] [0]=rad;


    }else if(ovl [0] [0]+rad>canvas_width)
    {
    dx=-dx;
    ovl [0] [0]=canvas_width-rad;

    }



    if(ovl [0] [1]-rad<0)// this is the same as above but for the verttical collision
    {
    dy=-dy; // this reverses the ball away from the wall
    ovl [0] [1]=rad;

    } else if(ovl [0] [1]+rad>canvas_height)
    {
    dy=-dy;
    ovl [0] [1]=canvas_height-rad;

    }


    repaint();

    works perfectley but for one instance
    so in order to fix it
    ovl [0] [0]
    ovl [0] [1]
    needs to represent all items in the array at the same time
    but i am not sure how to do it

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    ovl [0] [0]
    ovl [0] [1]
    needs to represent all items in the array at the same time
    Sorry, I don't understand what that means.

    So far I have found two problems by adding printlns and looking at the output.
    How are you doing?

  10. #10
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    array line No in array collum
    ovl [0] [0] = X cordinates

    ovl [1] [0] = Y cordinates


    i have been looking at the program testing it but i can't find any errors i dont know if its because i have been doing it for a while
    what problems did you find?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    Post your printed output and I point out where the problems are.
    Or I'll tell you where you should add more print outs to see the problem.

  12. #12
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    here is what i got:

    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    start while dx-1
    start while dy-1
    4th if dy1
    4th if ovl20
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1
    start while dx-1
    start while dy1

    X:\Desktop\Programing Resit#\tests\bb2\test 2\test3>

  13. #13
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    that without the for loop

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    Pretty boring. When posting long repeating group, use ... in the middle.
    You print outs don't show a lot of things about your program.
    One thing they do show is that there is only one delta value. That's fine for one ball, but what if you have more than one ball and they are moving in different directions?

    When is a new RunnableThread created?
    When is that class's run method called?
    System.out.println("run for " + Thread.currentThread().getName());


    How is each ball being moved?
    System.out.println("moving ball=" + i + ", x=" + ballData[i][0] + ", y=" + ballData[i][1]);

    What is the data for a newly created ball:
    System.out.println("new ball= " + java.util.Arrays.toString(ballData[nbrOfBalls]));


    I need descriptive names for variables so I changed these:

    private final int MaxBallData =50;
    private int [][] ballData = new int [MaxBallData][4]; // What are the 4 dims used for???
    private int nbrOfBalls =0;

  15. #15
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    a new runnable thread is created when the user clickes
    the run method is called when a thread starts

    i found that it runs thread1 then Thread - 3 which i presume is the sleep time it dose that 4 times one for each statment the it goes back to thread1

    the ball moves between 20 and 21 on the x axis and random on the y which is strange i admit

    and there is no data for a new ball this is because a new ball is not enter into the while loop

    i am not sure about the dims, i just know the program dose not work without them.

    i am sorry to keep you looking over this thread, i have not been programing java long i am in my second year at uni and arrays are not my strong point,

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    a new runnable thread is created when the user clickes
    the run method is called when a thread starts
    What is printed out by the printlns you put in the run method?
    Are the print outs what you expected?

  17. #17
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    yes apart from when the new ball is added it is as tho it never go created

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    public void run()
    {
     
          System.out.println("run for " + Thread.currentThread().getName());  //<<<<<
     
     
    while(true)
    {

    Add this println to your code as shown above and execute it.
    Look at what is printed out.

  19. #19
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    this is the print outs i got:
    X:\Desktop\Programing Resit#\tests\bb2\test 2\test3>javac *.java

    X:\Desktop\Programing Resit#\tests\bb2\test 2\test3>java Bouncing_Ball
    run for thread1
    run for Thread-3
    run for thread2
    run for Thread-4
    run for thread3
    run for Thread-5
    X:\Desktop\Programing Resit#\tests\bb2\test 2\test3>

    it runs the threads ok but the Thread-3 4 5 is abit strange i dont get why it is adding sleep time

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    run for Thread-3 <<<<<<<<<<<<<
    moving ball=0, x=551, y=580
    new ball= [0, 0, 0, 0]
    run for Thread-3 <<<<<<<<<<<<<
    moving ball=0, x=780, y=350
    new ball= [0, 0, 0, 0]
    run for thread1 <<<<<<<< New one
    moving ball=0, x=449, y=20
    new ball= [0, 0, 0, 0]
    run for thread1 <<<<<<<<< called again???
    Should run be called twice every time a new ball is added?
    How many threads are supposed to be running for a ball?

    Run the test and only add one ball. How many threads are shown by the print out?

  21. #21
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    no the run method should only be run once as it should continue to run
    1 thread per ball should be created

    i get

    X:\Desktop\Programing Resit#\tests\bb2\test 2\test3>javac *.java

    X:\Desktop\Programing Resit#\tests\bb2\test 2\test3>java Bouncing_Ball
    run for thread1
    run for Thread-3

    X:\Desktop\Programing Resit#\tests\bb2\test 2\test3>

    1 thread per print

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    If you only started one ball by clicking one time with the mouse, then why is the run method called two times? Do you wonder why you got two lines printed out?
    It looks like you are creating and starting two threads.

  23. #23
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    yes that is abit strange, but how can that happen it must be because when
    public RunnableThread(String s)
    {
    ball = new Thread(this,s);
    //ball.start();


    i am starting the ball here when i run it with this commented out there is only one print out


    X:\Desktop\Programing Resit#\tests\bb2\test 2\test3>javac *.java

    X:\Desktop\Programing Resit#\tests\bb2\test 2\test3>java Bouncing_Ball
    run for Thread-3

    X:\Desktop\Programing Resit#\tests\bb2\test 2\test3>

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Please

    Yes that looks right. Also you could get rid of the creation of the thread: ball = new Thread...
    The Thread was created and started in another place in your code.

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

    viper_07 (July 10th, 2011)

  26. #25
    Junior Member viper_07's Avatar
    Join Date
    Jul 2011
    Location
    manchester
    Posts
    23
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Please

    yeah that was careless of me to have put that there so now only the threads i need are created, but i still can't get to grips with why the out put is
    messed when the for loop is in place

Page 1 of 2 12 LastLast