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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:35:11+00:00 2026-05-24T00:35:11+00:00

So I have a JSplitPane, and two JPanels – one on top, one on

  • 0

So I have a JSplitPane, and two JPanels – one on top, one on the bottom. In both panels I overrode the paintComponent method and added my own graphics. In the bottom panel, I wanted to add an animation. When the panel does not repaint, it’s fine, but as soon as the Timer (javax.swing.Timer) starts to call repaints, the bottom panel mimics the appearance of the top panel and glitches out. The actual animations are not refreshed, but rather it keeps on adding (like a dragged paintbrush instead of a moving object).

Here’s the code for the Bottom Panel class:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;

public class WaitControls extends JPanel {

    private int pos;

    public WaitControls(){
        setBackground(Color.gray);
        pos = 0;
    }

    public void progress(){
        //animation timer:
        Timer timer = new Timer(30, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                pos++;
                repaint();
            }
        });
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g){
        g.fillRect(pos, pos, 10, 20);
    }
}

And here’s the code for the Splitpane class:

//my classes (imported packages)
import rcc.controls.ControlPanel;
import rcc.controls.InitControls;
import rcc.controls.WaitControls;
import rcc.video.Screen;

import javax.swing.JSplitPane;

public class MainPanel extends JSplitPane{

    public RCC rcc;
    public Screen screen;

    private int height;

    public ControlPanel curPanel;

    public MainPanel(RCC rcc, Screen screen, int height){
        super(JSplitPane.VERTICAL_SPLIT);

        this.rcc = rcc;
        this.screen = screen;
        this.height = height;

        setDividerSize(2);
        setEnabled(false);

        setTopComponent(screen);

        setToInitControls();
    }

    //sets the control panel to init controls ***WORKS FINE***
    public void setToInitControls(){
        InitControls initCtrls = new InitControls(this);
        setBottomComponent(initCtrls);
        curPanel = initCtrls;
        setDividerLocation(height / 4 * 3);
    }

    //sets the control panel to wait controls (trying to connect) ***GLITCHES***
    public void setToWaitControls(){
        WaitControls waitCtrls = new WaitControls();
        setBottomComponent(waitCtrls);
        curPanel = waitCtrls;
        setDividerLocation(height / 4 * 3);
        waitCtrls.progress();
    }
}

The top panel is a bit complicated. It involves mouse action (including a MouseEntered listener) and animates to interact with user mouse input.
The strange thing is, I have another bottom panel that was swapped out that also uses animations, and a timer, and does not have this glitch.

