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 8481417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:33:14+00:00 2026-06-10T19:33:14+00:00

Question’s all in the title :). I don’t know what’s wrong with my code

  • 0

Question’s all in the title :). I don’t know what’s wrong with my code and why it won’t draw the circle onto the Japplet. Can u help me?

Here’s my code:

import javax.swing.*;
import java.awt.Graphics;
import java.awt.Event;

public class BouncingBall extends JApplet {
    private static final long serialVersionUID = 1L;
    boolean b = true;
    long speed = 50;
    int pos = 250;

    public void init(){
        setSize(500,500);
    }
    public boolean mouseDown(Event e, int x, int y)
    {


        if(y>250)
        {
            speed = speed - 10;
        }
        else
        {
            speed = speed + 10;
        }

        repaint();
        return true;
    }
    public void paintComponents(Graphics g)
    {
        g.drawOval(250,pos,100,100);
        if(speed <= 20)
        {
            speed++;
            repaint();
        }
        try
        {
            Thread.sleep(speed);
        }
        catch(InterruptedException e){e.printStackTrace();}
        if(pos>=400)
        {
            b = false;
        }
        if(pos<=100)
        {
            b = true;
        }
        if(b==true)
        {
            pos = pos +5;
        }
        else
        {
            pos = pos -5;
        }
        repaint();
    }
}

Imulsion

  • 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-10T19:33:16+00:00Added an answer on June 10, 2026 at 7:33 pm

    While I prepare my response, please have a read through

    • Creating a GUI with Swing
    • Graphics2D
    • Painting in Swing
    • Writing Event Listeners
    • The Event Dispatching Thread
    • Concurrency in Swing
    • Thread and Swing

    Okay. About the only thing you did right, was extend from JApplet

    Your “paint” method is a complete mess…

    public void paintComponents(Graphics g) {
       // Where's the super call???  All paint methods have a super
       // if you don't call it, expect really bad things to happen...
       if(speed <= 20)
        {
            speed++;
            // Don't do this
            repaint();
        }
        try
        {
            // NEVER, EVER do this, EVER
            Thread.sleep(speed);
        }
        catch(InterruptedException e){e.printStackTrace();}
    
        // These choices should be made else where.
        if(pos>=400)
        {
            b = false;
        }
        if(pos<=100)
        {
            b = true;
        }
        if(b==true)
        {
            pos = pos +5;
        }
        else
        {
            pos = pos -5;
        }
        // NEVER DO THIS IN A PAINT METHOD...
        repaint();
    

    As has already been pointed out, don’t use mouseDown method, use a MouseListener instead

    As has already been pointed out, don’t paint on to the top level containers (JApplet or any type of window or frame), use a custom component instead.

    public class BouncingBall extends JApplet {
    
        private static final long serialVersionUID = 1L;
    
        public void init() {
            setSize(500, 500);
            setLayout(new BorderLayout());
            add(new BouncyPane());
        }
    
        protected class BouncyPane extends JPanel {
    
            private boolean b = true;
            private int speed = 50;
            private int pos = 250;
            private Timer timer;
            private int amount = 10;
    
            public BouncyPane() {
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
    
                        if (speed > 250) {
                            amount = -10;
                        } else if (speed <= 0) {
                            amount = 10;
                        }
    
                        speed += amount;
                        timer.stop();
                        timer.setDelay(speed);
                        timer.restart();
    
                        repaint();
                    }
                });
    
                timer = new Timer(speed, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (pos >= 400) {
                            b = false;
                        }
                        if (pos <= 100) {
                            b = true;
                        }
                        if (b == true) {
                            pos = pos + 5;
                        } else {
                            pos = pos - 5;
                        }
    
                        repaint();
    
                    }
                });
    
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
                g.setColor(Color.RED);
                g.drawOval(250, pos, 100, 100);
            }
        }
    }
    

    Please, make an effort to read through all the above links, they will highlight the problem areas in you code

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

Sidebar

Related Questions

Question: Can someone help me fix the following piece of code to meet the
Question is already been asked in title. Here is a code: (function($){ var filter
Question: I receive the following error for the code below, does anyone know why?
Question is not correctly framed i know, how do i code the below logic
Question: Given a Font object, how do I draw all the glyphs/symbols/characters of the
Question: How can I give a new user almost all privileges, but still keep
Question in the title. And what happens when all 3 of $_GET[foo] , $_POST[foo]
QUESTION: What am I missing or doing wrong? I'm trying to migrate fully functional
Question in title pretty much sums it up. I have some resource object defined
Question for the video tag, the following code works for every browsers expect opera

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.