Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default how to add more balls via user input

    I need to make a button that asks for the amount of balls the user wants on a screen. I'm not sure how to go about this.. The button action is located under actions preformed, where it says else if(e.getSource()==ballCount) I got the window to ask and store the number of balls, but I don't how to reproduce them on the screen. pleas help, thanks


    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.Random;
    
    public class RollB extends JPanel implements ActionListener{
      String inputSpeed;
      String inputCount;
      int numBall;
      int numSpeed; 
    
    
      Random random = new Random();
      int ranStart = random.nextInt(60) + 1;
      int width = 700; int height = 250; 
      Timer clock = new Timer(20,this); 
      int NWall = 0; int WWall = 0; int EWall = width; int SWall = height;
      int h = 3; int v = 2; 
      int x = 125; int y = 21;
      Ball ball = new Ball(x,y,50,h,v);
      JMenuBar b;
      JMenu menu = new JMenu("Game Controls");
      JMenuItem quit = new JMenuItem("Quit");
      JMenuItem stop = new JMenuItem("Stop");
      JMenuItem start = new JMenuItem("Start");
      JMenuItem speed = new JMenuItem("Speed");
      JMenuItem newStart = new JMenuItem("New Start");
      JMenuItem ballCount = new JMenuItem("Ball Count");
      
      public RollB(JMenuBar bar){
        
        clock.start();
        this.b=bar;
        b.add(menu);
        menu.add(quit);
        menu.add(stop);
        menu.add(start);
        menu.add(speed);
        menu.add(newStart);
        menu.add(ballCount);
        quit.addActionListener(this);
        stop.addActionListener(this);
        start.addActionListener(this);
        speed.addActionListener(this);
        newStart.addActionListener(this);
        ballCount.addActionListener(this);
        setPreferredSize(new Dimension(width,height));
        setBackground(Color.white);
        
      }
      
      
      public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawRect(0,0,width,height);
        drawBalls(g);
      }
      
      public void drawBalls(Graphics g){
        g.fillOval(ball.getX(),ball.getY(),ball.getR(),ball.getR());
      }
      
       public void reflectBalls(){
         // when ball reaches (crosses) a wall, it flips (bounces)
        if (ball.getY() <= NWall) ball.flipVertical();
        else if (ball.getY() >= (SWall-50)) ball.flipVertical();
        else if (ball.getX() <= WWall) ball.flipHorizontal();
        else if (ball.getX() >= EWall-50) ball.flipHorizontal();
      }
    
       public void advanceBalls(){
         // ball moves ahead h,v units
         ball.advance();
       }
       
      public void actionPerformed(ActionEvent e){ 
      
        if(e.getSource()==quit) System.exit(0);
        else if(e.getSource()==stop) {clock.stop();}
        else if (e.getSource() == clock) {
          advanceBalls();
          reflectBalls();
        }
        
        else if(e.getSource()==start) clock.start();
        else if(e.getSource()==speed){
          inputSpeed=JOptionPane.showInputDialog("Enter delay (smaller=faster)");
          numSpeed=Integer.parseInt(inputSpeed);
          clock.setDelay(numSpeed);
        }
        else if(e.getSource()==newStart){}
        
        else if(e.getSource()==ballCount){
          inputCount=JOptionPane.showInputDialog("Enter Ball Count");
          numBall=Integer.parseInt(inputCount);
          for(int x = 0; x < numBall; x++){
         drawBalls(Graphics_Object);
          }
          }
        repaint();
        
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to add more balls via user input

    What happens when you tried this? If there are compiler messages, post them to describe precisely what the problem is.

  3. #3
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: how to add more balls via user input

    Im sorry, drawBalls(Graphics_Object) shouldnt be there nor should the for loop above it. There're no errors, I just dont know how to print the number of balls on the screen.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to add more balls via user input

    That was what I thought.

    Basically you get a Graphics instance supplied to you as an argument to paintComponent() when the "system" decides to paint. It is at that point that you start painting balls.

    So... In the event handler you set numBall as you are doing. Then you call repaint() as you are doing. And that's basically all you do.

    The real action happens later in paintComponent(). In this method you have to paint all the balls.

    Java Code:
    public void drawBalls(Graphics g){
        for(Ball b :balls) {
            g.fillOval(b.getX(),b.getY(),b.getR(),b.getR());
        }
    }
    But what is balls?

    At the moment you declare an instance variable ball - but if you want multiple balls you should replace this with a collection of balls. If you are happy with List, then that would be best - otherwise balls could be an array.

    Notice that you don't really need a numBall variable any more since it's just the length of the array (or size of the list). And what the event handler should do is set the array (or list) up to have the right number of balls.

    Not only does drawBalls() have to change by working with the collection of balls rather than a single one, but the other methods involving ball (advancing and reflecting) also have to be rewritten so that they work on each ball of the collection.

    [Edit] If you decide on using an array rather than a list then the numBall variable will still be useful - since it records how many of the array elements are "valid".
    Last edited by pbrockway2; 12-10-2011 at 11:37 PM.

  5. #5
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: how to add more balls via user input

    I dont quite understand the for each loop, and it was giving me errors.. so i made this


    Java Code:
     public void drawBalls(Graphics g){
        for(int x= 0; x>20; x++){
        g.fillOval(ball.getX(),ball.getY(),ball.getR(),ball.getR());
      }
    Now i got no balls on the screen. Right now, I'm going to leave the enter ball count for later. I just wanna get a number of balls on the screen.

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

    Default Re: how to add more balls via user input

    You still need a ArrayList or array of Ball, balls, as pbrockway mentions above, and the for loop or for-each loop needs to iterate through each Ball in the list. Your code above does not do this while pbrockway's does. Have you tried to create one of these?

  7. #7
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: how to add more balls via user input

    How would i make an array of balls.. Sorry for the stupid question, I'm not good with arrays.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

  9. #9
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: how to add more balls via user input

    I got this with an identifier error

    Java Code:
    Ball ball = new Ball(x,y,50,h,v);
      int[] arrayB;
      balls = new arrayB[ball];

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to add more balls via user input

    The compiler won't know what to do with balls=... because you haven't declared that variable.

    balls is supposed to be an array of Ball instances. The tutorial page I linked to only mentioned arrays of primitives, but creating a Ball array is similar:

    Java Code:
    Ball[] balls = new Ball[42];
    I used 42 here - it could be anything, but it has to be an int or an int variable like numBall. It is the thing that determines the size of the array.

    One question you will have to think about is where you want to create the balls array. Bear in mind that arrays don't change length. That may be a problem for you, or it might not, but it affects where and how often you create the Ball array.

    [Edit] I just guessed that the compiler was complaining about balls being unknown. It's a good idea to copy and post complete compiler messages and enough of your code so that readers here know what's going.

  11. #11
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: how to add more balls via user input

    ok so i have this...
    Java Code:
    Ball ball = new Ball(x,y,50,h,v);
      Ball[] balls = new Ball[numSpeed];
    Java Code:
    public void drawBalls(Graphics g){
        for(Ball b:balls){
        g.fillOval(ball.getX(),ball.getY(),ball.getR(),ball.getR());
        }
      }
    But now no ball appears on the screen, and im not sure how to get numBalls value into it. Im sorry for all the questions

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to add more balls via user input

    Do you see the difference between your for loop and the one I used in #4?

    Tutorial: for loops.

  13. #13
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: how to add more balls via user input

    Haha yes.. b.getx() etc.. however, still no ball on the screen

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to add more balls via user input

    This isn't tennis - forget about the ball for moment and look at the console. You should be seeing an exception and trying to figure out why it is there.

  15. #15
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: how to add more balls via user input

    I really don't know what im doing :\

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to add more balls via user input

    At this point we have identified using arrays ("declaring" and "populating" them) and accessing them in a for loop. I would highly recommend the links to the Tutorial already given, and surrounding sections that deal with basic syntax.

  17. #17
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: how to add more balls via user input

    I see, I understand the basics.
    Java Code:
     anArray = new int[10];      // allocates memory for 10 integers
                
              anArray[0] = 100; // initialize first element
              anArray[1] = 200; // initialize second element
    this much is clear to me.. having an index holding the element.. in my case. numballs is the index. so if the user enters 5. balls has an 5 ball objects, correct?

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to add more balls via user input

    Only if - after the user enters 5 - you say something like

    Java Code:
    balls = new Ball[5];
    (balls having been declared properly somewhere before.)

    Those other two lines are important as well. It's not enough to create the new Ball[] array. You also have to create 5 balls and put them into the array:

    Java Code:
    balls[0] = new Ball(?,?,?,?,?);
    balls[1] = new Ball(?,?,?,?,?);
    // etc
    Probably you would create the balls and put them into the array in a for loop.

  19. #19
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: how to add more balls via user input

    but i would then have a set amount and cant exceed the 5 to appear on screen. wouldn't i replace the 0,1,etc with a variable, im thinking numBalls?

  20. #20
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to add more balls via user input

    wouldn't i replace the 0,1,etc with a variable, im thinking numBalls?
    As I said you would probably create the balls and add them to the array in a for loop. The index variable used by the for loop can be anything you like - but numBalls is already in use for something else: the number of balls.

    [Edit] Here's an idea: create an int array with 1000 and fill it with the even numbers 2,4,6,... Print the array contents to check that they are correct. Do the same exercise for an array whose length is obtained from the user.

    The point is that you have to be happy with the techniques for creating and working with arrays before you can apply them to a Ball[] array. Textbooks and tutorials are the place to find out about those techniques.
    Last edited by pbrockway2; 12-12-2011 at 01:56 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 02:52 PM
  2. Help with user input
    By sconniegorilla in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 02:00 PM
  3. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 03:09 AM
  4. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 AM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 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
  •