Bouncing ball Program

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 26
Reputation: D boss is an unknown quantity at this point 
Solved Threads: 0
D boss D boss is offline Offline
Light Poster

Bouncing ball Program

 
0
  #1
Apr 18th, 2008
hi guys,

i am working on a new java program, the program consists of a ball, bouncing around the applet, i have created button to control the ball, one to stop the ball bouncing, another to resume the ball boucing and another to reverse the direction of the ball bouncing,

but i am trying to re-design the buttons so that i can steer the direction of ball / drag the ball with the mouse around the applet.
the code so far is below...

any advice and help, would be appreciated a lot.....
______________________________________________________________________

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Bounce extends Applet implements Runnable {
int x_shift = 1;
int y_shift = 1;
Thread t;
boolean bouncing;
boolean testOutput = true;
Button
b1 = new Button("Start"),
b2 = new Button("Reverse Direction"),
b3 = new Button("Stop");

public void init() {
b1.addActionListener(new B1());
b2.addActionListener(new B2());
b3.addActionListener(new B3());
add(b1);
add(b2);
add(b3);



r = new Rectangle(30,50,20,20);
t = new Thread(this);
t.start();// behaviour of thread controlled by run method
bouncing = true;
}
public void paint(Graphics g) {
if (bouncing) g.setColor(Color.blue);
else g.setColor(Color.red);
g.fillOval(r.x,r.y,r.width,r.height);
if (testOutput)
System.out.println ("x = " + r.x + " y = " + r.y + " bouncing = " + bouncing);
}
class B1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
t.resume();
bouncing = true;
getAppletContext().showStatus("Start pressed");
}
}
class B2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (!bouncing)

t.suspend();
bouncing = true;
y_shift *= -1;
x_shift *= -1;
getAppletContext().showStatus("Reverse Direction pressed");
}
}
class B3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (!bouncing)
System.exit(1);
t.suspend();
bouncing = false;
x_shift *= -1;
getAppletContext().showStatus("Stop pressed");
}
}
public void update(Graphics g) {

Image offScreenImage = createImage(getSize().width, getSize().height);
paint(offScreenImage.getGraphics());
g.drawImage(offScreenImage,0,0, null);
}
public void run() {
while (true) { // Thread performs endless loop
r.x += x_shift;
r.y += y_shift;
if (r.x>= getSize().width || r.x < 0) {
x_shift *= -1;
}
if (r.y>= getSize().height || r.y < 40) { // Why 40?
y_shift *= -1;
}
repaint();
try {
Thread.currentThread().sleep(2);
}
catch (Exception e) {
}
}
}


private Rectangle r;



public boolean mouseDown(Event e) {


return true;
}
public boolean mouseDrag(Event e ) {

return true;
}
public boolean mouseUp(Event e) {

return true;
}

}

_________________________________________________________________________

thx for your time,
tc..
Last edited by D boss; Apr 18th, 2008 at 8:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 26
Reputation: D boss is an unknown quantity at this point 
Solved Threads: 0
D boss D boss is offline Offline
Light Poster

Re: Bouncing ball Program

 
-1
  #2
Apr 18th, 2008
.
Last edited by D boss; Apr 18th, 2008 at 8:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 26
Reputation: D boss is an unknown quantity at this point 
Solved Threads: 0
D boss D boss is offline Offline
Light Poster

Re: Bouncing ball Program

 
0
  #3
Apr 19th, 2008
or how can i make the ball go around the rectangle / applet?

without the ball going all over the place..
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Bouncing ball Program

 
0
  #4
Apr 19th, 2008
if you programmed it correctly it won't be "going all over the place" but following a highly predictable path dictated by it's starting position and energy vector.
So you change those and it will follow whatever path you want it to if.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Bouncing ball Program

 
0
  #5
Apr 19th, 2008
If you want to drag the ball around with mouse you might want to implement MouseMotionListener. It has a listener mouseDragged.
Last edited by jasimp; Apr 19th, 2008 at 9:32 am.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 26
Reputation: D boss is an unknown quantity at this point 
Solved Threads: 0
D boss D boss is offline Offline
Light Poster

Re: Bouncing ball Program

 
0
  #6
Apr 19th, 2008
the program runs ..ok..without any problems, but the ball goes in all directions i.e zig zag, up down, left right.

i actually wanted to make the ball go around the applet only, running in a rectangle shape.

but thx aniwais for the advice.
tc
Last edited by D boss; Apr 19th, 2008 at 9:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 26
Reputation: D boss is an unknown quantity at this point 
Solved Threads: 0
D boss D boss is offline Offline
Light Poster

Re: Bouncing ball Program

 
0
  #7
Apr 26th, 2008
i sorted it .. cheers guys.
tc
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Bouncing ball Program

 
0
  #8
Apr 27th, 2008
Thank you for marking your thread as solved. Not many people do that.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC