Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9032419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:58:42+00:00 2026-06-16T07:58:42+00:00

I made the applet Bouncing Ball and in the class Ball.java I made inner

  • 0

I made the applet Bouncing Ball and in the class Ball.java I made inner class TimerListener with method repaint(), and when I run the applet, instead of repaint the ball, java paint the ball again and again(not delete, and then paint).

here is my code for the class Ball.java

import javax.swing.Timer;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Ball extends JPanel {
private int delay = 10;

Timer timer=new Timer(delay, new TimerListener());

private int x=0;
private int y=0;
private int dx=20;
private int dy=20;
private int radius=5;

private class TimerListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        repaint();
    }
}

public void paintComponent(Graphics g){

    g.setColor(Color.red);
    if(x<radius) dx=Math.abs(dx);
    if(x>(getWidth()-radius)) dx=-Math.abs(dx);
    if(y>(getHeight()-radius)) dy=-Math.abs(dy);
    if(y<radius) dy=Math.abs(dy);
    x+=dx;
    y+=dy;
    g.fillOval(x-radius, y-radius, radius*2, radius*2);
        }
public void suspend(){
    timer.stop();
}
public void resume(){
    timer.start();
}
public void setDelay(int delay){
this.delay=delay;
timer.setDelay(delay);
}   
}

here is my code for class BallControl.java

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class BallControl extends JPanel{

private Ball ball = new Ball();
private JButton jbtSuspend = new JButton("Suspend");
private JButton jbtResume = new JButton("Resume");
private JScrollBar jsbDelay = new JScrollBar();

public BallControl(){
    JPanel panel = new JPanel();
    panel.add(jbtSuspend);
    panel.add(jbtResume);
    //ball.setBorder(new javax.swing.border.LineBorder(Color.red));
    jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
    ball.setDelay(jsbDelay.getMaximum());
    setLayout(new BorderLayout());
    add(jsbDelay, BorderLayout.NORTH);
    add(ball, BorderLayout.CENTER);
    add(panel, BorderLayout.SOUTH);

    jbtSuspend.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ball.suspend();
        }
    });

    jbtResume.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ball.resume();
        }
    });

    jsbDelay.addAdjustmentListener(new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            ball.setDelay(jsbDelay.getMaximum() - e.getValue());
        }
    });

}
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:58:43+00:00Added an answer on June 16, 2026 at 7:58 am

    Isn’t the Timer supposed to also change the Ball object’s position? In other words, isn’t it supposed to change its x and y values? i.e.,

    private class TimerListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
    
            // first change x and y here *****
    
            repaint();
        }
    }
    

    Else, how is the ball supposed to move much less bounce?

    You seem to have this change position code in your paintComponent(...) method, and that is not good since you don’t have full control over when or even if this method gets called. For this reason, program logic and code that changes this object’s state does not belong inside of that method.

    Also, your paintComponent(...) method override needs a call to to the super’s paintComponent(...) method on its first line so that the old ball can be erased before a new ball is drawn.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Java applet class and made a small HTML page to
I have created a java applet (.class file) and made a .jar with it
I have made a java applet/application game that should save levels that the user
I have made a Java Applet that depends on some third party libriaries as
I have two class fils upload.java and transferProgress.java . upload.java makes applet GUI and
So I made an Applet (not JApplet) and proceeded to upload it to my
I'm trying to replicate my code originally made extending the Applet class. but the
I've made a game in Java which works without any problem when I run
When running a java applet that I made (a basic little number-guessing game), whenever
I've made a java applet in Eclipse, it worked in Eclipse but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.