Results 1 to 3 of 3
- 11-30-2007, 04:21 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 46
- Rep Power
- 0
Need help making ball move and bounce off of sides of JPanel
I'm currently working on a program that creates a ball of random color. I'm trying to make the ball move around the JPanel it is contained in and if it hits a wall it should bounce off of it. I have been able to draw the ball and make it fill with a random color. However, I cannot get it to move around and bounce off the walls. Below is the code for the class that creates the ball. Should I possible be using multithreads? Any help would be appreciated.
Thanks
Java Code:import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; public class Ball extends JPanel{ Graphics g; int rval; // red color value int gval; // green color value int bval; // blue color value private int x = 1; private int y = 1; private int dx = 2; private int dy = 2; public void paintComponent(Graphics g){ for(int counter = 0; counter < 100; counter++){ // randomly chooses red, green and blue values changing color of ball each time rval = (int)Math.floor(Math.random() * 256); gval = (int)Math.floor(Math.random() * 256); bval = (int)Math.floor(Math.random() * 256); super.paintComponent(g); g.drawOval(0,0,30,30); // draws circle g.setColor(new Color(rval,gval,bval)); // takes random numbers from above and creates RGB color value to be displayed g.fillOval(x,y,30,30); // adds color to circle move(g); } } // end paintComponent public void move(Graphics g){ g.fillOval(x, y, 30, 30); x += dx; y += dy; if(x < 0){ x = 0; dx = -dx; } if (x + 30 >= 400) { x = 400 - 30; dx = -dx; } if (y < 0) { y = 0; dy = -dy; } if (y + 30 >= 400) { y = 400 - 30; dy = -dy; } g.fillOval(x, y, 30, 30); g.dispose(); } } // end Ball class
- 12-01-2007, 12:31 AM #2Java Code:
import java.awt.*; import javax.swing.*; public class BallMoves extends JPanel implements Runnable { Color color = Color.red; int dia = 30; long delay = 40; private int x = 1; private int y = 1; private int dx = 3; private int dy = 2; protected void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(color); g.fillOval(x,y,30,30); // adds color to circle g.setColor(Color.black); g2.drawOval(x,y,30,30); // draws circle } public void run() { while(isVisible()) { try { Thread.sleep(delay); } catch(InterruptedException e) { System.out.println("interrupted"); } move(); repaint(); } } public void move() { if(x + dx < 0 || x + dia + dx > getWidth()) { dx *= -1; color = getColor(); } if(y + dy < 0 || y + dia + dy > getHeight()) { dy *= -1; color = getColor(); } x += dx; y += dy; } private Color getColor() { int rval = (int)Math.floor(Math.random() * 256); int gval = (int)Math.floor(Math.random() * 256); int bval = (int)Math.floor(Math.random() * 256); return new Color(rval, gval, bval); } private void start() { while(!isVisible()) { try { Thread.sleep(25); } catch(InterruptedException e) { System.exit(1); } } Thread thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } public static void main(String[] args) { BallMoves test = new BallMoves(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); test.start(); } }
- 12-01-2007, 08:48 AM #3
Member
- Join Date
- Jul 2007
- Posts
- 46
- Rep Power
- 0
Similar Threads
-
Help with Move Shape
By romina in forum AWT / SwingReplies: 2Last Post: 12-09-2010, 04:25 AM -
bouncing ball issue
By adam405 in forum New To JavaReplies: 1Last Post: 03-18-2008, 04:48 AM -
How do I make My ball to move randomly?
By whdbstjr90 in forum New To JavaReplies: 4Last Post: 12-31-2007, 06:32 PM -
Problem deleting ball from bouncing ball app
By adlb1300 in forum New To JavaReplies: 2Last Post: 12-03-2007, 10:08 PM -
Bouncing Ball Just Suddenly Stops Mid Bounce
By adlb1300 in forum Java 2DReplies: 1Last Post: 12-03-2007, 03:58 PM
Bookmarks