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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:23:39+00:00 2026-06-08T09:23:39+00:00

this has been driving me crazy all day so I figured I’d post it

  • 0

this has been driving me crazy all day so I figured I’d post it here and see if someone else can work it out. First off I tried to add a background image, the default ImageIcon was not working so I went with overriding the paint method instead. #

public class ImageJPanel extends JPanel { 
private static final long serialVersionUID = -1846386687175836809L;
Image image = null; 

public ImageJPanel(){ 
    addComponentListener(new ComponentAdapter() { 
    public void componentResized(ComponentEvent e) { 
        ImageJPanel.this.repaint(); 
    } 
}); 
}
//Set the image.
public ImageJPanel(Image i) { 
  image=i; 
  setOpaque(false); 
} 
//Overide the paint component.
public void paint(Graphics g) { 
  if (image!=null) g.drawImage(image, 0, 0, null); 
  super.paint(g); 
} 

}

Once I used that it worked fine, however now I want to add images to my buttons but it is not working. Here is how my buttons work:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Images images = new Images();
JPanel j = new ImageJPanel(images.menuBackground); 
j.setLayout(null);
JButton Button_1= new JButton("Button_1",new ImageIcon("images/gui/Button.png"));

Insets insets = j.getInsets();
Dimension size = Button_1.getPreferredSize();
Button_1.setBounds(300 + insets.left, 150+ insets.top, size.width, size.height);

Singleplayer.setSize(200, 50);

j.add(Button_1);

frame.add(j);
frame.setSize(800,600);
frame.setResizable(false);
Button_1.addMouseListener(singleplayerPressed);

frame.setVisible(true);

All my images are .png, could that affect it?

  • 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-08T09:23:41+00:00Added an answer on June 8, 2026 at 9:23 am

    Let’s start with this:

    public void paint(Graphics g) { 
        if (image!=null) g.drawImage(image, 0, 0, null); 
        super.paint(g); 
    } 
    

    This is the wrong approach. Firstly, you really don’t want to override the paint method UNLESS you absolutely know that this is the correct approach to your problem (and without knowing more, I’d suggest it isn’t).

    Secondly, you paint your image on the component, then promptly paint over the top of it…(super.paint(g); can have the ability to paint over your work, I know the panel is opaque, but this is still a very bad approach).

    Use paintComponent instead

    protected void paintComponent(Graphics g) { 
        super.paint(g); 
        if (image!=null) g.drawImage(image, 0, 0, null); 
    } 
    

    PNG images are fine, they are supported by Swing out of the box.

    Make sure that your program can see the image. Is it been loaded from the file source or is it a resource within you JAR?

    Try this:

    System.out.println(new File("images/gui/Button.png").exits());
    

    If your program can see the file, it will return true other wise the program can not see the file and that is your problem.

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

Sidebar

Related Questions

This has been driving me crazy all day. I need to get a font
This has been driving me crazy all day. I just want the value of
I hope someone can help me. This has been driving me crazy for days.
he this has been driving me nutts, and I hope someone can shed some
This has been driving me crazy! It must be something simple. Here is my
This has been driving me crazy for the better part of the day. I've
this has been driving me crazy, someone please show me where i screwed it
This has been driving be crazy, but I can't seem to find an answer.
This has been driving me crazy because I can't find anything on Google. Maybe
This issue has been driving me crazy all morning. I've been trying to get

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.