function for swing the ball with appropriate angle
View Answers
January 19, 2009 at 6:10 AM
Hi friend,
import java.awt.*;
import java.util.*;
public class Ball {
float x, y;
float radius;
float speed;
float moveAngle;
private Color color;
// Constructor
public Ball(float x, float y, float radius, float speed, float angleInDegree, Color color) {
this.x = x;
this.y = y;
this.speed = speed;
this.moveAngle = (float)Math.toRadians(angleInDegree);
this.radius = radius;
this.color = color;
}
public float getSpeedX() {
return speed * (float)Math.cos(moveAngle);
}
public float getSpeedY() {
return speed * (float)Math.sin(moveAngle);
}
// Update the position of the ball (move one step).
public void update() {
x += getSpeedX();
y += getSpeedY();
}
// Draw itself
public void draw(Graphics g) {
g.setColor(color);
g.fillOval((int)(x - radius), (int)(y - radius), (int)(2 * radius), (int)(2 * radius));
}
// For debugging.
public String toString() {
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
formatter.format("(%3.0f,%3.0f) V=%5.2f \u0398=%8.3f",
x, y, speed, Math.toDegrees(moveAngle));
return sb.toString();
}
}
January 19, 2009 at 6:10 AM
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class BallWorld extends JFrame {
private static final int BOX_WIDTH = 640;
private static final int BOX_HEIGHT = 480;
private static final int UPDATE_RATE = 30;
private static final long UPDATE_PERIOD = 1000L / UPDATE_RATE;
private Ball ball;
// Handle for the custom drawing panel
private GameCanvas box;
// Constructor to create the UI components and init the game objects
public BallWorld() {
// UI components
box = new GameCanvas();
box.setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));
this.setContentPane(box);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
this.setTitle("Bouncing Ball");
this.setVisible(true);
// Start the game
gameStart();
}
// Start the ball bouncing
public void gameStart() {
// Generate a ball at a random location and moveAngle.
int radius = 200;
Random rand = new Random();
int x = rand.nextInt(BOX_WIDTH - radius * 2) + radius;
int y = rand.nextInt(BOX_HEIGHT - radius * 2) + radius;
int speed = 5;
int angleInDegree = rand.nextInt(360) - 179;
ball = new Ball(x, y, radius, speed, angleInDegree, Color.pink);
Thread gameThread = new Thread() {
public void run() {
while (true) {
gameUpdate();
repaint();
try {
Thread.sleep(UPDATE_PERIOD);
} catch (InterruptedException ex) { }
}
}
};
gameThread.start();
}
// Update the game objects and states
public void gameUpdate() {
// Compute the new position
ball.update();
// Detect collision and provide response
box.collidedWith(ball);
}
class GameCanvas extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.BLACK);
ball.draw(g);
// Display ball info for debugging.
g.setColor(Color.WHITE);
g.setFont(new Font("Courier New", Font.PLAIN, 12));
g.drawString("Ball: " + ball, 10, 20);
}
// Detect collision with the given ball, and provide response.
public boolean collidedWith(Ball ball) {
float currX = ball.x;
float currY = ball.y;
float radius = ball.radius;
if (currX + radius < BOX_WIDTH && currX - radius >= 0
&& currY + radius < BOX_HEIGHT && currY - radius >= 0) {
return false;
}
January 19, 2009 at 6:11 AM
// collision
synchronized (ball) {
float prevX = currX - ball.getSpeedX();
float prevY = currY - ball.getSpeedY();
float angle = ball.moveAngle;
float angleTangent = (float)Math.tan(angle);
float newX, newY;
if (currX + radius >= BOX_WIDTH) {
newX = BOX_WIDTH - 1 - radius;
newY = angleTangent * (newX - prevX) + prevY;
ball.x = newX;
ball.y = newY;
// Keep the angle within -pi to pi (not necessary)
if (angle >= 0) {
ball.moveAngle = (float)Math.PI - angle;
} else {
ball.moveAngle = -(float)Math.PI - angle;
}
} else if (currX - radius < 0) {
newX = radius;
newY = angleTangent * (newX - prevX) + prevY;
ball.x = newX;
ball.y = newY;
// Keep the angle within -pi to pi (not necessary)
if (angle >= 0) {
ball.moveAngle = (float)Math.PI - angle;
} else {
ball.moveAngle = -(float)Math.PI - angle;
}
}
currX = ball.x;
currY = ball.y;
angle = ball.moveAngle;
if (currY + radius >= BOX_HEIGHT) {
newY = BOX_HEIGHT - 1 - radius;
newX = (newY - prevY) / angleTangent + prevX;
ball.x = newX;
ball.y = newY;
ball.moveAngle = -angle;
} else if (currY - radius < 0) {
newY = radius;
newX = (newY - prevY) / angleTangent + prevX;
ball.x = newX;
ball.y = newY;
ball.moveAngle = -angle;
}
}
return true;
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new BallWorld();
}
});
}
}
---------------------------------------
visit for more information:
http://www.roseindia.net/java/example/java/swing/Thanks.
January 19, 2009 at 6:12 AM
Amardeep
Related Tutorials/Questions & Answers:
why my keyevent not function ? - java swing -why my keyevent not
function ? - java
swing - import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import...();
frame.setTitle( "Four
Function Calculator");
frame.setSize( 500, 120
Advertisements
why my keyevent not function ? - java swing -why my keyevent not
function ? - java
swing - import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import...();
frame.setTitle( "Four
Function Calculator");
frame.setSize( 500, 120
ModuleNotFoundError: No module named 'angle'ModuleNotFoundError: No module named '
angle' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
angle'
How to remove the ModuleNotFoundError: No module named '
angle'
ModuleNotFoundError: No module named 'angle'ModuleNotFoundError: No module named '
angle' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
angle'
How to remove the ModuleNotFoundError: No module named '
angle'
ModuleNotFoundError: No module named 'Ball'ModuleNotFoundError: No module named '
Ball' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
Ball'
How to remove the ModuleNotFoundError: No module named '
Ball' error
ModuleNotFoundError: No module named 'Ball'ModuleNotFoundError: No module named '
Ball' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
Ball'
How to remove the ModuleNotFoundError: No module named '
Ball' error
Moving ball - Applet java.lang.Thread;
import java.lang.*;
public class Moving_
ball extends Applet...)
{}
}
}
public static void main(String args[])
{
Moving_
ball mb=new Moving_
ball();
}
}
In the above program I would like to make the
ball appear
ModuleNotFoundError: No module named 'crazy-ball'ModuleNotFoundError: No module named 'crazy-
ball' Hi,
My Python... 'crazy-
ball'
How to remove the ModuleNotFoundError: No module named 'crazy-
ball' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'crazy-ball'ModuleNotFoundError: No module named 'crazy-
ball' Hi,
My Python... 'crazy-
ball'
How to remove the ModuleNotFoundError: No module named 'crazy-
ball' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'py-ball'ModuleNotFoundError: No module named 'py-
ball' Hi,
My Python...-
ball'
How to remove the ModuleNotFoundError: No module named 'py-
ball... to install padas library.
You can install py-
ball python with following command
ModuleNotFoundError: No module named 'crazy-ball'ModuleNotFoundError: No module named 'crazy-
ball' Hi,
My Python... 'crazy-
ball'
How to remove the ModuleNotFoundError: No module named 'crazy-
ball' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'crazy-ball'ModuleNotFoundError: No module named 'crazy-
ball' Hi,
My Python... 'crazy-
ball'
How to remove the ModuleNotFoundError: No module named 'crazy-
ball' error?
Thanks
Hi,
In your python environment you
moving function - Java3Dmoving function i am working on one project in java. can their is
function available for speed. i want move 2d
ball with
appropriate speed. can i increse or decrese speed with the help of java
function Hi friend
SWINGSWING A JAVA CODE OF MOVING TRAIN IN
SWING functionfunction difference between
function overloading and operator overloading
swingswing Write a java
swing program to delete a selected record from a table
swingswing How to make
swing component auto-resizable when JFrame resize
SwingSwing Write a java
swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
SwingSwing Write a java
swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
java swing - Swing AWTjava swing how i can insert multiple cive me exampleolumn and row in one JList in
swing?plz g Hi Friend,
Please clarify your question.
Thanks
swing-awt - Swing AWTswing-awt Hi,
Thanks for replying to my question...I'm getting some confusion to add action events in my application(Rich Text Editor).How to add action events?
Thank U
What is Swing?What is
Swing? What is Java
Swing? Where to get the tutorials of
Swing? I am beginner in Java
Swing programming and trying to find the tutorials of
Swing.
Tell me the urls to learn
swing.
Thanks
Hi,
Swing is Java
swing/awt - Swing AWTswing/awt How to create richtexteditor using swings...?I'm very much new to swings....It's urgent.....Thank u...
hello
read this book you get idea;
JFC
Swing Tutorial, The: A Guide to Constructing GUIs, Second
"Doubt on Swing" - Java Beginners"Doubt on
Swing" Hi Friend....
Thanks for ur goog Response..
i need to create a GUI Like...
pic1.gif RadioButton
pic2.gif RadioButton
Pic3.gif RadioButton
If we have select d
appropriate radio
Swing paint - Swing AWTSwing paint hi,
i want to print something on window using
swing applet..
m doing dis..
protected void paintComponent(Graphics g... the
Swing Applet, use html file with the following code:
Java Applet Demo
swing smsswing sms HOW TO SEND SMS MESSAGE FROM
SWING USING SQL DATABASE
Swing ProgramSwing Program Write a java
swing program to calculate the age from given date of birth
SWING FRMESSWING FRMES hai SIR?
HOW TO DESIGN
swing Frames
send source code
SWING FRMESSWING FRMES hai SIR?
HOW TO DESIGN
swing Frames
send source code
swing to appletswing to applet hi how i can isplay a java
swing into applet java
thanks
java swing - Swing AWTjava
swing how i can insert in JFrame in
swing? Hi Friend,
Try the following code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class FormDemo extends JFrame {
JButton ADD;
JPanel
java swing - Swing AWTjava swing how to add image in JPanel in
Swing? Hi Friend,
Try the following code:
import java.awt.*;
import java.awt.image....:
http://www.roseindia.net/java/example/java/
swing/
Thanks
Java swingJava swing what are the root classes of all classes in
swing Java swingJava swing Does
Swing contains any heavy weight component
AWT & SWINGAWT & SWING What is diffennce between AWT &
SWING java swingjava swing view the book details using
swing Jva swing Jva
swing How to create the model form like "Notepad
swing programswing program Write a java
swing program to getname and email id. Display the message if email id contains the name of theuser
SWING - Swing AWTSWING how can i insert image in Jpanel by extending class with JFrame in
swing? Hi Friend,
Try the following code:
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import