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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:22:45+00:00 2026-05-31T21:22:45+00:00

JRE Version 1.7 Update 3 EXPECTED BEHAVIOUR As I run the program, it works

  • 0

JRE Version 1.7 Update 3

EXPECTED BEHAVIOUR

As I run the program, it works as expected, everything works smoothly. As when I click on STOP JButton the animation stops and the text on the same JButton changes to START. Now when i click on BALL COLOUR JButton, the colour of the BALL changes, as well as the colour of the BALL COLOUR JBUTTON, also changes, to that of the BALL. This whole behaviour works if I run my application as is without resizing.

UNEXPECTED BEHAVIOUR

But when i RESIZE my JFrame, by pulling the Right Side, that’s when unexpected behaviour of my Application is shown, in the sense that if I press STOP JButton and then click on BALL COLOUR button, the text on the JButton clicked earlier whose text changed to START will change to STOP again when it should not be, as well as the colour of the BALL COLOUR JButton will remain unchanged or will turn to BLUE, when it should be changed to the colour of the ball. I am attaching the pics for more info. But if you will try to resize it back to it’s original size or closer to that, then things will come back to normal. Why is this happening ? Any idea or clue will be much appreciated.

As My Application Runs with EXPECTED BEHAVIOUR as described above :

Expected Behaviour

And here the UNEXPECTED BEHAVIOUR

Unexpected Behaviour

BOTTOM-LINE :

Why the Application runs as usual as it should be, at the BEGINNING , but not when RESIZED by dragging it’s RIGHT SIDE, but again if you bring it to it’s original size or closer to it, things come back to normal, it works as expected ?

So considering the scenario, am I doing something wrong, in the program. Or is this exactly the situation, where I should be using the SwingWorker, Or Is this an issue with the Layout, or something hidden related to Content Pane. Please do put some light 🙂

here is the code I am using, I had brought it down to the minimum, as I think to demonstrate my problem :

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class BallAnimation
{
    private int x;
    private int y;
    private boolean positiveX;
    private boolean positiveY;
    private boolean isTimerRunning; 
    private int speedValue;
    private int diameter; 
    private DrawingArea drawingArea;    
    private Timer timer;
    private int colourCounter;
     Color[] colours = {
                            Color.BLUE.darker(),
                            Color.MAGENTA.darker(),
                            Color.BLACK.darker(),
                            Color.RED.darker(),
                            Color.PINK.darker(),
                            Color.CYAN.darker(),
                            Color.DARK_GRAY.darker(),
                            Color.YELLOW.darker(),
                            Color.GREEN.darker()
                       };

    private Color backgroundColour;
    private Color foregroundColour; 

    private ActionListener timerAction = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            x = getX();
            y = getY();
            drawingArea.setXYColourValues(x, y, backgroundColour
                                        , foregroundColour);
        }       
    };

    private JPanel buttonPanel;
    private JButton startStopButton;
    private JButton speedIncButton;
    private JButton speedDecButton;
    private JButton resetButton;
    private JButton colourButton;
    private JButton exitButton;

    private ComponentAdapter componentAdapter = new ComponentAdapter()
    {
        public void componentResized(ComponentEvent ce)
        {
            timer.restart();
            startStopButton.setText("STOP");
            isTimerRunning = true;
        }
    };  

    public BallAnimation()
    {
        x = y = 0;
        positiveX = positiveY = true;
        speedValue = 1;
        colourCounter = 0;
        isTimerRunning = false;
        diameter = 50;
        backgroundColour = Color.WHITE.brighter();
        foregroundColour = colours[colourCounter];
        timer = new Timer(10, timerAction);
    }

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Ball Animation");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);

        drawingArea = new DrawingArea(x, y
                            , backgroundColour, foregroundColour, diameter);
        drawingArea.addComponentListener(componentAdapter);

        frame.add(makeButtonPanel(), BorderLayout.LINE_END);
        frame.add(drawingArea, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);     
    }

    private JPanel makeButtonPanel()
    {
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(0, 1));
        buttonPanel.setBorder(BorderFactory.createLineBorder(
                                    Color.DARK_GRAY, 5, true));

        startStopButton = new JButton("START");
        startStopButton.setBackground(Color.GREEN.darker());
        startStopButton.setForeground(Color.WHITE.brighter());
        startStopButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                System.out.println("START/STOP JButton Clicked!");
                if (!isTimerRunning)
                {
                    startStopButton.setText("STOP");
                    timer.start();
                    isTimerRunning = true;
                    buttonPanel.revalidate();
                    buttonPanel.repaint();
                }
                else if (isTimerRunning)
                {
                    startStopButton.setText("START");
                    timer.stop();
                    isTimerRunning = false;
                    buttonPanel.revalidate();
                    buttonPanel.repaint();
                }
            }
        });
        startStopButton.setBorder(BorderFactory.createLineBorder(
                                    Color.WHITE, 4, true));
        buttonPanel.add(startStopButton);

        colourButton = new JButton("BALL COLOUR");
        colourButton.setBackground(colours[colourCounter]);
        colourButton.setForeground(Color.WHITE);
        colourButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                System.out.println("COLOUR JButton Clicked!");
                //timer.restart();
                colourCounter++;
                if (colourCounter == 9)
                    colourCounter = 0;
                foregroundColour = colours[colourCounter];
                drawingArea.setXYColourValues(x, y, backgroundColour
                                                    , foregroundColour);
                //drawingArea.setForegroundForBall(foregroundColour);
                colourButton.setBackground(foregroundColour);
                colourButton.revalidate();
                colourButton.repaint();
                //timer.start();
            }
        });
        colourButton.setBorder(BorderFactory.createLineBorder(
                                    Color.WHITE, 2, true));
        buttonPanel.add(colourButton);

        exitButton = new JButton("EXIT");
        exitButton.setBackground(Color.RED.darker());
        exitButton.setForeground(Color.WHITE.brighter());
        exitButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                System.out.println("EXIT JButton Clicked!");
                timer.stop();
                System.exit(0);
            }
        });
        exitButton.setBorder(BorderFactory.createLineBorder(
                                    Color.RED.darker().darker(), 4, true));
        buttonPanel.add(exitButton);

        return buttonPanel;
    }

    private int getX()
    {
        if (x < 0)
            positiveX = true;
        else if (x >= drawingArea.getWidth() - diameter)
            positiveX = false;
        return (calculateX());
    }

    private int calculateX()
    {
        if (positiveX)
            return (x += speedValue);
        else
            return (x -= speedValue);
    }

    private int getY()
    {
        if (y < 0)
            positiveY = true;
        else if (y >= drawingArea.getHeight() - diameter)
            positiveY = false;
        return (calculateY());
    }

    private int calculateY()
    {
        if (positiveY)
            return (y += speedValue);
        else 
            return (y -= speedValue);
    }

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

