Results 1 to 2 of 2
Thread: ArrayList and Bouncing Balls
- 02-20-2011, 06:41 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 1
- Rep Power
- 0
ArrayList and Bouncing Balls
My program is suppose to ask the user how many balls bouncing they would like to have then store the random values of the ball including random color in an ArrayList. I believe that I am having an issue with the paintComponent, the program with pop up the GUI window just fine but the background isn't black and there are no balls bouncing. I was hoping that someone could suggest how to fix this problem. I attached the code.
Thank you
-
Your code for all to see, comments removed:
Java Code:import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.GraphicsEnvironment; import java.awt.Point; import javax.swing.*; import java.util.ArrayList; import java.util.Scanner; /** * Frame to hold a bouncing ball panel, implemented in the BallPanel class. * Controls the animation of the ball via pauses and calls to BallPanel's move * and paintComponent methods. * * @author Michael Peterson */ public class BallTest extends JFrame { private static final int WINDOW_WIDTH = 500; private static final int WINDOW_HEIGHT = 300; private BallPanel ballPanel; public static void pause() { try { Thread.sleep(20); } catch (Exception e) { System.out.println(e); e.printStackTrace(); } } public BallTest(ArrayList<Ball> ballList, int numberOfBalls) { super("Bouncing Ball"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setLayout(new BorderLayout()); ballPanel = new BallPanel(ballList, numberOfBalls); add(ballPanel); center(this); setVisible(true); while (true) { pause(); ballPanel.move(WINDOW_WIDTH, WINDOW_HEIGHT, ballList, numberOfBalls); ballPanel.repaint(); } } public static void center(JFrame frame) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Point center = ge.getCenterPoint(); int w = frame.getWidth(); int h = frame.getHeight(); int x = center.x - w / 2, y = center.y - h / 2; frame.setBounds(x, y, w, h); frame.validate(); } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); ArrayList<Ball> ballList = new ArrayList<Ball>(); String response = JOptionPane.showInputDialog(null, "How many balls do you want boucning", "Bouncing Balls", JOptionPane.QUESTION_MESSAGE); int numberOfBalls = Integer.parseInt(response); System.out.println("number" + numberOfBalls); BallTest t = new BallTest(ballList, numberOfBalls); } } class Ball { int bxCoord; int byCoord; int bHeight; int bWidth; int bRise; int bRun; Color bColor; BallPanel ballPanel; public Ball() { bxCoord = 0; byCoord = 0; bHeight = 0; bWidth = 0; bRise = 0; bRun = 0; bColor = Color.red; } public void initialize() { this.bxCoord = getBxCoord(); this.byCoord = getByCoord(); this.bHeight = getBHeight(); this.bWidth = getBWidth(); this.bRise = getBRise(); this.bRun = getBRun(); this.bColor = getBColor(); } public int getBxCoord() { setBxCoord(); System.out.println(bxCoord); return bxCoord; } public int getByCoord() { setByCoord(); System.out.println(byCoord); return byCoord; } public int getBHeight() { setBHeight(); System.out.println(bHeight); return bHeight; } public int getBWidth() { setBWidth(); System.out.println(bWidth); return bWidth; } public int getBRise() { setBRise(); System.out.println(bRise); return bRise; } public int getBRun() { setBRun(); System.out.println(bRun); return bRun; } public Color getBColor() { setBColor(); System.out.println(bColor); return bColor; } public void setBxCoord() { bxCoord = (int) (Math.random() * 50); } public void setByCoord() { byCoord = (int) (Math.random() * 50); } public void setBHeight() { bHeight = (int) (Math.random() * 10) + 11; } public void setBWidth() { bWidth = (int) (Math.random() * 10) + 11; } public void setBRise() { bRise = (int) (Math.random() * 10); } public void setBRun() { bRun = (int) (Math.random() * 10); } public void setBColor() { int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); Color randomColor = new Color(red, green, blue); bColor = randomColor; } public void paintComponent(Graphics g) { g.setColor(this.bColor); g.fillOval(this.bxCoord, this.byCoord, this.bWidth, this.bHeight); } public void move(int WINDOW_WIDTH, int WINDOW_HEIGHT) { if (this.bxCoord < (0 - this.bRun) || this.bxCoord > (WINDOW_WIDTH - this.bWidth)) { this.bRun = -this.bRun; } if (this.byCoord < (0 - this.bRise) || this.byCoord > (WINDOW_HEIGHT - this.bHeight)) { this.bRise = -this.bRise; } this.bxCoord += this.bRun; this.byCoord += this.bRise; } } class BallPanel extends JPanel { Ball ball; public BallPanel(ArrayList<Ball> ballList, int numberOfBalls) { for (int i = 0; i < numberOfBalls; i++) { ball = new Ball(); ball.initialize(); ballList.add(ball); } } public void paintComponent(Graphics g, ArrayList<Ball> ballList, int numberOfBalls) { super.paintComponent(g); g.setColor(Color.black); g.fillRect(0, 0, getWidth(), getHeight()); for (int i = 0; i < numberOfBalls; i++) { ballList.get(i).paintComponent(g); } } public void move(int WINDOW_WIDTH, int WINDOW_HEIGHT, ArrayList<Ball> ballList, int numberOfBalls) { for (int i = 0; i < numberOfBalls; i++) { ballList.get(i).move(WINDOW_WIDTH, WINDOW_HEIGHT); } } }
You'll want to read up on Concurrency in Swing and why doing Thread.sleep(...) and while (true) is not a good idea on the main Swing thread, the EDT.
Next, you'll want to read the tutorial on and use a Swing Timer
Similar Threads
-
Bouncing Ball program
By lost1000 in forum New To JavaReplies: 3Last Post: 11-18-2010, 03:26 PM -
high bouncing ball
By bouncingball in forum Reviews / AdvertisingReplies: 1Last Post: 06-19-2008, 11:21 AM -
Multiple bouncing balls
By Algar in forum AWT / SwingReplies: 2Last Post: 04-24-2008, 08:35 PM -
bouncing ball issue
By adam405 in forum New To JavaReplies: 1Last Post: 03-18-2008, 03:48 AM -
Movement of balls
By BlitzA in forum New To JavaReplies: 8Last Post: 01-09-2008, 03:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks