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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:02:30+00:00 2026-05-23T19:02:30+00:00

This is my first question, so please bear with me. I am working on

  • 0

This is my first question, so please bear with me.

I am working on an application (which I have already fully-designed). Now I am at the coding stage and I am having trouble placing a background image on the JFrame while still allowing it to play its role as a Container so I can put buttons on it and things of that nature.

I have created this JFrame class file in Netbeans 7.0 and if someone could tell me how to do this through the interface of Netbeans that would be great (if not, just the code would be fine).

I already know that I am supposed to override the paintComponent method (which I have done already, but my image file is not showing). Also I have a second questions, I don’t want to put the full file-path for the image, I have my source files in packages and now I am quite confused as to where I am to put my image files.

EDIT: initComponoents() is the generated method by Netbeans that determines the properties of the JFrame.

public class TinyTowerOrganizerInterface extends javax.swing.JFrame {

/** Creates new form TinyTowerOrganizerInterface */
    Image backgroundImage = Toolkit.getDefaultToolkit().getImage("D:/Java/TinyTowerOrganizer/Images/Background.jpg");

public TinyTowerOrganizerInterface() throws IOException {

    initComponents(); 
    class BackgroundPanel extends javax.swing.JPanel{
        private Image image;
        public BackgroundPanel(Image image){
            this.image = image;
        }
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);

        }

    }

}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Tiny Tower Organizer");
    setFont(new java.awt.Font("Pixelated", 0, 18)); // NOI18N
    setMinimumSize(new java.awt.Dimension(900, 500));
    setName("frame"); // NOI18N
    setResizable(false);
    setUndecorated(true);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 900, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 500, Short.MAX_VALUE)
    );

    pack();
}


public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            try {
                new TinyTowerOrganizerInterface().setVisible(true);
            } catch (IOException ex) {
                Logger.getLogger(TinyTowerOrganizerInterface.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}
  • 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-23T19:02:31+00:00Added an answer on May 23, 2026 at 7:02 pm

    I already know that I am supposed to override the paintComponent method

    JFrame does not have a paintComponent(…) method.

    Instead you should extend JPanel (or JComponent) and add your custom painting in the paintComponent() method. Then you add the panel to the frame.

    Also, don’t forget to override the getPreferredSize() method of the panel to return the size of the image.

    Edit:

    First of all when you post code post a SSCCE so we can copy and execute the code. I’ve include a simple SSCCE below.

    There are different problems.

    The first problem is that the getImage() method reads the image async so when the frame is displayed, the image is not completely loaded and there is nothing to display as you can see when run the code. Instead use ImageIO to read the image.

    What is it that I even want to get accomplished

    When you make the above change and run the code you will still only see a small frame even though pack() have been invoked. That is because you haven’t added any components to the poanel so the default preferred size is (10, 10) because you are using a FlowLayout. So you need to override the getPreferredSize() method to return the size of the image so the panel can be packed properly.

    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    import javax.imageio.*;
    
    public class MyApplication extends javax.swing.JFrame
    {
    
    /** Creates new form MyApplication */
        Image backgroundImage = Toolkit.getDefaultToolkit().getImage("mong.jpg");
    
        public MyApplication() throws IOException
        {
    
            this.setContentPane(new JPanel()
            {
    
                @Override
                public void paintComponent(Graphics g)
                {
                    super.paintComponent(g);
                    g.drawImage(backgroundImage, 0, 0, null);
                }
            });
    
            pack();
            setVisible(true);
        }
    
    
        public static void main(String[] args)
            throws Exception
        {
            new MyApplication();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question, so please, bear with me. I have a Swing
This is my first question here so please bear with me - I apologise
This is my first question asked here at stackoverflow, so bear with me please
Greetings everyone. This is my first question here at stackoverflow so please bear with
This is my first question so please be patient :) Background: I'm implementing an
this is my first question.. so, here we go. i have a site, 100%
Hello this is may first question and I have found so far the following
This will be quite a long way to ask my question, so please bear
This question is a bit long, please bear with me. In REST, i think
This is not a traditional scale-up or scale-out question. Please bear with me, here

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.