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

  • SEARCH
  • Home
  • 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 8691673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:10:02+00:00 2026-06-13T00:10:02+00:00

I have an applet written using only AWT, that has a customizable bouncing ball

  • 0

I have an applet written using only AWT, that has a customizable bouncing ball moving inside it. All the buttons and scrollbars work if you use them and then press run. But, if you press run first, then the rest of the buttons will not respond. I’m posting the whole program, as I don’t know where the issue is or what I can cut out. I have this compiled in Jcreator and running inside of a 640*480 applet window in a basic HTML page (below the java code).

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Bounce extends Applet implements ActionListener, AdjustmentListener
{
    //runtime variables
    boolean running = false;
    boolean currentlyCircle = true;
    boolean showtails = false;

    //buttons
    Button runbutton = new Button("Run");
    Button tailbutton = new Button("Tail");
    Button shapebutton = new Button("Square");
    Button clearbutton = new Button("Clear");
    Button quitbutton = new Button("Quit");

    //text
    Label speedlabel = new Label("Speed");
    Label sizelabel = new Label("Size");

    //scrollbars
    private final int barHeight = 20;
    private final int SLIDER_WIDTH = 10;
    private final int MAXSPEED = 110;
    private final int MINSPEED = 0;
    private final int MAX_SIZE = 110;
    private final int MIN_SIZE = 10;
    Scrollbar speedbar = new Scrollbar(Scrollbar.HORIZONTAL, MAXSPEED/2, SLIDER_WIDTH, MINSPEED, MAXSPEED);
    Scrollbar sizebar = new Scrollbar(Scrollbar.HORIZONTAL, MAX_SIZE/2, SLIDER_WIDTH, MIN_SIZE, MAX_SIZE);

    //drawn objs
    int size = 50;  
    private Graphics obj;
    Point currentlocation = new Point(100,100);
    Point previouslocation;
    Point nextlocation;

    //boundaries
    int bound_x;
    int bound_y;

    //directions
    int dx = 1; //1 = left, -1 = right
    int dy = 1; //1 = up, -1 = down

    //speed
    int speed = speedbar.getValue();

    //initialize the applet and draw everything
    public void init()
    {
        Dimension appSize = this.getSize();
        bound_x = appSize.width - 100;
        bound_y = appSize.height - 232;
        double colWeight[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//15 cols
        double rowWeight[] = {1,1,1,1,1,1,1,1,1,1}; //10 rows
        int colWidth[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//15 cols
        int rowHeight[] = {1,1,1,1,1,1,1,1,1,1}; //10 rows
        GridBagConstraints c = new GridBagConstraints();
        GridBagLayout gbl = new GridBagLayout();
        gbl.rowHeights = rowHeight;
        gbl.rowWeights = rowWeight;
        gbl.columnWeights = colWeight;
        gbl.columnWidths = colWidth;
        c.anchor = GridBagConstraints.CENTER;

        setBounds(0,0,480,640);
        setLayout(new BorderLayout());
        Panel p = new Panel();
        p.setLayout(gbl);

        //speed scrollbar
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.gridx = 1;
        c.gridy = 7;
        c.fill= GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(this.speedbar,c);

        //run button
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.gridx = 5;
        c.gridy = 7;
        c.fill= GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(this.runbutton,c);

        //tail button
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.gridx = 8;
        c.gridy = 7;
        c.fill= GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(this.tailbutton,c);

        //size scrollbar
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.gridx = 11;
        c.gridy = 7;
        c.fill= GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(this.sizebar,c);

        //speed text label
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.gridx = 1;
        c.gridy = 8;
        c.fill= GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(this.speedlabel,c);

        //shape button
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.gridx = 5;
        c.gridy = 8;
        c.fill= GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(this.shapebutton,c);

        //clear button
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.gridx = 8;
        c.gridy = 8;
        c.fill= GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(this.clearbutton,c);

        //size text label
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.gridx = 11;
        c.gridy = 8;
        c.fill= GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(this.sizelabel,c);

        //quit button
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.gridx = 6;
        c.gridy = 9;
        c.fill= GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(this.quitbutton,c);

        //add to the screen
        p.add(this.speedbar);
        p.add(this.runbutton);
        p.add(this.tailbutton);
        p.add(this.sizebar);
        p.add(this.speedlabel);
        p.add(this.shapebutton);
        p.add(this.clearbutton);
        p.add(this.sizelabel);
        p.add(this.quitbutton);

        //add listners
        speedbar.addAdjustmentListener(this);
        runbutton.addActionListener(this);
        tailbutton.addActionListener(this);
        sizebar.addAdjustmentListener(this);
        shapebutton.addActionListener(this);
        clearbutton.addActionListener(this);
        quitbutton.addActionListener(this);

        //drawing paramaters, draw the first object
        obj = getGraphics();
        obj.drawOval(100, 100, size, size);
        obj.fillOval(100, 100, size, size);
        nextlocation = new Point(currentlocation.x+dx, currentlocation.y+dy);

        //add the panels
        add("South", p);
    }

    public void run()
    {
        while (running)
        {
            if (!showtails)
            {
                obj.setColor(Color.white);
                obj.clearRect(currentlocation.x,currentlocation.y,size+5,size+5);
            }
            update();
            draw(obj);
            pause();
            move();
        }
    }

    public void update()
    {
        previouslocation = new Point(currentlocation);
        currentlocation = nextlocation;
    }

    //draw the dot an the next location
    public void draw(Graphics obj)
    {
        obj.setColor(Color.black);
        if(currentlyCircle)
        {
            obj.drawOval(nextlocation.x, nextlocation.y, size, size);
            obj.fillOval(nextlocation.x, nextlocation.y, size, size);
        }
        else
        {
            obj.drawRect(nextlocation.x, nextlocation.y, size, size);
            obj.fillRect(nextlocation.x, nextlocation.y, size, size);
        }
    }

    //wait a period of time, depending on the speed.
    public void pause()
    {
        long waittime = MAXSPEED - speedbar.getValue();
        for (long i = 0; i < waittime*100000L; i++) { continue; }
    }

    public void move()
    {
        nextlocation = new Point(currentlocation.x+dx, currentlocation.y+dy);
        //if it will hit the right or left, flip the x direction and set it 
        if (nextlocation.x >= bound_x || nextlocation.x <= 0)
        { dx *= -1; }
        nextlocation.x += dx;
        //if it will hit the top or bottom, flip the y direction and set it
        if (nextlocation.y >= bound_y + 100 || nextlocation.y <= 0)
        { dy *= -1; }
        nextlocation.y += dy;
    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == this.runbutton)
    {
        running = true;
        run();
    }
        else if (source == this.shapebutton)
        {
            if (currentlyCircle) //if circle, draw it as a rectangle
            {
                this.shapebutton.setLabel("Square");
                currentlyCircle = false;
            }
            else //if rectangle, draw it as a circle
            {
                this.shapebutton.setLabel("Circle");
                currentlyCircle = true;
            }
        }
        else if (source == this.clearbutton)
        {
            //clear tail off the screen
            obj.clearRect(0,0,bound_x,bound_y);
        }
        else if (source == this.tailbutton)
        {
            //toggle showing tails. This is used in run
            showtails = true;
        }
        else if (source == quitbutton)
        {
            //remove listeners
            stop();
        }
    }

    public void adjustmentValueChanged(AdjustmentEvent e)
    {
        Object source = e.getSource();
        //set the new size. If the size is too big its adjusted in draw
        if (source == sizebar)
        {
            size = sizebar.getValue();
        }
        if (source == speedbar)
        {
            speed = speedbar.getValue();
        }
    }

    public void stop()
    {
        this.speedbar.removeAdjustmentListener(this);
        this.runbutton.removeActionListener(this);
        this.tailbutton.removeActionListener(this);
        this.sizebar.removeAdjustmentListener(this);
        this.shapebutton.removeActionListener(this);
        this.clearbutton.removeActionListener(this);
        this.quitbutton.removeActionListener(this);
    }
}

HTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <title>Bounce/title>
   <style type="text/css">
      body { color: black; background: grey }
   </style>
</head>
<body>
  <div align="center">
   <applet code=Bounce.class width=640 height=480 ></applet>
  </div>
</body>
</html>
  • 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-13T00:10:03+00:00Added an answer on June 13, 2026 at 12:10 am

    The run method is your problem. Once this method is executed, you block the Event Dispatching Thread (EDT) from dispatching events, including the all important repaint event…

    public void run()
    {
        while (running)
        {
            if (!showtails)
            {
                obj.setColor(Color.white);
                obj.clearRect(currentlocation.x,currentlocation.y,size+5,size+5);
            }
            update();
            draw(obj);
            pause();
            move();
        }
    }
    

    You must NEVER block the EDT.

    I would have a read through

    • Event Dispatching Thread
    • Concurrency in Swing

    Normally, I would suggest the use of a SwingWorker, but in your case I don’t think it would fit the model, instead, you’re going to have to use a Thread and Runnable.

    This is going to complicate matters for you, as you should NEVER, EVER update any UI component from any thread other the EDT. For this, you’re going to have to use SwingUtilities#invokeLater or SwingUtilities#invokeAndWait

    UPDATED

    Here’s a little example I put together for another question

    public class TestApplet02 extends Applet implements KeyListener, Runnable {
    
        Button options = new Button("Options");
        Thread thread = new Thread(this);
        int y = 0;
    
        public void init() {
            thread.start();
        }
    
        @Override
        public void start() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    setLayout(new BorderLayout());
                    addKeyListener(TestApplet02.this);
                }
            });
        }
    
        public void paint(Graphics graphics) {
            super.paint(graphics);
            Graphics2D g2d = (Graphics2D) graphics;
            y++;
            if (y > getHeight()) {
                y = 0;
            }
            g2d.drawLine(0, y, getWidth(), y);
        }
    
        public void run() {
            try {
                while (true) {
                    thread.sleep(40);
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            repaint();
                        }
                    });
                }
            } catch (InterruptedException exception) {
            }
        }
    
        public void keyPressed(KeyEvent keyEvent) {
            switch (keyEvent.getKeyCode()) {
                case KeyEvent.VK_ESCAPE:
                    // pause game
                    add(options);
                    invalidate();
                    revalidate();
            }
        }
    
        public void keyReleased(KeyEvent keyEvent) {
        }
    
        public void keyTyped(KeyEvent keyEvent) {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an applet that communicates with a servlet using Http (Not sockets). Currently,
i have written a snake program using javascript.. the problem is that the snake
I have written an applet that should compare files. It calculates a CRC32 checksum
I have written a small app using Ojective-C w/ XCode. It's only for personal
I have developed one web application that runs under all browser via applet (core
I have a video that's written using AVAssetWriter w/ kCVPixelFormatType_32ARGB and the AVVideoCodecH264 codec.
I have written a java applet which opens a JFrame (so when run in
I have an applet which runs in a browser. I expect that when the
I have an applet that is communicating with a servlet. I am communicating with
I have an applet that throws this exception when trying to communicate with the

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.