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 8 of 8

Thread: Can't seem to get starting position right

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Can't seem to get starting position right

    So I have this code that I made to make a ball bounce off the sides of the window no matter what the size of it, yet, I don't know what to do about this problem. Towards the bottom of the code, there is too parts that I have written. One updates with the resizing of the window and has the ball bounce off accordingly and the other has it bounce on invisible walls so the size of the windows doesn't affect it.

    The problem I'm having with it is the fact that I want to have it start off in different places and eventually get it to start off at the bottom of the screen so I can add gravity and make it look like a regular bouncing ball. When i run the one with numbers I can set the ball to start where I want it to but when I run the one with getWidth and getHeight, no matter what numbers I put, it starts off at the corner of the screen on the top left hand corner. Can someone please help me and explain to me what is wrong with my code?

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package bouncing.ball;
     
    /**
     *
     * @author Marlin
     */
    import javax.swing.*;
    import java.awt.*;
     
    public class BouncePanel extends JPanel {
     
        //location of bouncing ball
        private int x;
        private int y;
        private int dx = 5;
        private int dy = 5;
     
        BouncePanel() {
            x = 100;
            y = 400;
     
            AnimationThread animethread = new AnimationThread(this);
            animethread.start();
        }
     
        public void paintComponent(Graphics g) {
            //clear screen
            super.paintComponent(g);
     
            g.setColor(Color.BLUE);
            g.fillOval(x, y, 40, 40);
        }
     
        public void update() {
     
            x += dx;
            y += dy;
            if (x < 0) {
                x = 0;
                dx = -dx;
            }
            if (x + 40 >= getWidth()) {
                x = getWidth() - 40;
                dx = -dx;
            }
            if (y < 0) {
                y = 0;
                dy = -dy;
            }
            if (y + 40 >= getHeight()) {
                y = getHeight() - 40;
                dy = -dy;
            }
     
    //                if(x < 0){
    //            x = 0;
    //            dx = -dx;
    //        }
    //        if (x + 30 >= 400) {
    //            x = 400 - 30;
    //            dx = -dx;
    //        }
    //        if (y < 0) {
    //            y = 0;
    //            dy = -dy;
    //        }
    //        if (y + 30 >= 400) {
    //            y = 400 - 30;
    //            dy = -dy;
    //        }
     
            repaint();
        }
    }


  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: Can't seem to get starting position right

    How can the posted code be compile and executed for testing? It needs a JFrame and a main() method.

    , it starts off at the corner of the screen on the top left hand corner
    That would be the 0,0 location. How does the code set the starting location?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't seem to get starting position right

    Well, this is just a portion of it. The JFrame is set in the Main() method and all everything needed is set in another java class. The BouncePanel() is what I was messing with to edit the starting position.

  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: Can't seem to get starting position right

    What are you expecting to happen now? There needs to be a SSCCE
    http://sscce.org/
    for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't seem to get starting position right

    Forgive me if this is a stupid question but what is SSCCE? I read it and I'm not sure I catch the jist of it. As for what I want to happen, I just want to be able to set the starting position of the ball myself.

  6. #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: Can't seem to get starting position right

    , I just want to be able to set the starting position
    I see that the code sets the starting position:
          x = 100;
          y = 400;

    what is SSCCE?
    Short, Self Contained, Correct Example
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't seem to get starting position right

    Actually what I meant to ask was, what exactly is short, self contained, correct example? I read it a bit but I didn't quite understand what it was. Here is what I am thinking it is, I think it is a way of running my program to show where the fault in it is so I can explain it more easily but then again I'm still fairly new to all this so I'm not sure. Am I about in the ball park?

    As for the code, yes, those two varibles are what control the starting position of my ball but when I use this method it seems to work only when I use the commented out part. The part that is not commented out makes the ball start off at the 0,0 position no matter what I set x and y to in the previous part of the program. I'm not sure why this is. I tried running it in my head to see if I could see what the computer is seeing but I just don't seem to understand why it is doing what it is doing. I can post the entire thing of code if you need it, it's only three pages that are not that long. Maybe 300 lines of code or so.

    public void update() {
     
            x += dx;
            y += dy;
            if (x < 0) {
                x = 0;
                dx = -dx;
            }
            if (x + 40 >= getWidth()) {
                x = getWidth() - 40;
                dx = -dx;
            }
            if (y < 0) {
                y = 0;
                dy = -dy;
            }
            if (y + 40 >= getHeight()) {
                y = getHeight() - 40;
                dy = -dy;
            }
     
    //                if(x < 0){
    //            x = 0;
    //            dx = -dx;
    //        }
    //        if (x + 30 >= 400) {
    //            x = 400 - 30;
    //            dx = -dx;
    //        }
    //        if (y < 0) {
    //            y = 0;
    //            dy = -dy;
    //        }
    //        if (y + 30 >= 400) {
    //            y = 400 - 30;
    //            dy = -dy;
    //        }
     
            repaint();
        }

  8. #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: Can't seem to get starting position right

    what exactly is short, self contained, correct example?
    Something small that will compile, execute and show the problem.

    part that is not commented out makes the ball start off at the 0,0 position
    Where are the values set to 0? try debugging the code by adding lots of println statements to show why and where the values are set to 0.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Need serious help starting this assignment
    By MFThomas in forum Loops & Control Statements
    Replies: 1
    Last Post: November 18th, 2012, 07:36 PM
  2. Need help starting on Java
    By dfixx in forum Member Introductions
    Replies: 2
    Last Post: November 18th, 2012, 04:12 AM
  3. Starting a Path
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2012, 07:10 PM
  4. Help starting program, need done by Nov 4
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 4th, 2009, 07:22 PM
  5. Need help starting a program
    By Mickey2315 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 7th, 2009, 02:21 PM