Moving ball

Moving ball

import java.awt.*;
import java.applet.*;
import java.lang.Thread;
import java.lang.*;

public class Moving_ball extends Applet
{
Thread t;
int i;
int x=34,y=14;

public void init()
{
t=new Thread();
}
public void paint(Graphics g)
{
for(i=1;i<=5;i++)
{
g.fillOval(x,y,40,40);
g.setColor(Color.red);
x=x+30;
y=y+30;
repaint();

try
{
t.sleep(1000);
}
catch(Exception e)
{}
}
}

public static void main(String args[])
{
Moving_ball mb=new Moving_ball();
}
}


In the above program I would like to make the ball appear as if it is rolling.I would be grateful if u help!!!!!
View Answers

February 9, 2010 at 12:27 PM

Hi Friend,

Try the following code;

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

public class Moving_ball extends JApplet{
int x,y;
int X1, Y1;
int R = 10;
int S = 10;
java.util.Timer timer;
TimerTask task;
public void init() {
x=75;
y=50;
X1=2*S;
Y1=S;
timer= new java.util.Timer();
task= new TimerTask() {
public void run() {
x+= X1;
y+= Y1;
if ((x-R) <= 0)
X1*= -1;
else if ((x+R) >= getWidth())
X1*= -1;

if ((y-R) <= 0)
Y1*= -1;
else if ((y+R) >= getHeight())
Y1*= -1;

Graphics g= getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.BLUE);
g.fillOval(x-R, y-R, R*2, R*2);
}
};
timer.schedule( task , 0, 50);
}
}

Call this applet with the following applet.html :

<html>
<body>
<h1>Java Applet Demo</h1>
<applet code=Moving_ball.class width=500 height=400>
</applet>
</body>
</html>

For more information, visit the following link:

http://www.roseindia.net/java/example/java/swing/graphics2D/bouncing-thread.shtml

Thanks

April 23, 2012 at 4:26 PM

hi frnds ,

i need to move an object with different velocity or speed that should be accelerate ? how we classify THE SPEED is 30 km / hr or 20km/hr or 2 km/hr.... reply me *Email ID Deleted*


April 23, 2012 at 4:27 PM

i need an object to move with velocity , speed and accelerate that ? how can an object move with speed 30km/ hr , 20 km. hr or 3 km / hr ?

thanks for giving us knowledege og java in various blogs and posts









