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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:36:33+00:00 2026-06-17T12:36:33+00:00

I’m working on a Tron game, and for my menu I used a paint

  • 0

I’m working on a “Tron” game, and for my menu I used a paint method to paint a background to the menu. Everything is good except that the 3 JButtons are “hiding” behind the background until I put my mouse over it then it appears. If there is a way to keep the buttons visible, I would appreciate it!

My code:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Menu extends JFrame {
    private static final long serialVersionUID = 1L;
    String name;
    Image bk;
    public Menu() {
        super("TRON");
        // Set the size of window
        setSize(800, 800);
        // Set the default operation for the window
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        // Set a background
        ImageIcon ibk = new ImageIcon("background.jpg");
        bk = ibk.getImage();
        // Set a new panel
        JPanel p = new JPanel(new GridBagLayout());
        // Make buttons!
        JButton b = new JButton("Play!");
        JButton b2 = new JButton("High Scores");
        JButton b3 = new JButton("Exit");
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new GameFrame();
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new hsview();
            }
        });
        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        // Set the GridBagConstraints to organize the layout
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets (15, 15, 15, 15);
        // Use gbc to space out each button
        gbc.gridx = (0);
        gbc.gridy = (0);
        // Add them to the panel then to JFrame
        p.add(b, gbc);
        gbc.gridx = (0);
        gbc.gridy = (1);
        p.add(b2, gbc);
        gbc.gridx = (0);
        gbc.gridy = (2);
        p.add(b3, gbc);
        // Add to the frame at the bottom
        add(p, BorderLayout.SOUTH);
    }
    // Background stuff
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(bk, 0, 0, null);
    }
}
  • 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-17T12:36:34+00:00Added an answer on June 17, 2026 at 12:36 pm

    Swing uses a “layering” concept for it’s painting…

    paint calls paintComponent, paintBorder and paintChildren. By overriding paint and failing to call super.paint, you’ve prevented the component from painting it’s various layers.

    In Swing, it is preferred to use paintComponent to provide custom painting, which allows you to paint underneath any other components that might be added to the component.

    enter image description here

    public class TestPaint01 {
    
      public static void main(String[] args) {
        new TestPaint01();
      }
    
      public TestPaint01() {
        EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
            try {
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception ex) {
            }
    
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new TestPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    
          }
        });
      }
    
      public class TestPane extends JPanel {
    
        private Image backgroundImage;
    
        public TestPane() {
          try {
            BufferedImage background = ImageIO.read(new File("/path/to/image.jpg"));
            //backgroundImage = background.getScaledInstance(-1, background.getHeight() / 4, Image.SCALE_SMOOTH);
            backgroundImage = background;
          } catch (IOException ex) {
            ex.printStackTrace();
          }
          setLayout(new GridBagLayout());
          add(new JButton("Hello"));
        }
    
        @Override
        public Dimension getPreferredSize() {
          return backgroundImage == null ? super.getPreferredSize() : new Dimension(backgroundImage.getWidth(this), backgroundImage.getHeight(this));
        }
    
        @Override
        protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          int x = (getWidth() - backgroundImage.getWidth(this)) / 2;
          int y = (getHeight() - backgroundImage.getHeight(this)) / 2;
          g.drawImage(backgroundImage, x, y, this);
        }
    
      }
    
    }
    

    You might find A Closer look at the Paint Mechanism and Painting in AWT and Swing informative.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

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.