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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:24:08+00:00 2026-06-13T07:24:08+00:00

I am coding a gui and I wanted to use embedde some pictures, but

  • 0

I am coding a gui and I wanted to use embedde some pictures, but before embedding it in my main program I wrote that code to test it:

    public class guikopie extends javax.swing.JFrame{
        public guikopie() {
            a = new javax.swing.JLabel();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            a.setIcon(new javax.swing.ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Tulpen.jpg"));       
            add(a);//here i add it to the jlabel
            pack();
        }

        public static void main(String args[]){
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new guikopie().setVisible(true);
                }
            });
        }

        private javax.swing.JLabel a;
    }

My question is: Why does this code do not display the picture?

  • 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-13T07:24:08+00:00Added an answer on June 13, 2026 at 7:24 am

    I think people may hate me for repeating this :P:

    • Dont extend JFrame class
    • Class names begin with captial letter i.e Guikopie

    Also depending on what the background is being used for i.e if its a logo that will be added to a specific location on the JPanel then using a JLabel is fine, however, if its being used as a background it is not; because it will moved around as more components are being added, thus we should not add the background as a component rather we paint the background on the component.

    As for your question:

    My question is: Why does this code do not display the picture?

    your code works perfect for me thus the location of your picture must be incorrect.

    I did a short example showing how to add a Image to JPanel background and then add JPanel to JFrame, it also includes class ImgUtils for resizing picture:

    enter image description here

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.RenderingHints;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class JFrameWithPicture {
    
        public JFrameWithPicture() throws MalformedURLException, IOException {
            initComponents();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        new JFrameWithPicture();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }
    
        private void initComponents() throws MalformedURLException, IOException {
            JFrame frame = new JFrame("Frame with JPanel and background");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            final Image background = ImageUtils.scaleImage(300, 300, ImageIO.read(new URL("http://images2.layoutsparks.com/1/98191/naruto-14-red-design.jpg")));
            final Dimension jpanelDimensions = new Dimension(new ImageIcon(background).getIconWidth(), new ImageIcon(background).getIconHeight());
    
            frame.add(new JPanel() {
                @Override
                protected void paintComponent(Graphics grphcs) {
                    super.paintComponent(grphcs);
                    grphcs.drawImage(background, 0, 0, this);
                }
    
                @Override
                public Dimension getPreferredSize() {
                    return jpanelDimensions;
                }
            });
    
            frame.setResizable(false);
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    class ImageUtils {
    
        public static BufferedImage scaleImage(int width, int height, String filename) {
            BufferedImage bi;
            try {
                ImageIcon ii = new ImageIcon(filename);
                bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                Graphics2D g2d = (Graphics2D) bi.createGraphics();
                g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
                g2d.drawImage(ii.getImage(), 0, 0, width, height, null);
            } catch (Exception e) {
                return null;
            }
            return bi;
        }
    
        static Image scaleImage(int width, int height, BufferedImage filename) {
            BufferedImage bi;
            try {
                bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                Graphics2D g2d = (Graphics2D) bi.createGraphics();
                g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
                g2d.drawImage(filename, 0, 0, width, height, null);
            } catch (Exception e) {
                return null;
            }
            return bi;
        }
    }
    

    Alternatively you may want to resize using Image#getScaledInstance(int width,int height,int hints), but this has its perils, the main problem being:

    Image.getScaledInstance() does not return a finished, scaled image.
    It leaves much of the scaling work for a later time when the image
    pixels are used.

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

Sidebar

Related Questions

I am coding a gui and I wanted to use JFormattedTextField to verify my
I am currently coding a gui and want to verify that the user fills
I have been coding an application which renders tiles and GUI and all that.
I'm coding a prototype, but got problems with the GUI. I want the JPanel
I am learning GUI coding with Qt and hope to clear up some confusion
I am about to start learning coding the GUI. Now I know that its
I am trying to bind events from a GUI file to use code from
I recently did some work modifying a Python gui app that was using wxPython
I'm looking for patterns that concern coding parts of a GUI. Not as global
I'm in need of some advice in proper coding: I'm working on a program

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.