class DrawingArea extends JComponent
{
    private int x;
    private int y;
    private int ballDiameter;
    private Color backgroundColor;
    private Color foregroundColor;

    public DrawingArea(int x, int y
                , Color bColor, Color fColor, int dia)
    {
        this.x = x;
        this.y = y;
        ballDiameter = dia;
        backgroundColor = bColor;
        foregroundColor = fColor;
        setBorder(BorderFactory.createLineBorder(
                        Color.DARK_GRAY.darker(), 5, true));
    }

    public void setXYColourValues(int x, int y
                            , Color bColor, Color fColor)
    {
        this.x = x;
        this.y = y;
        backgroundColor = bColor;
        foregroundColor = fColor;
        repaint();
    }

    public Dimension getPreferredSize()
    {
        return (new Dimension(500, 400));
    }

    public void paintComponent(Graphics g)
    {
        g.setColor(backgroundColor);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(foregroundColor);
        g.fillOval(x, y, ballDiameter, ballDiameter);
    }
}

**LATEST EDIT : **

LATEST

  • 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-31T21:22:47+00:00Added an answer on May 31, 2026 at 9:22 pm

    Seems like there is something wrong with BorderLayout.LINE_END thingy, only when I place the buttonPanel on LINE_END, I am getting undesirable results. I had tried to use only one JButton, instead of three as the latest measure, to sort out thingies. Now the problem that use to come as shown in this pic :

    LINE_END

    has been sorted out by changing the position of the JButton Panel to LINE_START or using JRE version 1.6 update 31, in the pic as below :

    LINE_START

    Here is the code used for this example :

    import java.awt.*;
    
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class BallAnimation
    {
        private int x;
        private int y;
        private boolean positiveX;
        private boolean positiveY;
        private boolean isTimerRunning; 
        private int speedValue;
        private int diameter; 
        private DrawingArea drawingArea;    
        private Timer timer;
        private int colourCounter;
         Color[] colours = {
                                Color.BLUE.darker(),
                                Color.MAGENTA.darker(),
                                Color.BLACK.darker(),
                                Color.RED.darker(),
                                Color.PINK.darker(),
                                Color.CYAN.darker(),
                                Color.DARK_GRAY.darker(),
                                Color.YELLOW.darker(),
                                Color.GREEN.darker()
                           };
    
        private Color backgroundColour;
        private Color foregroundColour; 
    
        private ActionListener timerAction = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                x = getX();
                y = getY();
                drawingArea.setXYColourValues(x, y, backgroundColour
                                            , foregroundColour);
            }       
        };
    
        private JPanel buttonPanel;
        private JButton startStopButton;
        private JButton speedIncButton;
        private JButton speedDecButton;
        private JButton resetButton;
        private JButton colourButton;
        private JButton exitButton;
    
        private ComponentAdapter componentAdapter = new ComponentAdapter()
        {
            public void componentResized(ComponentEvent ce)
            {
                timer.restart();
            }
        };  
    
        public BallAnimation()
        {
            x = y = 0;
            positiveX = positiveY = true;
            speedValue = 1;
            colourCounter = 0;
            isTimerRunning = false;
            diameter = 50;
            backgroundColour = Color.WHITE.brighter();
            foregroundColour = colours[colourCounter];
            timer = new Timer(10, timerAction);
        }
    
        private void createAndDisplayGUI()
        {
            JFrame frame = new JFrame("Ball Animation");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
    
            drawingArea = new DrawingArea(x, y
                                , backgroundColour, foregroundColour, diameter);
            drawingArea.addComponentListener(componentAdapter);
    
            frame.add(makeButtonPanel(), BorderLayout.LINE_START);
            frame.add(drawingArea, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);     
        }
    
        private JPanel makeButtonPanel()
        {
            buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(0, 1));
            buttonPanel.setBorder(BorderFactory.createLineBorder(
                                        Color.DARK_GRAY, 5, true));
    
            colourButton = new JButton("BALL COLOUR");
            colourButton.setOpaque(true);
            colourButton.setBackground(colours[colourCounter]);
            colourButton.setForeground(Color.WHITE);
            colourButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    System.out.println("COLOUR JButton Clicked!");
                    if (timer.isRunning())
                        timer.stop();
                    colourCounter++;
                    if (colourCounter == 9)
                        colourCounter = 0;
                    foregroundColour = colours[colourCounter];
                    drawingArea.setXYColourValues(x, y, backgroundColour
                                                        , foregroundColour);
                    colourButton.setBackground(foregroundColour);
                    if (!timer.isRunning())
                        timer.start();
                }
            });
            colourButton.setBorder(BorderFactory.createLineBorder(
                                        Color.WHITE, 2, true));
            buttonPanel.add(colourButton);
    
            return buttonPanel;
        }
    
        private int getX()
        {
            if (x < 0)
                positiveX = true;
            else if (x >= drawingArea.getWidth() - diameter)
                positiveX = false;
            return (calculateX());
        }
    
        private int calculateX()
        {
            if (positiveX)
                return (x += speedValue);
            else
                return (x -= speedValue);
        }
    
        private int getY()
        {
            if (y < 0)
                positiveY = true;
            else if (y >= drawingArea.getHeight() - diameter)
                positiveY = false;
            return (calculateY());
        }
    
        private int calculateY()
        {
            if (positiveY)
                return (y += speedValue);
            else 
                return (y -= speedValue);
        }
    
        public static void main(String... args)
        {
            Runnable runnable = new Runnable()
            {
                public void run()
                {
                    new BallAnimation().createAndDisplayGUI();
                }
            };
            SwingUtilities.invokeLater(runnable);
        }
    }
    
    class DrawingArea extends JComponent
    {
        private int x;
        private int y;
        private int ballDiameter;
        private Color backgroundColor;
        private Color foregroundColor;
    
        public DrawingArea(int x, int y
                    , Color bColor, Color fColor, int dia)
        {
            this.x = x;
            this.y = y;
            ballDiameter = dia;
            backgroundColor = bColor;
            foregroundColor = fColor;
            setBorder(BorderFactory.createLineBorder(
                            Color.DARK_GRAY.darker(), 5, true));
        }
    
        public void setXYColourValues(int x, int y
                                , Color bColor, Color fColor)
        {
            this.x = x;
            this.y = y;
            backgroundColor = bColor;
            foregroundColor = fColor;
            repaint();
        }
    
        public Dimension getPreferredSize()
        {
            return (new Dimension(500, 400));
        }
    
        public void paintComponent(Graphics g)
        {
            g.setColor(backgroundColor);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(foregroundColor);
            g.fillOval(x, y, ballDiameter, ballDiameter);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If Java application requires certain JRE version then how can I check its availability
On a fresh install of Eclipse: Eclipse - 3.52 Galileo-SR2-win32 Java Version - JRE
I installed/uninstalled java jre/jdk now many times and finally installed the older version 1.6.0_17
What's the best way to determine if the version of the JRE installed on
Will code compiled using 1.5 and 1.6 run on a 1.4 JRE? We weren't
We are facing a problem with lastest JRE 6 update 22 and 23. The
Now with new java 1.6.22 update installed (previous version was OK) my customers are
I am using Launch4j with a bundled jre version. The path of the jre
I updated my JRE to version 7. Then got this error in the eclipse's
I got an exception running XMLspark.jar because of my JRE version configured in 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.