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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:29:37+00:00 2026-05-29T05:29:37+00:00

So I have a JPanel object as a component of a JFrame, and I

  • 0

So I have a JPanel object as a component of a JFrame, and I am periodically redrawing the contents of the JPanel with a Timer object. Everything is working fine except for the JPanel being redrawn over top of the JFrame’s menu when therefore making the menu items unreadable. Is there a way around this problem without having to pause the timer every time the user goes to access the menu?

Control Frame Class

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

public class ControlFrame extends JFrame implements ActionListener{
    /*======Public Constants======*/
    public static int DEFAULT_HEIGHT = 400;
    public static int DEFAULT_WIDTH = 400;

    /*======Private Instance Variables======*/
    private AnimationPanel animPane;
    private JMenu menu;
    private JMenuItem menuExit;
    private JMenuBar menuBar;

    /*======Constructors======*/
    public ControlFrame(){
        initialize();
    }

    /*======Public Instance Methods======*/
    public void actionPerformed(ActionEvent ae) {
        if(ae.getActionCommand().equals("exit")){
                System.exit(0);
        }
    }

    /*======Private Instance Methods======*/
    private void initialize(){
        this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setLayout(new GridLayout(0,2));

        this.animPane = new AnimationPanel(this.getWidth(), this.getHeight());

        this.add(animPane);

        createCFMenu();

        this.setVisible(true);
    }

    private void createCFMenu(){
        this.menuBar = new JMenuBar();
        this.menu = new JMenu("File");
        this.menu.setMnemonic(KeyEvent.VK_F);
        this.menuBar.add(this.menu);

        this.menuExit = new JMenuItem("Exit", KeyEvent.VK_X);
        this.menuExit.addActionListener(this);
        this.menuExit.setActionCommand("exit");
        this.menu.add(menuExit);

        this.setJMenuBar(this.menuBar);
    }

    /*======Main Method======*/
    public static void main(String[] args){
        ControlFrame cf = new ControlFrame();


    }

}

AnimationPanel Class

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

public class AnimationPanel extends JPanel implements ActionListener{



    /*======Private Instance Variables======*/
    private int timeInterval;
    private Timer animTimer;

    /*======Constructor======*/
    public AnimationPanel(int width, int height){
        timeInterval = 50;

        this.setSize(width, height);

        this.animTimer = new Timer(timeInterval, this);

        animTimer.start();
    }


    public void actionPerformed(ActionEvent arg0) {

        paint();
    }

    /*======Private Instance Variables======*/
    private void paint(){
        BufferedImage bImage = new BufferedImage(this.getWidth(), 
            this.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics bg = bImage.getGraphics();

        bg.setColor(Color.WHITE);
        bg.fillRect(0, 0, bImage.getWidth(), bImage.getHeight());

        this.getGraphics().drawImage(bImage, 0, 0, this);
    }
} 

The problem is the Animation Panel is drawing over top of the ControlFrames Menu

  • 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-29T05:29:38+00:00Added an answer on May 29, 2026 at 5:29 am

    Don’t call getGraphics() in Java code. A Java GUI must repaint when it is told to do so, and should do so using either paint(Graphics) or paintComponent(Graphics). That is why the menu was vanishing.

    The bug is solved in this version of AnimationPanel.

    class AnimationPanel extends JPanel implements ActionListener{
        /*======Private Instance Variables======*/
        private int timeInterval;
        private Timer animTimer;
    
        /*======Constructor======*/
        public AnimationPanel(int width, int height){
            timeInterval = 50;
            this.setSize(width, height);
            this.animTimer = new Timer(timeInterval, this);
            animTimer.start();
        }
    
        public void actionPerformed(ActionEvent arg0) {
            repaint();
        }
    
        /*======Private Instance Variables======*/
        public void paintComponent(Graphics g){
            // important to get the component to paint itself & borders etc.
            super.paintComponent(g); 
            BufferedImage bImage = new BufferedImage(this.getWidth(),
                this.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics bg = bImage.getGraphics();
    
            bg.setColor(Color.WHITE);
            bg.fillRect(0, 0, bImage.getWidth(), bImage.getHeight());
            bg.dispose();  // Assist the garbage collector!
    
            g.drawImage(bImage, 0, 0, this);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Graphics object of JPanel and that is working fine: import java.awt.Color;
Consider the case where I have a JFrame and a JPanel object, with the
I have a JFrame with JScrollPane in it. I have JPanel inside a scrollPane.
I have a JFrame and JPanel full of Jsomethings with an actionlistener. When the
I have a JComboBox object in my JFrame which behaves exactly as expected. However,
I have a JFrame and a Jpanel over that in which various buttons are
I have two views (JPanel) that uses the same domain object. My domain object
I have a JPanel, which I would like to detect the following events (1)
I have a JPanel which uses a BoxLayout in the X_AXIS direction. The problem
I have a JPanel that encapsulates two JPanels, one on top of the other.

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.