Any ideas what may have caused this? Thank you for all your help!

  • 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-24T00:35:12+00:00Added an answer on May 24, 2026 at 12:35 am

    I can’t imagine how your animations works,

    1/ but if animation(s) depends of by any of Listener then Timer must be Timer#restart();

    2/ check (example), how to pass addNotify()/removeNotify() for start/stop animatiom(s)

    NOTE required fullHD monitor for better output or change code line

    for (int iPanels = 0; iPanels < 3; iPanels++) {
    

    to

    for (int iPanels = 0; iPanels < 2; iPanels++) {
    

    Example:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    public class AnimationBackground {
    
        public AnimationBackground() {
            Random random = new Random();
            JFrame frame = new JFrame("Animation Background");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
            frame.setLayout(new GridLayout(0, 3, 10, 10));
            for (int iPanels = 0; iPanels < 3; iPanels++) {
                final MyJPanel panel = new MyJPanel();
                panel.setBackground(Color.BLACK);
                for (int i = 0; i < 50; i++) {
                    Star star = new Star(new Point(random.nextInt(490), random.nextInt(490)));
                    star.setColor(new Color(100 + random.nextInt(155), 100 + random.nextInt(155), 100 + random.nextInt(155)));
                    star.setxIncr(-3 + random.nextInt(7));
                    star.setyIncr(-3 + random.nextInt(7));
                    panel.add(star);
                }
                panel.setLayout(new GridLayout(10, 1));
                JLabel label = new JLabel("This is a Starry background.", JLabel.CENTER);
                label.setForeground(Color.WHITE);
                panel.add(label);
                JPanel stopPanel = new JPanel();
                stopPanel.setOpaque(false);
                stopPanel.add(new JButton(new AbstractAction("Stop this madness!!") {
    
                    private static final long serialVersionUID = 1L;
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        panel.stopAnimation();
                    }
                }));
                panel.add(stopPanel);
                JPanel startPanel = new JPanel();
                startPanel.setOpaque(false);
                startPanel.add(new JButton(new AbstractAction("Start moving...") {
    
                    private static final long serialVersionUID = 1L;
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        panel.startAnimation();
                    }
                }));
                panel.add(startPanel);
                frame.add(panel);
            }
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    AnimationBackground animationBackground = new AnimationBackground();
                }
            });
        }
    
        class Star extends Polygon {
    
            private static final long serialVersionUID = 1L;
            private Point location = null;
            private Color color = Color.YELLOW;
            private int xIncr, yIncr;
            static final int WIDTH = 500, HEIGHT = 500;
    
            Star(Point location) {
                int x = location.x;
                int y = location.y;
                this.location = location;
                this.addPoint(x, y + 8);
                this.addPoint(x + 8, y + 8);
                this.addPoint(x + 11, y);
                this.addPoint(x + 14, y + 8);
                this.addPoint(x + 22, y + 8);
                this.addPoint(x + 17, y + 12);
                this.addPoint(x + 21, y + 20);
                this.addPoint(x + 11, y + 14);
                this.addPoint(x + 3, y + 20);
                this.addPoint(x + 6, y + 12);
            }
    
            public void setColor(Color color) {
                this.color = color;
            }
    
            public void move() {
                if (location.x < 0 || location.x > WIDTH) {
                    xIncr = -xIncr;
                }
                if (location.y < 0 || location.y > WIDTH) {
                    yIncr = -yIncr;
                }
                translate(xIncr, yIncr);
                location.setLocation(location.x + xIncr, location.y + yIncr);
            }
    
            public void setxIncr(int xIncr) {
                this.xIncr = xIncr;
            }
    
            public void setyIncr(int yIncr) {
                this.yIncr = yIncr;
            }
    
            public Color getColor() {
                return color;
            }
        }
    
        class MyJPanel extends JPanel {
    
            private static final long serialVersionUID = 1L;
            private ArrayList<Star> stars = new ArrayList<Star>();
            private Timer timer = new Timer(20, new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (Star star : stars) {
                        star.move();
                    }
                    repaint();
                }
            });
    
            public void stopAnimation() {
                if (timer.isRunning()) {
                    timer.stop();
                }
            }
    
            public void startAnimation() {
                if (!timer.isRunning()) {
                    timer.start();
                }
            }
    
            @Override
            public void addNotify() {
                super.addNotify();
                timer.start();
            }
    
            @Override
            public void removeNotify() {
                super.removeNotify();
                timer.stop();
            }
    
            MyJPanel() {
                this.setPreferredSize(new Dimension(520, 520));
            }
    
            public void add(Star star) {
                stars.add(star);
            }
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                for (Star star : stars) {
                    g.setColor(star.getColor());
                    g.fillPolygon(star);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vertically split JSplitPane and when I move the divider down, it
I have created a divider in JSplitPane . I am unable to set the
Have any one run into trouble when running dotLess and having hacks on your
Have any one tried to activate fancybox thumbnail gallery using a button or an
I have a JFrame with a JSplitPane that is OneTouchExpandable. I want to remember
Have this method call: -> simpleJdbcTemplate.queryForInt(SQL,null); -> queryForInt() method in the springs SimpleJdbcTemplate throws
I have three JPanels inside a main Frame. Clockwise from the left, in the
Have added the following code to my SQL query: (Note cut down version) DECLARE
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could

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.