How do I make it so the circle in this frame actually moves automatically?












1















I can't get around this problem. I've created this frame and panel with my desired output. Only thing is that I haven't been able to figure out was how to make the ball move "automatically". My ideal game would have the ball/circle dropping at the start of the game, perhaps with a click of a button or such. How would I go about doing this?



I've tried to move the ball with the E key, but that would be too inconvenient for the user, so I figured that having it move without an event handler would be a better choice.



    private int ballX, ballY, ballR;
private int score1, score2;
private JPanel panel;
private JFrame frame;
private DrawingArea canvas;
private int xpos, ypos,width,height;
private int xpos2, ypos2, width2, height2;
public Pong(){
xpos = 300;
ypos = 550;
width = 100;
height = 50;

xpos2 = 300;
ypos2 = 100;
width2 = 100;
height2 = 50;

ballR = 50;
ballX = 325;
ballY = 330;


}
public static void main(Stringargs){
Pong p = new Pong();
p.run();
}
public void run(){
frame = new JFrame("Pong");
frame.setSize(700,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas = new DrawingArea(); // create a panel to draw on
canvas.setBackground(Color.WHITE);
canvas.addFocusListener(this);
canvas.addKeyListener(this);
canvas.addMouseListener(this);


frame.getContentPane().add(canvas);
frame.setVisible(true);
}
class DrawingArea extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor ( Color.blue );
g.fillRect ( xpos, ypos, width, height );
g.setColor(Color.red);
g.fillRect(xpos2, ypos2, width2, height2);


g.setColor(Color.black);
g.fillRect(0,0,700,50);
g.fillRect(0,630,700,50);

g.fillOval(ballX,ballY,ballR, ballR);

}
}
public void keyPressed ( KeyEvent e ) {
int value = e.getKeyCode();

switch ( value ) {
case KeyEvent.VK_RIGHT: xpos += 50; break;
case KeyEvent.VK_LEFT: xpos -= 50; break;
case KeyEvent.VK_A: xpos2 -= 50; break;
case KeyEvent.VK_D: xpos2 += 50; break;
/*try to drop the ball with the space button
* case KeyEvent.VK_SPACE:
ballX+=25;
ballY+=25;
break;
case KeyEvent.VK_ENTER:
xpos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
ypos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
break;
*/
}
if( (xpos < 0 || xpos >= 700) || (xpos2 < 0 || xpos2 >= 700)){
if(xpos < 0 || xpos2 < 0){
if(xpos < 0) xpos = 0;
else if(xpos2 < 0) xpos2 = 0;
return;
}
else if(xpos >= 700 || xpos2 >= 700){
if(xpos >= 700)xpos = 550;
else if(xpos2 >= 700) xpos2=550;
return;
}
}

canvas.repaint ( );
}

}


I expect the output to have the ball drop down on to the blue rectangle with the start of the game/press of a button, but I can't get that to work.










share|improve this question

























  • Use a Swing Timer to update the position the ball appears, then call repaint(). For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

    – Andrew Thompson
    Dec 31 '18 at 11:51
















1















I can't get around this problem. I've created this frame and panel with my desired output. Only thing is that I haven't been able to figure out was how to make the ball move "automatically". My ideal game would have the ball/circle dropping at the start of the game, perhaps with a click of a button or such. How would I go about doing this?



I've tried to move the ball with the E key, but that would be too inconvenient for the user, so I figured that having it move without an event handler would be a better choice.



    private int ballX, ballY, ballR;
private int score1, score2;
private JPanel panel;
private JFrame frame;
private DrawingArea canvas;
private int xpos, ypos,width,height;
private int xpos2, ypos2, width2, height2;
public Pong(){
xpos = 300;
ypos = 550;
width = 100;
height = 50;

xpos2 = 300;
ypos2 = 100;
width2 = 100;
height2 = 50;

ballR = 50;
ballX = 325;
ballY = 330;


}
public static void main(Stringargs){
Pong p = new Pong();
p.run();
}
public void run(){
frame = new JFrame("Pong");
frame.setSize(700,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas = new DrawingArea(); // create a panel to draw on
canvas.setBackground(Color.WHITE);
canvas.addFocusListener(this);
canvas.addKeyListener(this);
canvas.addMouseListener(this);


frame.getContentPane().add(canvas);
frame.setVisible(true);
}
class DrawingArea extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor ( Color.blue );
g.fillRect ( xpos, ypos, width, height );
g.setColor(Color.red);
g.fillRect(xpos2, ypos2, width2, height2);


g.setColor(Color.black);
g.fillRect(0,0,700,50);
g.fillRect(0,630,700,50);

g.fillOval(ballX,ballY,ballR, ballR);

}
}
public void keyPressed ( KeyEvent e ) {
int value = e.getKeyCode();

switch ( value ) {
case KeyEvent.VK_RIGHT: xpos += 50; break;
case KeyEvent.VK_LEFT: xpos -= 50; break;
case KeyEvent.VK_A: xpos2 -= 50; break;
case KeyEvent.VK_D: xpos2 += 50; break;
/*try to drop the ball with the space button
* case KeyEvent.VK_SPACE:
ballX+=25;
ballY+=25;
break;
case KeyEvent.VK_ENTER:
xpos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
ypos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
break;
*/
}
if( (xpos < 0 || xpos >= 700) || (xpos2 < 0 || xpos2 >= 700)){
if(xpos < 0 || xpos2 < 0){
if(xpos < 0) xpos = 0;
else if(xpos2 < 0) xpos2 = 0;
return;
}
else if(xpos >= 700 || xpos2 >= 700){
if(xpos >= 700)xpos = 550;
else if(xpos2 >= 700) xpos2=550;
return;
}
}

canvas.repaint ( );
}

}


I expect the output to have the ball drop down on to the blue rectangle with the start of the game/press of a button, but I can't get that to work.










share|improve this question

























  • Use a Swing Timer to update the position the ball appears, then call repaint(). For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

    – Andrew Thompson
    Dec 31 '18 at 11:51














1












1








1








I can't get around this problem. I've created this frame and panel with my desired output. Only thing is that I haven't been able to figure out was how to make the ball move "automatically". My ideal game would have the ball/circle dropping at the start of the game, perhaps with a click of a button or such. How would I go about doing this?



I've tried to move the ball with the E key, but that would be too inconvenient for the user, so I figured that having it move without an event handler would be a better choice.



    private int ballX, ballY, ballR;
private int score1, score2;
private JPanel panel;
private JFrame frame;
private DrawingArea canvas;
private int xpos, ypos,width,height;
private int xpos2, ypos2, width2, height2;
public Pong(){
xpos = 300;
ypos = 550;
width = 100;
height = 50;

xpos2 = 300;
ypos2 = 100;
width2 = 100;
height2 = 50;

ballR = 50;
ballX = 325;
ballY = 330;


}
public static void main(Stringargs){
Pong p = new Pong();
p.run();
}
public void run(){
frame = new JFrame("Pong");
frame.setSize(700,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas = new DrawingArea(); // create a panel to draw on
canvas.setBackground(Color.WHITE);
canvas.addFocusListener(this);
canvas.addKeyListener(this);
canvas.addMouseListener(this);


frame.getContentPane().add(canvas);
frame.setVisible(true);
}
class DrawingArea extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor ( Color.blue );
g.fillRect ( xpos, ypos, width, height );
g.setColor(Color.red);
g.fillRect(xpos2, ypos2, width2, height2);


g.setColor(Color.black);
g.fillRect(0,0,700,50);
g.fillRect(0,630,700,50);

g.fillOval(ballX,ballY,ballR, ballR);

}
}
public void keyPressed ( KeyEvent e ) {
int value = e.getKeyCode();

switch ( value ) {
case KeyEvent.VK_RIGHT: xpos += 50; break;
case KeyEvent.VK_LEFT: xpos -= 50; break;
case KeyEvent.VK_A: xpos2 -= 50; break;
case KeyEvent.VK_D: xpos2 += 50; break;
/*try to drop the ball with the space button
* case KeyEvent.VK_SPACE:
ballX+=25;
ballY+=25;
break;
case KeyEvent.VK_ENTER:
xpos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
ypos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
break;
*/
}
if( (xpos < 0 || xpos >= 700) || (xpos2 < 0 || xpos2 >= 700)){
if(xpos < 0 || xpos2 < 0){
if(xpos < 0) xpos = 0;
else if(xpos2 < 0) xpos2 = 0;
return;
}
else if(xpos >= 700 || xpos2 >= 700){
if(xpos >= 700)xpos = 550;
else if(xpos2 >= 700) xpos2=550;
return;
}
}

canvas.repaint ( );
}

}


I expect the output to have the ball drop down on to the blue rectangle with the start of the game/press of a button, but I can't get that to work.










share|improve this question
















I can't get around this problem. I've created this frame and panel with my desired output. Only thing is that I haven't been able to figure out was how to make the ball move "automatically". My ideal game would have the ball/circle dropping at the start of the game, perhaps with a click of a button or such. How would I go about doing this?



I've tried to move the ball with the E key, but that would be too inconvenient for the user, so I figured that having it move without an event handler would be a better choice.



    private int ballX, ballY, ballR;
private int score1, score2;
private JPanel panel;
private JFrame frame;
private DrawingArea canvas;
private int xpos, ypos,width,height;
private int xpos2, ypos2, width2, height2;
public Pong(){
xpos = 300;
ypos = 550;
width = 100;
height = 50;

xpos2 = 300;
ypos2 = 100;
width2 = 100;
height2 = 50;

ballR = 50;
ballX = 325;
ballY = 330;


}
public static void main(Stringargs){
Pong p = new Pong();
p.run();
}
public void run(){
frame = new JFrame("Pong");
frame.setSize(700,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas = new DrawingArea(); // create a panel to draw on
canvas.setBackground(Color.WHITE);
canvas.addFocusListener(this);
canvas.addKeyListener(this);
canvas.addMouseListener(this);


frame.getContentPane().add(canvas);
frame.setVisible(true);
}
class DrawingArea extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor ( Color.blue );
g.fillRect ( xpos, ypos, width, height );
g.setColor(Color.red);
g.fillRect(xpos2, ypos2, width2, height2);


g.setColor(Color.black);
g.fillRect(0,0,700,50);
g.fillRect(0,630,700,50);

g.fillOval(ballX,ballY,ballR, ballR);

}
}
public void keyPressed ( KeyEvent e ) {
int value = e.getKeyCode();

switch ( value ) {
case KeyEvent.VK_RIGHT: xpos += 50; break;
case KeyEvent.VK_LEFT: xpos -= 50; break;
case KeyEvent.VK_A: xpos2 -= 50; break;
case KeyEvent.VK_D: xpos2 += 50; break;
/*try to drop the ball with the space button
* case KeyEvent.VK_SPACE:
ballX+=25;
ballY+=25;
break;
case KeyEvent.VK_ENTER:
xpos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
ypos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
break;
*/
}
if( (xpos < 0 || xpos >= 700) || (xpos2 < 0 || xpos2 >= 700)){
if(xpos < 0 || xpos2 < 0){
if(xpos < 0) xpos = 0;
else if(xpos2 < 0) xpos2 = 0;
return;
}
else if(xpos >= 700 || xpos2 >= 700){
if(xpos >= 700)xpos = 550;
else if(xpos2 >= 700) xpos2=550;
return;
}
}

canvas.repaint ( );
}

}


I expect the output to have the ball drop down on to the blue rectangle with the start of the game/press of a button, but I can't get that to work.







java swing animation awt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 11:49









Andrew Thompson

153k28162342




153k28162342










asked Dec 31 '18 at 9:51









ManjuManju

61




61













  • Use a Swing Timer to update the position the ball appears, then call repaint(). For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

    – Andrew Thompson
    Dec 31 '18 at 11:51



















  • Use a Swing Timer to update the position the ball appears, then call repaint(). For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

    – Andrew Thompson
    Dec 31 '18 at 11:51

















Use a Swing Timer to update the position the ball appears, then call repaint(). For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

– Andrew Thompson
Dec 31 '18 at 11:51





Use a Swing Timer to update the position the ball appears, then call repaint(). For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.

– Andrew Thompson
Dec 31 '18 at 11:51












