| | |
Bouncing ball Program
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 26
Reputation:
Solved Threads: 0
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..
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.
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.
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.
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: J2ME mp3 player for motorola mobile phone
- Next Thread: Help, how would I store an array of objects ?
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp draw eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