Related Tutorials/Questions & Answers:
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
Moving The Images
Moving The Images  How to moving multiple images in one frame using swings
Advertisements
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 function - Java3D
moving 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
Manhattan Moving Services
Manhattan Moving Services  **Deleted by admin *** - Shleppers Moving &
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 '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 'crystal-ball'
ModuleNotFoundError: No module named 'crystal-ball'  Hi, My Python... 'crystal-ball' How to remove the ModuleNotFoundError: No module named 'crystal-ball' error? Thanks   Hi, In your python
How to design a golf ball, design a golf ball, a golf ball
How to design a golf ball       Make your own golf ball shape by take help of this example. New File: Take a new file.ADS_TO_REPLACE_1 Halftone Pattern: Go to filter
How to make a blast soccer ball, make a blast soccer ball, blast soccer ball
How to make a blast soccer ball       You have chance to learn a blast soccer ball by this example, I is fully described to learn easily.  Ball picture: First take a soccer
How to design a bouncing cricket ball, design a bouncing cricket ball, cricket ball
How to design a bouncing cricket ball       Now you can bounce a cricket ball by using... a cricket ball.ADS_TO_REPLACE_1 Capture a ball: Capture any cricket ball
ModuleNotFoundError: No module named 'magic-py-ball'
ModuleNotFoundError: No module named 'magic-py-ball'  Hi, My... 'magic-py-ball' How to remove the ModuleNotFoundError: No module named 'magic-py-ball' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'opencv-rolling-ball'
ModuleNotFoundError: No module named 'opencv-rolling-ball'  Hi, My... named 'opencv-rolling-ball' How to remove the ModuleNotFoundError: No module named 'opencv-rolling-ball' error? Thanks   Hi
Moving Applet Problem
Moving Applet Problem  When image scroll in applet then a blinking on image is continuously working.how to remove this blinking.. import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import
ModuleNotFoundError: No module named 'moving-chameleon'
ModuleNotFoundError: No module named 'moving-chameleon'  Hi, My... named 'moving-chameleon' How to remove the ModuleNotFoundError: No module named 'moving-chameleon' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'moving_pictures'
ModuleNotFoundError: No module named 'moving_pictures'  Hi, My... named 'moving_pictures' How to remove the ModuleNotFoundError: No module named 'moving_pictures' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'moving-stars'
ModuleNotFoundError: No module named 'moving-stars'  Hi, My Python... 'moving-stars' How to remove the ModuleNotFoundError: No module named 'moving-stars' error? Thanks   Hi, In your python
function for swing the ball with appropriate angle - Java3D
function for swing the ball with appropriate angle  how i can swing... java.awt.*; import java.util.*; public class Ball { float x, y...; private Color color; // Constructor public Ball(float x, float y
monitoring framework for continuously moving objects
monitoring framework for continuously moving objects  hi i want a source code on if the object moves outt of the safe region i want to get the update to the server
Moving Images in GUI - Swing AWT
Moving Images in GUI  I want to add an image of a person in my GUI and make it move to a specific location. How can I add the image to the frame and move
moving docker image to another host
moving docker image to another host  Hi, I have few projects running on the Docker container. I have to build the docker image and send the image... to give it to the testing team for testing. Tell me the stpes for moving docker
draw the running or moving graph using database in jsp
draw the running or moving graph using database in jsp  hi my question is how to draw the running or moving graph using jsp and take x and y co-ordinate value from database
draw the running or moving graph using database in jsp
draw the running or moving graph using database in jsp  hi my question is how to draw the running or moving graph using jsp and take x and y co-ordinate value from database
how to write weighted moving average code using 2d-array
how to write weighted moving average code using 2d-array  how to write weighted moving average code using 2d-array
Moving projects from tomcat-6.0 to jboss 5.0.1 GA
Moving projects from tomcat-6.0 to jboss 5.0.1 GA  Now i am using tomcat server version6.0 for my project. Now i want to change the server from tomcat to jboss 5.0.1 GA. What are the things i have to do for using jboss instead
GXT Grid moving right side after right alighnment
GXT Grid moving right side after right alighnment  I have created one gxt grid.there are 7 column and last column align as RIGHT .so grid width is moving right side 2px. so how can i fix
Jkmegamenu drop downs moving left when window is resized in Chrome
Jkmegamenu drop downs moving left when window is resized in Chrome  I'm using the JKmegamenu plugin to implement a megamenu on a website I'm working on. It works and looks nice, however when the web browser(Chrome) window
Example of copying and moving Oracle VirtualBox VM to another machine
Example of copying and moving Oracle VirtualBox VM to another machine  Hi, What could be the best and easiest method to copy a vm from one machine to another machine. I want to move a VM from Oracle Virtual box to another
Moving Cursor within ResultSet
; } Moving Cursor Within ResultSet There are many methods are given to move... direction System.out.println("Moving Forward"); while (resultSet.next...: Moving Forward Roll No 1, Name Rajan, Course MCA, Address Motihari
Moving a Column in JTable
Moving a Column in JTable      ... component. Moving is a very simple method  that  moves the data... in moving a column into  JTable. This program creates a table through the JTable
Moving Div by button click using animate effect
Moving Div by button click using animate effect In this tutorial , we will discuss how to move 'div' left-right by button click using jQuery. In this example , 'div' is moving left / right by left / right button click
Moving file or directory from one directory to another
Moving file or directory from one directory to another... illustrates you the method or procedure for moving a file or directory contents... method to complete the program for moving file or complete folder from specified
Copying and Moving data using drag and drop
Copying and Moving data using drag and drop: You can drag and drop the data from list to dataGrid using different data formats in this example. When you move data and add it to the drop target it will be removed from draginitiator
Bouncing Thread Example
for the ball are defined. The shape of the ball is given by graphics.fillOval(p, q, P... application that creates a ball which bounce with the help of thread in Graphics.... On pushing the button 'Start', class ActionListener is called and the ball
Bouncing Thread Example
for the ball are defined. The shape of the ball is given by graphics.fillOval(p, q... application that creates a ball which bounce with the help of thread in Graphics.... On pushing the button 'Start', class ActionListener is called and the ball
Spark Move3D Effect in Flex4
Spark Move3D Effect in Flex4: The Move3D effect is used for moving the component in the x, y and z coordinate system. If you specify the moving in z direction the effect will look like a Zoom effect. The tag of Move3D effect is <s
Fleet tracking services
Fleet tracking services helps people in fleet business to know the exact location of their moving fleet. These device are preinstalled in the vehicles, which then collect all the information related to vehicle, and stores them, it also
What is Differential GPS?
the accuracy to within two meters of the actual position for moving objects... of which is stationary and the other moving around making position measurements. Here... with the same amount of error as the moving receiver. The stationary receiver then works
jQuery chain-able state of transition
jQuery chain-able state of transition       ADS_TO_REPLACE_1 In this JQuery tutorial we will develop a program that changing the state of transition of box moving starting
Data Provider or other service returned an E_FAIL status Error
Error" while moving the Record Set forward. On trying for the solution I found
Mobile Tracking System
A mobile tracking system is a web-based solution that uses state-of-art technology and authentication methods to attain the position of stationary or moving mobile phone. Mobile tracking is done with the help of the GPS technology
java - Java Beginners
java  1)write a java applet programme to implement moving a ball from top to bottom  Hi Friend, Please visit the following link and create applet from the code: http://www.roseindia.net/java/example/java
Restaurant Search Applications ? Find New Places To Eat, Fast!
Restaurant Search Applications – Find New Places To Eat, Fast! Mobile phones have gone from being functional to being an all round ball of fun that helps you stay connected and get more out of life. Applications are a great way
HSlider in Flex
Adobe Flex HSlider Controls In Adobe Flex HSlider Control we can change the value by moving the slider thumb between the end points of the slider track. The current value is determined by the relative location of the thumb
GPS Tracking Services
is a GPS technology based continuous tracking service of moving vehicles, vessels and air cargoes. It works for keep eyeing on the moving trucks, loaded
NumericStepper in Flex
NumericStepper We can use NumericStepper control of Flex to select a number from a set. This Control looks like a list box which contains a text box and two arrow buttons for moving through the valid numbers. As the direction

Ads