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.