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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:34:38+00:00 2026-05-31T20:34:38+00:00

The Problem is when I resize the JFrame the animation goes on with it’s

  • 0

The Problem is when I resize the JFrame the animation goes on with it’s Pre supplied dimensions for JComponent . Is there a way that I can update my width and height variables as I resize the JFrame, so that the Animation can run along with the new Co-ordinates.

In simpler terms, say the JComponent has initial width = 300 and height = 300, so the BALL moves inside these specified Co-ordinates. Now if I resize my JFrame, the size for the JComponent still remains as is i.e. width = 300 and height = 300, but what I was hoping for, is a way to modify these variables with the present size of the window. Do let me know if I lack something in explaining my issue.

Here is the code that I am using :

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

public class BallAnimation
{
    private int x;
    private int y;
    private int count;
    private int width;
    private int height;
    private int speedValue;
    private boolean flag;
    /*
     * These variables are used to keep track of 
     * the ball, either it is going LEFT or RIGHT
     * depending on that, we will set the 
     * Co-ordinates.
     */
    private boolean toLeft, toRight;

    private boolean fromTop, fromBottom;

    private Timer timer;

    private JButton button;

    private ActionListener actionTimer; 
    private ActionListener buttonAction;

    public BallAnimation()
    {
        x = y = count = 0;
        flag = toLeft = false;
        toRight = true;
        fromTop = true;
        fromBottom = false;
        speedValue = 5;
    }

    public static void main(String args[])
    {
        Runnable runnable = new Runnable()
        {
            public void run()
            {
                BallAnimation animation = new BallAnimation();
                animation.go();
            }
        };      
        SwingUtilities.invokeLater(runnable);
    }

    public void go()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //JPanel contentPane = new JPanel();

        /*
         * Class Name : 
         * Java Naming Convention says that class names 
         * should be in Pascal Case, i.e. the first
         * letter of the class name should be capitalized
         * and every new word must start with a capitalized 
         * Alphabet.
         * For Example : 
         * public class ClassName{...}
         * ----------------------------------------------------------
         * Variable Name : 
         * Java Naming Convention says that the variable name
         * should be in Camel Case, i.e. the first letter of 
         * the variable name should be small case or _ (underscore)
         * and every new word must start with a capitalized
         * Alphabet.
         * ---------------------------------------------------------
         */
        final MyDraw drawPanel = new MyDraw(0, 0);
        x = drawPanel.getXValue();
        y = drawPanel.getYValue();
        //contentPane.add(drawPanel);

        actionTimer = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {               
                if (fromTop && !fromBottom && x < width && y < height 
                            && toRight && !toLeft)
                {
                    x += speedValue;
                    y += speedValue;
                }
                else if (fromTop && !fromBottom && x < width && y >= height
                                 && toRight && !toLeft)
                {
                    /*
                     * Since the ball coming from the TOP LEFT Side
                     * touched the BOTTOM of the JPanel.
                     */
                    y -= speedValue;
                    x += speedValue;
                    fromTop = false;
                    fromBottom = true;
                }
                else if (!fromTop && fromBottom && x < width && y <= 0
                                  && toRight && !toLeft)
                {
                    /*
                     * Since the ball coming from BOTTOM LEFT Side
                     * touched the TOP of the JPanel. 
                     */
                    fromTop = true;
                    fromBottom = false;
                    x += speedValue;
                    y += speedValue;
                }
                else if (!fromTop && fromBottom && x < width && y < height
                                  && toRight && !toLeft)
                {
                    x += speedValue;
                    y -= speedValue;
                }
                else if (!fromTop && fromBottom && x >= width && y < height
                                  && toRight && !toLeft)
                {
                    /*
                     * Since the ball coming from the BOTTOM LEFT Side
                     * touched the RIGHT Side of the JPanel.
                     */
                    toRight = false;
                    toLeft = true;
                    x -= speedValue;
                    y -= speedValue;
                }                
                else if (!fromTop && fromBottom && x < width && y <= 0
                                  && !toRight && toLeft)
                {
                    /*
                     * Since the ball coming from the BOTTOM RIGHT Side
                     * touched the Top Side of the JPanel.
                     */
                    fromTop = true;
                    fromBottom = false;
                    x -= speedValue;
                    y += speedValue;
                }
                else if (fromTop && !fromBottom && x <= 0 && y < height
                                 && !toRight && toLeft)
                {
                    /*
                     * Since the ball coming from the TOP RIGHT Side
                     * touched the LEFT Side of the JPanel.
                     */
                    toRight = true;
                    toLeft = false;
                    x += speedValue;
                    y += speedValue;
                }
                else if (fromTop && !fromBottom && x >= width && y < height
                                  && toRight && !toLeft)
                {
                    /*
                     * Since the ball coming from the TOP LEFT Side
                     * touched the RIGHT Side of the JPanel
                     */
                    toRight = false;
                    toLeft = true;
                    x -= speedValue;
                    y += speedValue;
                }
                else if (fromTop && !fromBottom && x < width && y < height
                                  && !toRight && toLeft)
                {
                    x -= speedValue;
                    y += speedValue;
                }
                else if (!fromTop && fromBottom && x <= 0 && y < height
                                  && !toRight && toLeft)
                {
                    /*
                     * Since the ball coming from the BOTTOM RIGHT Side
                     * touched the LEFT Side of the JPanel.
                     */
                    toRight = true;
                    toLeft = false;
                    x += speedValue;
                    y -= speedValue;
                }
                else if (!fromTop && fromBottom && x < width && y < height
                                  && !toRight && toLeft)
                {
                    x -= speedValue;
                    y -= speedValue;
                }
                else if (fromTop && !fromBottom && x < width && y >= height
                                  && !toRight && toLeft)
                {
                    /*
                     * Since the ball coming from the TOP RIGHT Side
                     * touched the BOTTOM Side of the JPanel.
                     */
                    fromTop = false;
                    fromBottom = true;
                    x -= speedValue;
                    y -= speedValue;
                }
                System.out.println("X : " + x);
                System.out.println("Y : " + y);
                System.out.println("Direction is LEFT : " + toLeft);
                System.out.println("Direction is RIGHT : " + toRight);
                System.out.println("Coming from TOP : " + fromTop);
                System.out.println("Coming from BOTTOM : " + fromBottom);
                drawPanel.setXYValues(x, y);
            }
        };

        buttonAction = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (!flag)
                {
                    timer.start();
                    button.setText("STOP ANIMATION");
                    flag = true;
                }
                else if (flag)
                {
                    timer.stop();
                    button.setText("START ANIMATION");
                    flag = false;
                }
            }
        };

        button = new JButton("START ANIMATION");
        button.addActionListener(buttonAction);

        frame.getContentPane().add(drawPanel, BorderLayout.CENTER);
        frame.getContentPane().add(button, BorderLayout.PAGE_END);
        frame.setSize(300, 300);
        //frame.pack();
        frame.setVisible(true);        

        timer = new Timer(40, actionTimer);
        width = drawPanel.getWidth() - 30;
        System.out.println("WIDTH : " + width);
        height = drawPanel.getHeight() - 30;    
        System.out.println("HEIGHT : " + height);
    }
    class MyDraw extends JComponent
    {
        private int x;
        private int y;
        private Timer timer;

        public MyDraw(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public int getXValue()
        {
            return x;
        }

        public int getYValue()
        {
            return y;
        }

        public void setXYValues(int x, int y)
        {
            this.x = x;
            this.y = y;
            repaint();
        }

        public Dimension getPreferredSize()
        {
            return (new Dimension(300, 300));
        }

        public void paintComponent(Graphics g)
        {
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.BLUE);
            g.fillOval(x, y, 40, 40);
        }
    }
}
  • 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-05-31T20:34:39+00:00Added an answer on May 31, 2026 at 8:34 pm

    Now that is a whole heapin’ helping of code! Try this variant (breaks one or two things, but fixes the main problem). The fix is to base the width/height on the current size of the component.

    package test;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class BallAnimation
    {
        private int x;
        private int y;
        private int count;
        //private int drawPanel.getWidth();
        //private int drawPanel.getHeight();
        private int speedValue;
        private boolean flag;
        /*
         * These variables are used to keep track of 
         * the ball, either it is going LEFT or RIGHT
         * depending on that, we will set the 
         * Co-ordinates.
         */
        private boolean toLeft, toRight;
    
        private boolean fromTop, fromBottom;
    
        private Timer timer;
    
        private JButton button;
    
        private ActionListener actionTimer; 
        private ActionListener buttonAction;
    
        MyDraw drawPanel;
    
        public BallAnimation()
        {
            x = y = count = 0;
            flag = toLeft = false;
            toRight = true;
            fromTop = true;
            fromBottom = false;
            speedValue = 5;
        }
    
        public static void main(String args[])
        {
            Runnable runnable = new Runnable()
            {
                public void run()
                {
                    BallAnimation animation = new BallAnimation();
                    animation.go();
                }
            };      
            SwingUtilities.invokeLater(runnable);
        }
    
        public void go()
        {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //JPanel contentPane = new JPanel();
    
            /*
             * Class Name : 
             * Java Naming Convention says that class names 
             * should be in Pascal Case, i.e. the first
             * letter of the class name should be capitalized
             * and every new word must start with a capitalized 
             * Alphabet.
             * For Example : 
             * public class ClassName{...}
             * ----------------------------------------------------------
             * Variable Name : 
             * Java Naming Convention says that the variable name
             * should be in Camel Case, i.e. the first letter of 
             * the variable name should be small case or _ (underscore)
             * and every new word must start with a capitalized
             * Alphabet.
             * ---------------------------------------------------------
             */
            drawPanel = new MyDraw(0, 0);
            x = drawPanel.getXValue();
            y = drawPanel.getYValue();
            //contentPane.add(drawPanel);
    
            actionTimer = new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {               
                    if (fromTop && !fromBottom && x < drawPanel.getWidth() && y < drawPanel.getHeight() 
                                && toRight && !toLeft)
                    {
                        x += speedValue;
                        y += speedValue;
                    }
                    else if (fromTop && !fromBottom && x < drawPanel.getWidth() && y >= drawPanel.getHeight()
                                     && toRight && !toLeft)
                    {
                        /*
                         * Since the ball coming from the TOP LEFT Side
                         * touched the BOTTOM of the JPanel.
                         */
                        y -= speedValue;
                        x += speedValue;
                        fromTop = false;
                        fromBottom = true;
                    }
                    else if (!fromTop && fromBottom && x < drawPanel.getWidth() && y <= 0
                                      && toRight && !toLeft)
                    {
                        /*
                         * Since the ball coming from BOTTOM LEFT Side
                         * touched the TOP of the JPanel. 
                         */
                        fromTop = true;
                        fromBottom = false;
                        x += speedValue;
                        y += speedValue;
                    }
                    else if (!fromTop && fromBottom && x < drawPanel.getWidth() && y < drawPanel.getHeight()
                                      && toRight && !toLeft)
                    {
                        x += speedValue;
                        y -= speedValue;
                    }
                    else if (!fromTop && fromBottom && x >= drawPanel.getWidth() && y < drawPanel.getHeight()
                                      && toRight && !toLeft)
                    {
                        /*
                         * Since the ball coming from the BOTTOM LEFT Side
                         * touched the RIGHT Side of the JPanel.
                         */
                        toRight = false;
                        toLeft = true;
                        x -= speedValue;
                        y -= speedValue;
                    }                
                    else if (!fromTop && fromBottom && x < drawPanel.getWidth() && y <= 0
                                      && !toRight && toLeft)
                    {
                        /*
                         * Since the ball coming from the BOTTOM RIGHT Side
                         * touched the Top Side of the JPanel.
                         */
                        fromTop = true;
                        fromBottom = false;
                        x -= speedValue;
                        y += speedValue;
                    }
                    else if (fromTop && !fromBottom && x <= 0 && y < drawPanel.getHeight()
                                     && !toRight && toLeft)
                    {
                        /*
                         * Since the ball coming from the TOP RIGHT Side
                         * touched the LEFT Side of the JPanel.
                         */
                        toRight = true;
                        toLeft = false;
                        x += speedValue;
                        y += speedValue;
                    }
                    else if (fromTop && !fromBottom && x >= drawPanel.getWidth() && y < drawPanel.getHeight()
                                      && toRight && !toLeft)
                    {
                        /*
                         * Since the ball coming from the TOP LEFT Side
                         * touched the RIGHT Side of the JPanel
                         */
                        toRight = false;
                        toLeft = true;
                        x -= speedValue;
                        y += speedValue;
                    }
                    else if (fromTop && !fromBottom && x < drawPanel.getWidth() && y < drawPanel.getHeight()
                                      && !toRight && toLeft)
                    {
                        x -= speedValue;
                        y += speedValue;
                    }
                    else if (!fromTop && fromBottom && x <= 0 && y < drawPanel.getHeight()
                                      && !toRight && toLeft)
                    {
                        /*
                         * Since the ball coming from the BOTTOM RIGHT Side
                         * touched the LEFT Side of the JPanel.
                         */
                        toRight = true;
                        toLeft = false;
                        x += speedValue;
                        y -= speedValue;
                    }
                    else if (!fromTop && fromBottom && x < drawPanel.getWidth() && y < drawPanel.getHeight()
                                      && !toRight && toLeft)
                    {
                        x -= speedValue;
                        y -= speedValue;
                    }
                    else if (fromTop && !fromBottom && x < drawPanel.getWidth() && y >= drawPanel.getHeight()
                                      && !toRight && toLeft)
                    {
                        /*
                         * Since the ball coming from the TOP RIGHT Side
                         * touched the BOTTOM Side of the JPanel.
                         */
                        fromTop = false;
                        fromBottom = true;
                        x -= speedValue;
                        y -= speedValue;
                    }
                    System.out.println("X : " + x);
                    System.out.println("Y : " + y);
                    System.out.println("Direction is LEFT : " + toLeft);
                    System.out.println("Direction is RIGHT : " + toRight);
                    System.out.println("Coming from TOP : " + fromTop);
                    System.out.println("Coming from BOTTOM : " + fromBottom);
                    drawPanel.setXYValues(x, y);
                }
            };
    
            buttonAction = new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    if (!flag)
                    {
                        timer.start();
                        button.setText("STOP ANIMATION");
                        flag = true;
                    }
                    else if (flag)
                    {
                        timer.stop();
                        button.setText("START ANIMATION");
                        flag = false;
                    }
                }
            };
    
            button = new JButton("START ANIMATION");
            button.addActionListener(buttonAction);
    
            frame.getContentPane().add(drawPanel, BorderLayout.CENTER);
            frame.getContentPane().add(button, BorderLayout.PAGE_END);
            frame.setSize(300, 300);
            //frame.pack();
            frame.setVisible(true);        
    
            timer = new Timer(40, actionTimer);
            System.out.println("WIDTH : " + drawPanel.getWidth());
            System.out.println("HEIGHT : " + drawPanel.getHeight());
        }
        class MyDraw extends JComponent
        {
            private int x;
            private int y;
            private Timer timer;
    
            public MyDraw(int x, int y)
            {
                this.x = x;
                this.y = y;
            }
    
            public int getXValue()
            {
                return x;
            }
    
            public int getYValue()
            {
                return y;
            }
    
            public void setXYValues(int x, int y)
            {
                this.x = x;
                this.y = y;
                repaint();
            }
    
            public Dimension getPreferredSize()
            {
                return (new Dimension(300, 300));
            }
    
            public void paintComponent(Graphics g)
            {
                g.setColor(Color.WHITE);
                g.fillRect(0, 0, getWidth(), getHeight());
                g.setColor(Color.BLUE);
                g.fillOval(x, y, 40, 40);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem: Ajax suggest-search on [ n ] ingredients in recipes. That is: match recipes
Problem: We have a web app that calls some web services asynchronously (from the
I'm having a problem: I want to resize and watermark an image in CodeIgniter.
When I resize JFrame, the table (or more specifically the scrollPane) squishes into a
I have a UIImageView that can be resized by pinching in and out (Example:
I have problem with the background div, when I resize the window too much,
I have a problem when I try to resize a JLabel. In my application
I have face resize problem of listview columns. If you anchor/docking the listview to

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.