Results 1 to 11 of 11

Thread: Ball program

  1. #1
    codeStone is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Smile Ball program

    This is a ball.java program assignment.
    The object of the game is when a user mousePress to the JPanel, it creates a ball and successive clicks create and change color of ball.
    I have the entire program working except the change of colors.
    Can you please guide me to where a went wrong?
    Thanks.
    codeStone

    Java Code:
    //bounce.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.util.Random;
    
    public class Bounce extends JFrame implements MouseListener
    {
    	private JPanel canvas;
    	private JPanel buttonPanel;
    	private Ball ball;
    	private int x = 0, y = 0, xCoord, yCoord;
    		 
    		
    	public Color randomColor()
    	{
    		Random randomColor = new Random();
    		int red = (int)(Math.random()*256);
    		int green = (int)(Math.random()*256);
    		int blue = (int)(Math.random()*256);
    		
    		return (new Color(red, green, blue));
    	}
    	
    	public Bounce()
    	{
    		setTitle("Bounce");
    		Container contentPane = getContentPane();
    		canvas = new JPanel();
    		
    		contentPane.add(canvas);
    		buttonPanel = new JPanel();	
    		LineBorder line = new LineBorder(Color.black);
    		buttonPanel.setBorder(line);
    		contentPane.add(buttonPanel, "South");
    		setSize(300, 300);
    		setVisible(true);
    		
    		canvas.addMouseListener(this);
    }
    
    
    	public void mousePressed(MouseEvent e) 
    	{	
    
    		ball = new Ball(canvas); // create a new ball
    		Thread t = new Thread(ball);
    		randomColor();
    		//gets coordinates from where mouse is clicked
    		x = e.getX();
    		y = e.getY(); 
    		//gives coordinates to ball object
    		ball.setXCoord(x); 
    		ball.setYCoord(y);
    			
    		ball.start();		
    	}
    	
    
    	public void mouseClicked(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    	
    	
    } // end class Bounce
    Java Code:
    //Ball.java
    import java.awt.*;
    import javax.swing.*;
    public class Ball extends Thread implements Runnable
    {
    	private JPanel box;
    	private static final int XSIZE = 30;	//constant on size of ball move x-axis
    	private static final int YSIZE = 30;	//constant on size of ball move y-axis
    	//declare variables
    	private int x = 0;							
       private int y = 0;	
    	//declare variables for dimension of x and y						
       private int dx = 2;							
       private int dy = 2;							
    	
    	public Ball(JPanel b) 
    	{
    		box = b; 
    		
    	}//end ball constructor
    	
    	//sets the ball coordinates of x and y axis
    	public void setXCoord(int XIn)
    	{x=XIn;}// end setXCoord
    	public void setYCoord(int YIn)
    	{y=YIn;}// end setYCoord
    
    
    public void draw() 
    	{
    		
    		//algorithm to draw the ball and size
    		Graphics g = box.getGraphics();
    		g.fillOval(x, y, XSIZE, YSIZE);
    		g.dispose();
    	}//end draw
    	
    public void move() 
    	{
    		//algorithm for the move of the ball
    		Graphics g = box.getGraphics();
    		g.clearRect(x, y, XSIZE, YSIZE);
    		x += dx;
    		y += dy;
    		Dimension d = box.getSize();
    
    		if (x < 0)
    		{ 
    			x = 0; dx = -dx;
    		}
    
    		if (x + XSIZE >= d.width)
    		{ 
    			x = d.width - XSIZE; dx = -dx; 
    		}
    if (y < 0)
    		{
    			y = 0; dy = -dy;
    		}
    
    		if (y + YSIZE >= d.height)
    		{ 
    			y = d.height - YSIZE; dy = -dy; 
    		}
    		
    		//sets the color of the ball and its radius use of fillOval
    		g.setColor(Color.red);
    		g.fillOval(x, y, XSIZE, YSIZE);
    		g.dispose();
    	}//end move
    	
    public void bounce()
    	{
    		draw();//call for the draw method
    		
    		//loop and call the move method
    		for (int i = 1; i <= 2000; i++) 
    		{
    			move();
    
    			try
    			{ 
    				//this is the thread to sleep in 5 seconds
    				Thread.sleep(5); 
    			}
    			catch(InterruptedException e) 
    			{
    				System.err.println("InterruptedException" + e.getMessage());
    			}//if some how interrupted it throw an exception caught by InterruptException
    		}
    	}//end bounce
    	
    public void run()
    	{
    		try
    		{
    			for (int run=0; run < 5; run++) 
    			{
    			
    			bounce ();
    			Thread.sleep((long)(Math.random() * 500));//this is the thread random generator multiply by 500 for 5 seconds
    			
    			} //end run
    		}
    		catch (Exception e)
    		{
    			System.err.println(e.toString());
    		}
    	}
    
    } // end class Ball
    Java Code:
    //BallDriver.java
    class BallDriver
    {
    	public static void main(String[] args) 
    	{
    		Bounce app = new Bounce();
    
    	}
    }

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

    Default

    You call the randomColor() method which returns a Color, but I don't see you placing the returned Color in a variable or doing anything with it. Also your Ball class has the Color hard-coded to Color.red, so I wouldn't expect the color to change. Shouldn't you give this class a method or constructor that allows you to change the Color?

  3. #3
    codeStone is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Smile

    Quote Originally Posted by Fubarable View Post
    You call the randomColor() method which returns a Color, but I don't see you placing the returned Color in a variable or doing anything with it. Also your Ball class has the Color hard-coded to Color.red, so I wouldn't expect the color to change. Shouldn't you give this class a method or constructor that allows you to change the Color?
    Hi so that is the problem why it wont change colors?
    I have forgotten to get this color to change.
    the programs fine but won't change the color.
    I do have the constructor with the return value.
    I commented out the hard coded color now its black as a default color thats not the problem.
    what is it im doing wrong?
    I tried every way getting this to work to no avail.
    codeStone
    Last edited by codeStone; 02-23-2011 at 07:10 PM.

  4. #4
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by codeStone View Post
    Java Code:
            public void mousePressed(MouseEvent e) 
    	{	
    
    		ball = new Ball(canvas); // create a new ball
    		Thread t = new Thread(ball);
    		randomColor();
    		//gets coordinates from where mouse is clicked
    		x = e.getX();
    		y = e.getY(); 
    		//gives coordinates to ball object
    		ball.setXCoord(x); 
    		ball.setYCoord(y);
    			
    		ball.start();		
    	}
    Your problem is in this method. You're ignoring randomColor()'s returned color. Nor does your Ball class even have a setColor() method, so you have to add that.

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

    Default

    Quote Originally Posted by codeStone View Post
    Hi so that is the problem why it wont change colors?
    I have forgotten to get this color to change.
    the programs fine but won't change the color.
    I do have the constructor with the return value.
    I commented out the hard coded color now its black as a default color thats not the problem.
    what is it im doing wrong?
    I tried every way getting this to work to no avail.
    codeStone
    If you've made changes, please show your changes by posting your new code. Otherwise how can we tell you what you're doing wrong. Besides now you've gotten the same explanation twice, once by me and once more recently by NRitH.

  6. #6
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    If you've made changes, please show your changes by posting your new code. Otherwise how can we tell you what you're doing wrong. Besides now you've gotten the same explanation twice, once by me and once more recently by NRitH.
    Whoops, I didn't notice that you said the exact same thing. I was too busy looking at your explanation of how he hard-coded Color.red.

  7. #7
    codeStone is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Smile

    Hello Guys, Sorry for the delay in responding had to pick up wifey from work.
    You're correct I should had included the changes to my code.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.util.Random;
    
    
    
    public class Bounce extends JFrame implements MouseListener
    {
    	private JPanel canvas;
    	private JPanel buttonPanel;
    	private Ball ball;
    	private int x = 0, y = 0, xCoord, yCoord;
    		 
    		
    	public Color randomColor()
    	{
    		Random random = new Random();
    		int red = (int)(Math.random()*256);
    		int green = (int)(Math.random()*256);
    		int blue = (int)(Math.random()*256);
    		
    		return (new Color(red, green, blue));
    	}
    	
    	public Bounce()
    	{
    		setTitle("Bounce");
    		Container contentPane = getContentPane();
    		canvas = new JPanel();
    		
    		contentPane.add(canvas);
    		buttonPanel = new JPanel();	
    		LineBorder line = new LineBorder(Color.black);
    		buttonPanel.setBorder(line);
    		contentPane.add(buttonPanel, "South");
    		setSize(300, 300);
    		setVisible(true);
    		
    		canvas.addMouseListener(this);
    }
    
    
    	public void mousePressed(MouseEvent e) 
    	{	
    
    		ball = new Ball(canvas); // create a new ball
    		Thread t = new Thread(ball);
    		randomColor();
    		[U]ball.setColor(Color.randomColor());[/U]//error below		//gets coordinates from where mouse is clicked
    		x = e.getX();
    		y = e.getY(); 
    		//gives coordinates to ball object
    		ball.setXCoord(x); 
    		ball.setYCoord(y);
    			
    		ball.start();		
    	}
    	
    
    	public void mouseClicked(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    	
    	
    } // end class Bounce
    
    Error in code setColor:
    
     ----jGRASP exec: javac -g Bounce.java
    
    Bounce.java:51: cannot find symbol
    symbol  : method randomColor()
    location: class java.awt.Color
    		ball.setColor(Color.randomColor());
    		                   ^
    1 error
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    I believe you're saying to include the setColor in the run() method in the Ball class?

    codeStone

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    codeStone is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Hi Kevin,
    Sorry for the late reply.
    I didn't realize that both sites were the same. So I never intended to do a crosspost. However, I utilize all the help i can get for my programs does not mean that the answer given is inadequate. It is at times programmers tend to overcomplicated code. I am a simple person and like to keep things simple.

    The answer given prior to your post I also posted another question because i can't figure out how to code the random generator to generate different colors to the balls when the user successively click on the canvas.

    I already have the constructor and the method and the code in the mousePress event it still does not render the color.
    Above is the code which rendered an error.
    codeStone.

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

    Default

    Quote Originally Posted by codeStone View Post
    Hi Kevin,
    Sorry for the late reply.
    I didn't realize that both sites were the same.
    They are not the same, but you obviously have not helped others on a site like this, for if you had you'd understand our concern. Imagine that you've engrossed yourself in someone else's program and problem, that you've spent a fair bit of time analyzing things, coming up with a solution and have posted it, only to find that someone else had posted the same thing hours earlier in a cross-post that you hadn't been told about. You'd likely be justifiably upset at the original poster's allowing you to have wasted your valuable time answering an already-answered question just because he didn't have the courtesy to inform all about cross-posts and to provide links to all cross-posts.

    It is for this reason that when we find posters who cross-post repeatedly without notifying others, we usually shun them and their questions.

  11. #11
    codeStone is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Hello Fubarable,
    Yes I agree with you. I can understand how you all feel. I would feel the same way and would shun the poster as well.

    I didn't realize what i was doing, just needed quick responses. Being desperate made me to think to crosspost without realize i was crossposting. I apologize to everyone in this forum. It will not happen again. If only you guys give a chance, I truly need assistance in the working problem.

    Sincerely,

    codeStone

Similar Threads

  1. Bouncing Ball program
    By lost1000 in forum New To Java
    Replies: 3
    Last Post: 11-18-2010, 03:26 PM
  2. Need help with a third ball in game.
    By vlan in forum Java Applets
    Replies: 2
    Last Post: 05-30-2010, 03:37 PM
  3. Box Ball Java
    By fcgb9115 in forum New To Java
    Replies: 1
    Last Post: 04-19-2010, 02:33 AM
  4. Bouncing ball program... having problems..
    By ilovenayoon in forum Java Applets
    Replies: 1
    Last Post: 12-08-2009, 04:16 PM
  5. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 09:08 PM

Posting Permissions

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