1 Answer
1






active

oldest

votes


















0














As you want to move the ball automatically, in run() method change position of the ball(Your xpos, ypos, xpos1 and ypos2) with some increment values(as you have done in keyPressed() interface method) dx, dy etc. and invoke repaint() method of your JFrame.



public class BouncingBall extends JPanel {

// Box height and width
int width;
int height;

// Ball Size
float radius = 40;
float diameter = radius * 2;

// Center of Call
float X = radius + 50;
float Y = radius + 20;

// Direction
float dx = 3;
float dy = 3;

public BouncingBall() {

Thread thread = new Thread() {
public void run() {
while (true) {

width = getWidth();
height = getHeight();

X = X + dx ;
Y = Y + dy;

if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}

if (Y - radius < 0) {
dy = -dy;
Y = radius;
} else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}
repaint();

try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}

}
}
};
thread.start();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
}

public static void main(String args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Bouncing Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setContentPane(new BouncingBall());
frame.setVisible(true);
}
}


As I have used render time of 50 ms in Thread.sleep(), you can specify your own time. So for me each 50 ms your JFrame gets updated.






share|improve this answer



















  • 1





    Don't use a Thread. Updates to Swing components should be done on the Event Dispatch Thread (EDT. Read the Swing tutorial on Concurrency for more information. Instead you should be using a Swing Timer to schedule the animation. Or you could use the SwingWorker (as described in the tutorial) and "publish" the new x/y location of the oval.

    – camickr
    Dec 31 '18 at 15:01











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986005%2fhow-do-i-make-it-so-the-circle-in-this-frame-actually-moves-automatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














As you want to move the ball automatically, in run() method change position of the ball(Your xpos, ypos, xpos1 and ypos2) with some increment values(as you have done in keyPressed() interface method) dx, dy etc. and invoke repaint() method of your JFrame.



public class BouncingBall extends JPanel {

// Box height and width
int width;
int height;

// Ball Size
float radius = 40;
float diameter = radius * 2;

// Center of Call
float X = radius + 50;
float Y = radius + 20;

// Direction
float dx = 3;
float dy = 3;

public BouncingBall() {

Thread thread = new Thread() {
public void run() {
while (true) {

width = getWidth();
height = getHeight();

X = X + dx ;
Y = Y + dy;

if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}

if (Y - radius < 0) {
dy = -dy;
Y = radius;
} else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}
repaint();

try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}

}
}
};
thread.start();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
}

public static void main(String args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Bouncing Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setContentPane(new BouncingBall());
frame.setVisible(true);
}
}


As I have used render time of 50 ms in Thread.sleep(), you can specify your own time. So for me each 50 ms your JFrame gets updated.






share|improve this answer



















  • 1





    Don't use a Thread. Updates to Swing components should be done on the Event Dispatch Thread (EDT. Read the Swing tutorial on Concurrency for more information. Instead you should be using a Swing Timer to schedule the animation. Or you could use the SwingWorker (as described in the tutorial) and "publish" the new x/y location of the oval.

    – camickr
    Dec 31 '18 at 15:01
















0














As you want to move the ball automatically, in run() method change position of the ball(Your xpos, ypos, xpos1 and ypos2) with some increment values(as you have done in keyPressed() interface method) dx, dy etc. and invoke repaint() method of your JFrame.



public class BouncingBall extends JPanel {

// Box height and width
int width;
int height;

// Ball Size
float radius = 40;
float diameter = radius * 2;

// Center of Call
float X = radius + 50;
float Y = radius + 20;

// Direction
float dx = 3;
float dy = 3;

public BouncingBall() {

Thread thread = new Thread() {
public void run() {
while (true) {

width = getWidth();
height = getHeight();

X = X + dx ;
Y = Y + dy;

if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}

if (Y - radius < 0) {
dy = -dy;
Y = radius;
} else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}
repaint();

try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}

}
}
};
thread.start();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
}

public static void main(String args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Bouncing Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setContentPane(new BouncingBall());
frame.setVisible(true);
}
}


As I have used render time of 50 ms in Thread.sleep(), you can specify your own time. So for me each 50 ms your JFrame gets updated.






