Results 1 to 3 of 3
- 02-12-2012, 05:08 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 1
- Rep Power
- 0
i can not see balls bouncing and background is not black please help!|
-----------------------------------------Java Code:/* this class uses field instead of variables */ package malik_lab5; import java.awt.*; import java.awt.Color; /** * * @author Aamina Malik */ public class Ball { private int bxCoord; private int byCoord; private int bHeight; private int bWidth; private int bRise; private int bRun; private final Color color; public Ball() { bxCoord = (int) (Math.random() * 50); byCoord = (int) (Math.random() * 50); bHeight = (int) (Math.random() * 10) + 11; bWidth = (int) (Math.random() * 10) + 11; bRise = (int) (Math.random() * 10); bRun = (int) (Math.random() * 10); int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); color = new Color(red, green, blue); } public void paintComponent(Graphics g) { g.setColor(color); g.fillOval(bxCoord, byCoord, bWidth, bHeight); } public void move(int WINDOW_WIDTH, int WINDOW_HEIGHT) { if (this.bxCoord < (0 - this.bRun) || this.bxCoord > (WINDOW_WIDTH - bWidth)) { this.bRun = -this.bRun; } if (this.byCoord < (0 - this.bRise) || this.byCoord > (WINDOW_HEIGHT - bHeight)) { this.bRise = -this.bRise; } this.bxCoord += this.bRun; this.byCoord += this.bRise; } }
---------------------------------------------------------------Java Code:package malik_lab5; import java.awt.*; import javax.swing.*; import java.util.*; /** * A panel containing two bouncing balls. This panel may be placed in a JFrame */ public class BallPanel extends JPanel { Ball ball; public BallPanel(ArrayList<Ball> ballList, int numberOfBalls) { for (int i = 0; i < numberOfBalls; i++) { ball = new Ball(); 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(this.getHeight(),this.getWidth()); } } }
---------------------------------Java Code:package malik_lab5; import java.awt.*; // Old library classes, you still need them import java.util.ArrayList; import java.util.Scanner; import javax.swing.*; // New library classes, Swing extends AWT /** * 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); } } public BallTest(ArrayList<Ball> ballList, int numberOfBalls) { super("Bouncing Ball"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setLayout(new BorderLayout()); 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); } }
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package malik_lab5; import java.util.ArrayList; import java.util.Scanner; import javax.swing.JOptionPane; /** * * @author Aamina Malik */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); ArrayList ballList = new ArrayList(); 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 OF BALLS " + numberOfBalls); BallTest t = new BallTest(ballList, numberOfBalls); } }Last edited by Fubarable; 02-12-2012 at 09:42 PM. Reason: code tags added
-
Re: i can not see balls bouncing and background is not black please help!|
I've added code tags to your code posted above, but I can't help you as you haven't explained your problem at all, save the brief heading to this thread.
If you seriously need our help, then you'll probably need to do more than simply dump your code in here without much explanation. Consider posting a paragraph or two telling the details or your problem. A good rule of thumb is to put in as much effort asking your question as you'd like one of the volunteers here to put into answering it.
- 02-13-2012, 06:45 AM #3
Re: i can not see balls bouncing and background is not black please help!|
Your overload of paintComponent(Graphics g, ArrayList<Ball> ballList, int numberOfBalls) won't ever be called. Go through this Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) to learn how to override the correct method.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
More bouncing balls...
By ShaolinMunky in forum New To JavaReplies: 5Last Post: 06-10-2012, 09:43 AM -
ArrayList and Bouncing Balls
By jamie23 in forum AWT / SwingReplies: 1Last Post: 02-20-2011, 06:54 PM -
Multiple bouncing balls
By Algar in forum AWT / SwingReplies: 2Last Post: 04-24-2008, 08:35 PM -
Movement of balls
By BlitzA in forum New To JavaReplies: 8Last Post: 01-09-2008, 03:30 PM -
Why this image background is black ?
By samson in forum Java 2DReplies: 1Last Post: 07-17-2007, 04:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks