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 03-17-2008, 11:59 PM
Member
 
Join Date: Mar 2008
Posts: 7
adam405 is on a distinguished road
bouncing ball issue
I am trying to create a bouncing ball. At the momene the ball just travels off the applet screen instead of bouncing back. Any help is appreciated.

Code:
// import necessary packages import java.applet.*; import java.awt.*; import java.util.Random; public class FirstApplet extends Applet implements Runnable { //position of the ball int x_pos = 1; int y_pos = 1; int first_x =1; int first_y =1; //ball size int radius = 5; //direction int x_speed = 1; int y_speed = 1; //applet size int appletsize_top = 10; int appletsize_bottom = 370; int appletsize_left = 10; int appletsize_right = 370; //random movement Random rnd = new Random (); //Drawing image off screen private Image dbImage; private Graphics dbg; int maxspeed = 1; // start - method is called every time you enter the HTML public void start () { // define a new thread Thread th = new Thread (this); // start this thread th.start (); } //run- start the applet game public void run () { while (true) { // lower ThreadPriority Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //change direction if ball hits right side if (x_pos > appletsize_left) { // Setzen der x - Position x_pos += first_x; y_pos += first_y; x_speed = (rnd.nextInt ()) % maxspeed; } else if (x_pos < appletsize_right) { // Setzen der x - Position x_pos += first_x; y_pos += first_y; x_speed = (rnd.nextInt ()) % maxspeed; } else if (y_pos > appletsize_top) { // Setzen der x - Position x_pos += first_x; y_pos += first_y; x_speed = (rnd.nextInt ()) % maxspeed; } else if (y_pos < appletsize_bottom) { // Setzen der x - Position x_pos += first_x; y_pos += first_y; x_speed = (rnd.nextInt ()) % maxspeed; } //repaint applet repaint(); try { // Stop thread for the speed Thread.sleep (10); } catch (InterruptedException ex) { } // set ThreadPriority to maximum value Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } //create an image off screen and loads it into the applet public void update (Graphics g) { // initialize buffer if (dbImage == null) { dbImage = createImage (this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics (); } // clear screen in background dbg.setColor (getBackground ()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); // draw elements in background dbg.setColor (getForeground()); paint (dbg); // draw image on the screen g.drawImage (dbImage, 0, 0, this); } /** paint - method allows you to paint into your applet. This method is called e.g. if you move your browser window or if you call repaint() */ public void paint (Graphics g) { // set color g.setColor (Color.red); // paint a filled colored circle g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); } // Method to handle mouse down events public boolean mouseDown (Event e, int x, int y) { // Change direction, put speed into a minus number 1 now become -1 x_speed = - (x_speed); return true; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-18-2008, 04:48 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,017
hardwired is on a distinguished road
Here's how you deal with deprecation warnings:
Code:
C:\jexp>javac firstappletrx.java Note: firstappletrx.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. C:\jexp>javac -Xlint:deprecation firstappletrx.java firstappletrx.java:141: warning: [deprecation] mouseDown(java.awt.Event,int,int) in java.a wt.Component has been deprecated public boolean mouseDown (Event e, int x, int y) ^ 1 warning
Code:
// <applet code="FirstAppletRx" width="400" height="400"></applet> import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class FirstAppletRx extends Applet implements Runnable { //position of the ball int x_pos = 12; int y_pos = 12; // int first_x = 1; // int first_y = 1; //ball size int radius = 5; //direction int x_speed = 3; int y_speed = 2; //applet size int appletsize_top = 10; int appletsize_bottom = 370; int appletsize_left = 10; int appletsize_right = 370; //random movement Random rnd = new Random (); //Drawing image off screen private Image dbImage; private Graphics dbg; int maxspeed = 5; Thread thread; boolean running = false; public void init() { addMouseListener(ma); } // start - method is called after init // and every time the applet is maximized public void start () { // define a new thread running = true; thread = new Thread (this); // start this thread thread.start (); System.out.println("start"); } // A well–behaved Applet stops // its animation when minimized. public void stop() { running = false; if(thread != null) thread.interrupt(); thread = null; System.out.println("stop"); } //run- start the applet game // Implementation of the Runnable interface. public void run () { while (running) { // lower ThreadPriority Thread.currentThread().setPriority(Thread.MIN_PRIORITY); // change direction if ball hits any side if (x_pos < appletsize_left || x_pos > appletsize_right) { x_speed *= -1; } if (y_pos < appletsize_top || y_pos > appletsize_bottom) { y_speed *= -1; } x_pos += x_speed; y_pos += y_speed; //repaint applet repaint(); try { // Stop thread for the speed Thread.sleep (50); } catch (InterruptedException ex) { running = false; break; } // set ThreadPriority to maximum value Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } //create an image off screen and loads it into the applet public void update (Graphics g) { // initialize buffer if (dbImage == null) { dbImage = createImage (this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics (); } // clear screen in background dbg.setColor (getBackground ()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); // draw elements in background dbg.setColor (getForeground()); paint (dbg); // draw image on the screen g.drawImage (dbImage, 0, 0, this); } /** * paint - method allows you to paint into your applet. * This method is called e.g. if you move your browser * window or if you call repaint() */ public void paint (Graphics g) { // set color g.setColor (Color.red); // paint a filled colored circle g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); } private MouseListener ma = new MouseAdapter() { // Method to handle mousePressed events public void mousePressed(MouseEvent e) { // Preserve the direction of x_speed [-:left +:right]. //int sign = (int)Math.signum(x_speed); // j2se 1.5+ // or we can try: int sign = (x_speed > 0) ? 1 : -1; x_speed = 1 + rnd.nextInt(maxspeed); // [1 - maxspeed] // Restore the sign. x_speed *= sign; } }; }
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
Multiple bouncing balls Algar AWT / Swing 2 04-24-2008 09:35 PM
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
Need help making ball move and bounce off of sides of JPanel adlb1300 New To Java 2 12-01-2007 08:48 AM


All times are GMT +3. The time now is 11:28 PM.


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