share|improve this answer



















  • 1





    Don't use a Thread. Updates to Swing components should be done on the Event Dispatch Thread (EDT. Read the Swing tutorial on Concurrency for more information. Instead you should be using a Swing Timer to schedule the animation. Or you could use the SwingWorker (as described in the tutorial) and "publish" the new x/y location of the oval.

    – camickr
    Dec 31 '18 at 15:01














0












0








0







As you want to move the ball automatically, in run() method change position of the ball(Your xpos, ypos, xpos1 and ypos2) with some increment values(as you have done in keyPressed() interface method) dx, dy etc. and invoke repaint() method of your JFrame.



public class BouncingBall extends JPanel {

// Box height and width
int width;
int height;

// Ball Size
float radius = 40;
float diameter = radius * 2;

// Center of Call
float X = radius + 50;
float Y = radius + 20;

// Direction
float dx = 3;
float dy = 3;

public BouncingBall() {

Thread thread = new Thread() {
public void run() {
while (true) {

width = getWidth();
height = getHeight();

X = X + dx ;
Y = Y + dy;

if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}

if (Y - radius < 0) {
dy = -dy;
Y = radius;
} else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}
repaint();

try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}

}
}
};
thread.start();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
}

public static void main(String args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Bouncing Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setContentPane(new BouncingBall());
frame.setVisible(true);
}
}


As I have used render time of 50 ms in Thread.sleep(), you can specify your own time. So for me each 50 ms your JFrame gets updated.






share|improve this answer













As you want to move the ball automatically, in run() method change position of the ball(Your xpos, ypos, xpos1 and ypos2) with some increment values(as you have done in keyPressed() interface method) dx, dy etc. and invoke repaint() method of your JFrame.



public class BouncingBall extends JPanel {

// Box height and width
int width;
int height;

// Ball Size
float radius = 40;
float diameter = radius * 2;

// Center of Call
float X = radius + 50;
float Y = radius + 20;

// Direction
float dx = 3;
float dy = 3;

public BouncingBall() {

Thread thread = new Thread() {
public void run() {
while (true) {

width = getWidth();
height = getHeight();

X = X + dx ;
Y = Y + dy;

if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}

if (Y - radius < 0) {
dy = -dy;
Y = radius;
} else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}
repaint();

try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}

}
}
};
thread.start();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
}

public static void main(String args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Bouncing Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setContentPane(new BouncingBall());
frame.setVisible(true);
}
}


As I have used render time of 50 ms in Thread.sleep(), you can specify your own time. So for me each 50 ms your JFrame gets updated.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 31 '18 at 10:26









Shankar AcharyaShankar Acharya

111




111








  • 1





    Don't use a Thread. Updates to Swing components should be done on the Event Dispatch Thread (EDT. Read the Swing tutorial on Concurrency for more information. Instead you should be using a Swing Timer to schedule the animation. Or you could use the SwingWorker (as described in the tutorial) and "publish" the new x/y location of the oval.

    – camickr
    Dec 31 '18 at 15:01














  • 1





    Don't use a Thread. Updates to Swing components should be done on the Event Dispatch Thread (EDT. Read the Swing tutorial on Concurrency for more information. Instead you should be using a Swing Timer to schedule the animation. Or you could use the SwingWorker (as described in the tutorial) and "publish" the new x/y location of the oval.

    – camickr
    Dec 31 '18 at 15:01








1




1





Don't use a Thread. Updates to Swing components should be done on the Event Dispatch Thread (EDT. Read the Swing tutorial on Concurrency for more information. Instead you should be using a Swing Timer to schedule the animation. Or you could use the SwingWorker (as described in the tutorial) and "publish" the new x/y location of the oval.

– camickr
Dec 31 '18 at 15:01





Don't use a Thread. Updates to Swing components should be done on the Event Dispatch Thread (EDT. Read the Swing tutorial on Concurrency for more information. Instead you should be using a Swing Timer to schedule the animation. Or you could use the SwingWorker (as described in the tutorial) and "publish" the new x/y location of the oval.

– camickr
Dec 31 '18 at 15:01


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986005%2fhow-do-i-make-it-so-the-circle-in-this-frame-actually-moves-automatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas