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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:08:58+00:00 2026-06-15T22:08:58+00:00

Following is the code I have so far: All the imports are correct. I’m

  • 0

Following is the code I have so far:
All the imports are correct. I’m sure. 😀

When I run the program, all I get is a blank frame, without the picture. It should show up.

public class WindowPractice extends JFrame {

   final static int width= 800;
   final static int height= 400;
   int x;
   int y;
   Image steve;
   Dimension gamesize= new Dimension (width, height);
    public WindowPractice(){
        setTitle ("Hangman");
        setSize (gamesize);
        setVisible (true);
        setResizable (false);
        setLocationRelativeTo (null);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);


    }
    public static void main (String[] args) {
        new WindowPractice();
        ImageIcon steve= new ImageIcon ("Libraries/Pictures/ba190cd951302bcebdf216239e156a4.jpg");
        JLabel imageLabel = new JLabel(steve);

    }
    public void paint(Graphics g){

        g.setColor(Color.red);
        //g.fillRect(  x, y, 100, 20);
        g.drawImage(steve, x, y,this);


        x= 150;
        y= 250;
    }

}
  • 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-15T22:09:00+00:00Added an answer on June 15, 2026 at 10:09 pm

    There are so many things wrong with this I’m not sure where to start…

    Let’s start at the beginning…

    Problem #1

    You declare a instance field called steve in you WindowPractice class, this is fine, but in your main method, you declare ANOTHER variable called steve which you are using the reference to the loaded image…

    public static void main(String[] args) {
        new WindowPractice();
        ImageIcon steve = new ImageIcon("C:/Users/shane/Dropbox/issue459.jpg");
        JLabel imageLabel = new JLabel(steve);
    }
    

    This means that the class instance variable is never initialised and remains null.

    Problem #2

    While not directly related, you never call super.paint from your paint method. This is a BIG NO, NO. You are obligated to maintain the paint chain. The paint methods are complex and very, very important.

    Problem #3

    You should never override a top level container (such as JFrame) nor you should you override any of it’s paint methods. There are lots of reasons for this, but among the top two are, most top level containers actually contain a number of components (JRootPane, which houses the glass pane, content pane, layer pane and menu bar) which can sit over your painting efforts and, generally, they’re not double buffered, meaning you paint updates will flicker and look horrible 😉

    You should also avoid using paint, instead, you should look towards using paintComponent where it’s available.

    Problem #4

    ImageIcon is not you best choice for loading images. The main reason I don’t use them is that you have no idea of when the image being loaded will actually become available (actually there are ways, but to be frank, ImageIO is just simpler). This was a great feature back in 1999 when dial-up speeds where around the 14.4k mark, but now days…

    ImageIO supports a wider range of picture formats, supports reading and writing of images and guarantees that when the method returns (successfully), the image pixel data is available to your application.

    Example

    Here’s a better (IMHO) approach…

    enter image description here

    public class BetterDrawing {
    
        public static void main(String[] args) {
            new BetterDrawing();
        }
    
        public BetterDrawing() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new PaintPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class PaintPane extends JPanel {
    
            private BufferedImage background;
    
            public PaintPane() {
                try {
                    background = ImageIO.read(new File("/path/to/image"));
                    // Use this instead to load embedded resources instead
                    //background = ImageIO.read(getClass().getResource("/path/to/image"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            @Override
            public Dimension getPreferredSize() {
                return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                if (background != null) {
    
                    int x = (getWidth() - background.getWidth()) / 2;
                    int y = (getHeight() - background.getHeight()) / 2;
    
                    g.drawImage(background, x, y, this);
    
                }
    
            }
        }
    }
    

    Have a read of

    • Performing custom painting
    • Working with Images

    For some more information.

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

Sidebar

Related Questions

I have to following code and as far as I know it is correct,
So far I have the following code: colors = ('k','r','b') ax = [] for
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Hi I have following code block public class Driver { static String x =
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
In YUI I have following code working for mouse wheel. How do I make
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for loading image from url in xml parsing endElement method
I have following code for inserting data into database using PDO. It inserts data

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.