Results 1 to 3 of 3
  1. #1
    aaminamirza is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default 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 10:42 PM. Reason: code tags added

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

    Default 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.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. More bouncing balls...
    By ShaolinMunky in forum New To Java
    Replies: 5
    Last Post: 06-10-2012, 10:43 AM
  2. ArrayList and Bouncing Balls
    By jamie23 in forum AWT / Swing
    Replies: 1
    Last Post: 02-20-2011, 07:54 PM
  3. Multiple bouncing balls
    By Algar in forum AWT / Swing
    Replies: 2
    Last Post: 04-24-2008, 09:35 PM
  4. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 04:30 PM
  5. Why this image background is black ?
    By samson in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 05:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •