Title: How to implement an object in a JFrame and make it bounce of the walls
Tags: java;user-interface
Question: I have just started learning java, and don't know much about GUI Components except for JFrame and JLayout.
How do I implement an object (ball) in a JFrame, and make it bounce of the walls infinite times?
Comment: See [Ball Animation in Swing](http://stackoverflow.com/questions/9800968/ball-animation-in-swing) and this [followup](http://stackoverflow.com/q/9849950/230513).

Comment: SO is not for asking how to do something you don't have any idea of how to do, it's for stuff you've researched and hit a wall. In that case, you show your code, error messages along with a description of what's happening. Then we can help you fix the problem

Comment: Here's what google told me http://www.leepoint.net/notes-java/examples/animation/40BouncingBall/bouncingball.html

Comment: This question is much to broad and general for stackoverflow. Search for a tutorial or read a book on the subject.

Here is the accepted answer: You can create it via swing with Thread concept
You can create ball by swing drawImage in swing concept
To do the moving and delaying concept you can use Thread pakage
To do this and all see the reference Java complete edition 7
Comment for this answer: `Thread` is a little low-level; use `javax.swing.Timer`.

Here is another answer: One approach is to add a subclass of JPanel with overridden paintComponent method to your JFrame. This class could have fields for the ball position and javax.swing.Timer for repainting the panel. It could be something like this (not tested):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BouncingBall extends JPanel{
//Ball position
private int x = 0;
private int y = 0;
//Position change after every repaint
private int xIncrement = 5;
private int yIncrement = 5;
//Ball radius
private final int R = 10;
private Timer timer;
public BouncingBall(){
timer = new Timer(25, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BouncingBall.this.repaint();
}
});
timer.start();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//If the ball hits one of the panel boundaries
//change to the opposite direction
if (x < 0 || x &gt; getWidth()) {
xIncrement *= -1;
}
if (y < 0 || y &gt; getHeight()) {
yIncrement *= -1;
}
//increment position
x += xIncrement;
y += yIncrement;
//draw the ball
g.fillOval(x - R, y - R, R * 2, R * 2);
Comment for this answer: You have to add an instance of this class to JFrame. Invoke JFrame's add(new BouncingBall(), BorderLayout.CENTER)
Comment for this answer: okay, so how do i use this in the main class ?
Comment for this answer: Not good to have the logic within the painting method

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:04/13/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><user-interface>', 'Title': 'How to implement an object in a JFrame and make it bounce of the walls', 'ViewCount': 1898, 'id': 15986849, 'user_AboutMe': None, 'user_AccountId': 2631344.0, 'user_DisplayName': 'Abdul Moeed', 'user_Reputation': 19.0, 'user_Views': 17.0, 'user_id': 2277220.0}}