Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Bouncing Ball Program Random Color Change

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Bouncing Ball Program Random Color Change

    Hi I am working on the Bouncing Ball project that when a user clicks on the mouse the mousePress invokes and create a ball and when mousePress click successively it creates another ball and randomly changes color. I have the entire program working but I am having trouble in the Random and Color changing of the program.
    Can you please assist? Thanks.

    /*Main Class*/
    class BallDriver
    {
    	public static void main(String[] args) 
    	{
    		Bounce app = new Bounce();
     
    	}
    }

    /*Ball class*/
    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

    /*Bounce Class*/
    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();
    		//ball.setColor(Color.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

    Error Code:

    ----jGRASP exec: javac -g BallDriver.java
    ----jGRASP: operation complete.
    ----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 greatly appreciate for your assistance. I forgot how to use these methods and randomly change color.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Bouncing Ball Program Random Color Change

    Did you not like the answers you already got?

    This thread has been cross posted here:

    http://www.java-forums.org/advanced-java/39459-ball-program.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bouncing Ball Program Random Color Change

    Hello Kevin,
    Is not that i didn't like the previous answers given is that at times programmers tend to overcomplicate things.
    I also like to see the different ways of coding.
    I had no intention to crosspost. I didn't know that this forum is the same as the other java forum. It totally has different names and no link to the latter site.
    coderEvolution.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Bouncing Ball Program Random Color Change

    The forums are not the same. I just happened to be looking at both.

    The problem with crossposting is that we don't know what help you've already received, so any help we give you might be wasting our time repeating what you've already been told. We have hundreds of posts to get to, so it can be frustrating to help somebody who has already received an answer, when there are plenty of other people who did not crosspost and have not already received help elsewhere.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bouncing Ball Program Random Color Change

    Quote Originally Posted by KevinWorkman View Post
    The forums are not the same. I just happened to be looking at both.

    The problem with crossposting is that we don't know what help you've already received, so any help we give you might be wasting our time repeating what you've already been told. We have hundreds of posts to get to, so it can be frustrating to help somebody who has already received an answer, when there are plenty of other people who did not crosspost and have not already received help elsewhere.
    Hi Kevin,
    I understand.
    So can I close one of the posting maybe like to stay in this forum for future projects.
    I am still in need of assistance with the ball program.
    I only need to code for the random change of color for every mousePress.

    How can I go about deleting the crossposts?

    Thanks.

    coderEvolution

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Bouncing Ball Program Random Color Change

    Quote Originally Posted by coderEvolution View Post
    How can I go about deleting the crossposts?
    You can't really. I saw you got some suggestions at the other forum, what happened when you tried them?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bouncing Ball Program Random Color Change

    Hi, it actually didn't work, didn't change the color of the balls.
    The program works every mousePress click produces the balls but does not change the color.
    All I needed is how to change the color of the ball after every mousePress.

    coderEvolution

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Bouncing Ball Program Random Color Change

    Staying at these forums for future projects is fine with us!

    So what happens when you click the mouse? Does anything happen? Are any exceptions thrown?
    I will take a more indepth look in the morning and hopefully offer a solution.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  9. #9
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bouncing Ball Program Random Color Change

    When i click on the canvas it create another ball.
    There is no exception but when i had coded to try to instantiate the ball class to the main i did get an exception:
    ----jGRASP exec: javac -g BallDriver.java

    BallDriver.java:7: '.class' expected
    ball.getColor(int ballColor, Graphics2D passedG);
    ^
    BallDriver.java:7: ';' expected
    ball.getColor(int ballColor, Graphics2D passedG);
    ^
    BallDriver.java:7: ';' expected
    ball.getColor(int ballColor, Graphics2D passedG);
    ^
    3 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
    //Ball class
     
    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 <= 3000; 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
     
    			} //end run
    		}
    		catch (Exception e)
    		{
    			System.err.println(e.toString());
    		}
    	}
    	private void getColor(int ballColor, Graphics2D passedG)
    	{
    		//Switch statement to determine random color
    		switch(ballColor)
    		{
    			case 0: passedG.setColor(Color.BLUE);
    			break;
    			case 1: passedG.setColor(Color.YELLOW);
    			break;
    			case 2: passedG.setColor(Color.RED);
    			break;
    			case 3: passedG.setColor(Color.MAGENTA);
    			break;
    			case 4: passedG.setColor(Color.PINK);
    			break;
    			case 5: passedG.setColor(Color.GRAY);
    			break;
    			case 6: passedG.setColor(Color.CYAN);
    			break;
    			case 7: passedG.setColor(Color.ORANGE);
    			break;
    			case 8: passedG.setColor(Color.WHITE);
    			break;
    			default: passedG.setColor(Color.BLACK);
    			break;
    		}
    	}
    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;
    	private Random random;
     
     
    	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();
    		// Color randomColor = new Color((int)(256*Math.random()), 
    // 											(int)(256*Math.random()), 
    // 											(int)(256*Math.random()));
    		// Random gen = new Random(); 
    // 		for(int i = 1; i< 20; i++)
    // 		{ 
    // 			int red = gen.nextInt(256); 
    // 			int green = gen.nextInt(256); 
    // 			int blue = gen.nextInt(256);
    // 			Color ball = new Color(red, green, blue);
    // 		return ball;
    		//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
    //Main class
    class BallDriver
    {
    	public static void main(String[] args) 
    	{
    		Bounce app = new Bounce();
    		Ball ball = new Ball();
    		ball.getColor(int ballColor, Graphics2D passedG);
    	}
    }

    I also commented some of the code that i was working on that didn't give the result i needed. I am trying every each way i can.

    Thank you i appreciate for your assistance.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Bouncing Ball Program Random Color Change

    That's not how you call methods. Recommended reading: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bouncing Ball Program Random Color Change

    Quote Originally Posted by KevinWorkman View Post
    That's not how you call methods. Recommended reading: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    Thank you for your help.
    Well appreciated.

Similar Threads

  1. How to Change JTextArea font, font size and color
    By Flash in forum Java Swing Tutorials
    Replies: 7
    Last Post: January 14th, 2012, 10:47 PM
  2. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM
  3. Change font color and size
    By javanovice in forum AWT / Java Swing
    Replies: 2
    Last Post: April 20th, 2010, 09:57 AM
  4. Change of color for selected text in AWT
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: April 9th, 2010, 03:51 AM
  5. Checkbox - Change Font Color
    By wakebrdr77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 10:57 AM