Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Applet Moving ball
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Avishek Bhattacharyya
Moving ball
3 Answer(s)      4 years and 8 months ago
Posted in : Applet


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!!!!!

Advertisement
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
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. Halftone Pattern: Go to filter menu > Sketch
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
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. Capture a ball: Capture any cricket ball picture and set
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
Manhattan Moving Services
Manhattan Moving Services  **Deleted by admin *** - Shleppers Moving &
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
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
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
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
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... of columns. Description of program: This program helps you in moving a column
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
. The shape of the ball is given by graphics.fillOval(p, q, P, Q). Here... 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
. The shape of the ball is given by graphics.fillOval(p, q, P, Q). Here... 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
jQuery chain-able state of transition
jQuery chain-able state of transition       In this JQuery tutorial we will develop a program that changing the state of transition of box moving starting Left -> Right
What is Differential GPS?
the accuracy to within two meters of the actual position for moving objects... and the other moving around making position measurements. Here... of error as the moving receiver. The stationary receiver then works backwards
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
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
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
Advertisements
 

 

 

DMCA.com