Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-30-2007, 04:21 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
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

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-01-2007, 12:31 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
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(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-01-2007, 08:48 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
Thanks Hardwired for the help. Animating objects is very new to me. This is the first time I had to do in programming. I appreciate the time you have taken to help out a beginner like myself.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
bouncing ball issue adam405 New To Java 1 03-18-2008 04:48 AM
How do I make My ball to move randomly? whdbstjr90 New To Java 4 12-31-2007 06:32 PM
Problem deleting ball from bouncing ball app adlb1300 New To Java 2 12-03-2007 10:08 PM
Bouncing Ball Just Suddenly Stops Mid Bounce adlb1300 Java 2D 1 12-03-2007 03:58 PM
Help with Move Shape romina AWT / Swing 1 08-07-2007 06:27 AM


All times are GMT +3. The time now is 05:46 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org