Results 1 to 20 of 23
- 12-10-2011, 11:37 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
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(); } }
- 12-10-2011, 11:42 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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.
- 12-10-2011, 11:50 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
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.
- 12-11-2011, 12:33 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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()); } }
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-11-2011 at 12:37 AM.
- 12-11-2011, 01:06 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
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()); }
-
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?
- 12-11-2011, 01:39 AM #7
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
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.
- 12-11-2011, 01:49 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
Re: how to add more balls via user input
- 12-11-2011, 04:57 AM #9
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
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];
- 12-11-2011, 05:07 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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];
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.
- 12-11-2011, 05:31 AM #11
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
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()); } }
- 12-11-2011, 06:45 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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.
- 12-11-2011, 08:25 AM #13
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
Re: how to add more balls via user input
Haha yes.. b.getx() etc.. however, still no ball on the screen
- 12-11-2011, 08:55 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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.
- 12-11-2011, 10:25 AM #15
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
Re: how to add more balls via user input
I really don't know what im doing :\
- 12-12-2011, 12:01 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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.
- 12-12-2011, 01:34 AM #17
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
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
- 12-12-2011, 01:53 AM #18
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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];
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
- 12-12-2011, 02:28 AM #19
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
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?
- 12-12-2011, 02:50 AM #20
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
Re: how to add more balls via user input
wouldn't i replace the 0,1,etc with a variable, im thinking numBalls?
[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 02:56 AM.
Similar Threads
-
User Input???
By jonytek in forum New To JavaReplies: 8Last Post: 01-13-2013, 03:52 PM -
Help with user input
By sconniegorilla in forum New To JavaReplies: 2Last Post: 02-16-2011, 03:00 PM -
Need help getting input(first/last name) from user
By nightrise420 in forum New To JavaReplies: 11Last Post: 09-11-2010, 04:09 AM -
User Input
By brmcdani in forum New To JavaReplies: 2Last Post: 02-05-2010, 02:59 AM -
cant take input from user
By new_1 in forum New To JavaReplies: 6Last Post: 12-25-2007, 08:38 AM
Bookmarks