Post: Bouncing Ball Program - Java
04-10-2012, 09:25 PM #1
Chaz
Tremble Underfoot
(adsbygoogle = window.adsbygoogle || []).push({}); Just something I did last semester in my computer science class :p Nothing spectacular,
but for novice programmers this is something cool. Check it out Smile

    import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class BouncingBall extends JApplet
{
public void paint(Graphics graphics)
{
int x = 101,
y = 100, //initial position of ball
xMax = 300, //max horizontal size of panel
xMin = 20,
yMax = 265,
yMin = 20,
xInc = 5, //amount to increment x,y
yInc = 5,
radius = 40; //radius of ball
double delay = 0.02; // in seconds

//draw border for ball to bounce in
graphics.setColor(Color.red);
graphics.drawRect(xMin-5,yMin-1,xMax-xMin+radius+6,yMax-yMin+radius+1);

final Color ballColor = Color.blue;
graphics.setColor(ballColor);

while(true)
{
//cover blue ball with a white one
graphics.setColor(Color.white);
graphics.fillOval(x,y,radius,radius);

//increment x, y coordinates
x+=xInc;
y+=yInc;

//(re)set color of ball and draw it
graphics.setColor(ballColor);
graphics.fillOval(x,y,radius,radius);

//pause
Wait.pause(delay);

//change direction of ball when it reaches the border
if (x<=xMin || x>=xMax)
xInc*=-1;
if (y<=yMin || y>=yMax)
yInc*=-1;

//draw a white ball to cover the previous red one
graphics.setColor(Color.white);
graphics.fillOval(x,y,radius,radius);

}
}

}


Then for the Wait class, just compile this code and keep it in the same directory as the
above code, but don't run it. It's just needed so the Bouncing Ball class works. Smile

    public class Wait{
public static void pause(double sec)
{

//Causes the terminal to pause for SEC seconds
int msec = (int)(1000.0*sec + 0.5);

final int max = 3000; //pause no more than 4 seconds

msec = msec> max ? max : msec;
try
{
Thread.currentThread().sleep(msec);
}
catch (InterruptedException e)
{
//e.printStackTrace();
}

}
}


Thats all Smile

(adsbygoogle = window.adsbygoogle || []).push({});

The following user thanked Chaz for this useful post:

Pichu
04-13-2012, 05:47 PM #2
Pichu
RIP PICHU.
Originally posted by ChazAnus View Post
Just something I did last semester in my computer science class :p Nothing spectacular,
but for novice programmers this is something cool. Check it out Smile

    import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class BouncingBall extends JApplet
{
public void paint(Graphics graphics)
{
int x = 101,
y = 100, //initial position of ball
xMax = 300, //max horizontal size of panel
xMin = 20,
yMax = 265,
yMin = 20,
xInc = 5, //amount to increment x,y
yInc = 5,
radius = 40; //radius of ball
double delay = 0.02; // in seconds

//draw border for ball to bounce in
graphics.setColor(Color.red);
graphics.drawRect(xMin-5,yMin-1,xMax-xMin+radius+6,yMax-yMin+radius+1);

final Color ballColor = Color.blue;
graphics.setColor(ballColor);

while(true)
{
//cover blue ball with a white one
graphics.setColor(Color.white);
graphics.fillOval(x,y,radius,radius);

//increment x, y coordinates
x+=xInc;
y+=yInc;

//(re)set color of ball and draw it
graphics.setColor(ballColor);
graphics.fillOval(x,y,radius,radius);

//pause
Wait.pause(delay);

//change direction of ball when it reaches the border
if (x<=xMin || x>=xMax)
xInc*=-1;
if (y<=yMin || y>=yMax)
yInc*=-1;

//draw a white ball to cover the previous red one
graphics.setColor(Color.white);
graphics.fillOval(x,y,radius,radius);

}
}

}


Then for the Wait class, just compile this code and keep it in the same directory as the
above code, but don't run it. It's just needed so the Bouncing Ball class works. Smile

    public class Wait{
public static void pause(double sec)
{

//Causes the terminal to pause for SEC seconds
int msec = (int)(1000.0*sec + 0.5);

final int max = 3000; //pause no more than 4 seconds

msec = msec> max ? max : msec;
try
{
Thread.currentThread().sleep(msec);
}
catch (InterruptedException e)
{
//e.printStackTrace();
}

}
}


Thats all Smile



Didn't compile, but bouncing ball games makes me giggle. :P

---
I'm slowly reworking a snake game, I wrote out the main parts of it in C# but now am just making it pretty. Adding audio to it and such.

Since I don't have XNA, I can't play multiple sounds at once, (EG. Background then if it eats a food, a munch sound can played along with the background) since I don't have XNA, I can't do that unless I want to reset the audio.
04-22-2012, 04:45 AM #3
no shit, where do you go to school at.?
i'm going to ITT tech and we did something similar to this. lol
i made mine with a picture of me that bounced though. it was neat.
04-26-2012, 08:33 AM #4
Earnesterin
Save Point
A number of issues arise in considering hacking from the educator perspective. First, we need to consider the fact that the public perception of hackers is mixed, and that "hacking" and "being considered a hacker" can be quite appealing to students who are going through developmental periods in which they are defining themselves, as well as challenging authority and rules. There is often a Robin Hood mentality to early actions, though it is unclear exactly who "the poor" are, and how they are "beingcompensated". Second, the anonymity of actions which hackers perform against others often enhances the severity of actions.
05-06-2012, 12:12 AM #5
<Javascript = You must login or register to view this content. :p.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo