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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:54:12+00:00 2026-05-25T10:54:12+00:00

Just using the paint method and my image won’t show up initially. Once I

  • 0

Just using the paint method and my image won’t show up initially. Once I minimize the java window and resize it, the image shows up. Is there any code I’m missing?

public class Lil extends JFrame {

Image image = Toolkit.getDefaultToolkit().getImage("images/Untitled.png");

public Lil(){

    setTitle("flame");
    setBackground(Color.WHITE);
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
}

public void paint(Graphics g){

    g.clearRect(0, 0, 400, 400);        
    g.drawImage(image, 60, 25, null);
    //repaint();
}   

public static void main(String [] args){    
        new lil();  
    }
}
  • 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-25T10:54:13+00:00Added an answer on May 25, 2026 at 10:54 am

    Don’t draw directly in a JFrame. Instead draw in a JPanel’s paintComponent method and then add that JPanel onto your JFrame’s contentPane. Better still, if you’re not going to be using the image-component as a container (to hold other components), just create an ImageIcon with the Image, place the icon into a JLabel via its constructor or its setIcon method, and simply display the JLabel. No muss, no fuss, no trouble. Also, likely there’s no need to call clearRect if you call super’s paintComponent method as the first call in the JPanel’s paintComponent override method.

    For instance, if going the more complex route of drawing directly in a JPanel, you’d do something like so:

    import java.awt.*;
    import java.awt.image.*;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class Lil extends JPanel {
    
       private static final String URL_PATH = "http://duke.kenai.com/Oracle/" +
            "OracleStratSmall.png";
    
       BufferedImage image = null;
    
       public Lil() {
          setBackground(Color.white);
          try {
             image = ImageIO.read(new URL(URL_PATH));
          } catch (MalformedURLException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
    
       @Override
       public Dimension getPreferredSize() {
          if (image != null) {
             return new Dimension(image.getWidth(), image.getHeight());
          }
          return super.getPreferredSize(); // default
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (image != null) {
             g.drawImage(image, 0, 0, null);
          }
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                Lil lil = new Lil();
    
                JFrame frame = new JFrame();
                frame.getContentPane().add(lil);
                frame.setTitle("flame");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
             }
          });
    
       }    
    }
    

    Again, only do this if you’re going to be placing components on the image such as text fields, buttons, and so forth. If not, then use the simpler ImageIcon/JLabel idea.

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

Sidebar

Related Questions

When programming against a fluent API or just using method-chaining, I've seen the style
I just create a app for writing the text on Screen using TouchEvent method.
I'm trying to convert an image to a binary image (bilevel) using the method
I'm just curious, but is there a name for the process using print statements
We do ASP.NET Development using Visual Studio. A discussion point we've just had is
Just using this as an example... Here are the columns in my UserProfile table:
I know that just using rand() is predictable, if you know what you're doing,
I was just using the plugin Yslow for Mozilla Firefox, and it told me
Instead of just using urllib does anyone know of the most efficient package for
I'm just using the opengl SDL template with Xcode, and everything runs fine. I

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.