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.
Code Java:
/*Main Class*/
class BallDriver
{
public static void main(String[] args)
{
Bounce app = new Bounce();
}
}
Code Java:
/*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
Code Java:
/*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.
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
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.
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.
Re: Bouncing Ball Program Random Color Change
Quote:
Originally Posted by
KevinWorkman
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
Re: Bouncing Ball Program Random Color Change
Quote:
Originally Posted by
coderEvolution
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?
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
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.
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.
Code :
//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;
}
}
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;
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
Code :
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.
Re: Bouncing Ball Program Random Color Change
Re: Bouncing Ball Program Random Color Change
Quote:
Originally Posted by
KevinWorkman
Thank you for your help.
Well appreciated.