Image Not Displaying In JFrame

Posted on 2007-10-10
Last Modified: 2013-11-23
Hi All,
I am trying to display an image in the Jframe i coded. But the image is not showing. This is a sample of my codes.

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.border.LineBorder;

public class E_Blind_App{
     public static void main(String[]args){//main method
        E_BLIND frame = new E_BLIND();

public class E_BLIND extends JFrame{
   public E_BLIND(){
       setLayout(new BorderLayout());
       Image TestImage;
        TestImage = Toolkit.getDefaultToolkit().getImage("junk.gif");
       TopPanel Top_Panel = new TopPanel();
       BottomPanel Bottom_Panel = new BottomPanel();
       CenterPanel Center_Panel = new CenterPanel();
       BottomButton Bottom_Button =new BottomButton();
       getContentPane().add( Top_Panel,BorderLayout.NORTH );
       getContentPane().add( Bottom_Panel,BorderLayout.SOUTH );

public class CenterPanel extends JPanel{
    Image TestImage;
    public CenterPanel(){
        MediaTracker tracker;
        int[][][] threeDPix;
        int[][][] threeDPixMod;
        int[] oneDPix;
        int ImgCols, ImgRows;
        TestImage = Toolkit.getDefaultToolkit().getImage("junk.jpg");
        tracker = new MediaTracker(this);
            System.out.println("Load error.");
        catch(InterruptedException e){
        if((tracker.statusAll(false) & MediaTracker.ERRORED & MediaTracker.ABORTED) != 0){
            System.out.println("Load errored or aborted");
        ImgCols = TestImage.getWidth(this);
        ImgRows = TestImage.getHeight(this);
    public void paintComponent(Graphics g) {

Above is my code. The frame contain several panels. One of the panel which is CenterPanel is suppose to diplay the picture. I do not get any errors when executing the code but the image is not being diplayed. When i try to print out the length and width of the image it is able to print it out. I don not know what i am doing wrong. I also have a question when is the paintComponent method in the JPanel being called? Because when i insert a print statement in it the print statement does not display when i execute the code. Please help me.
Question by:talltk
    LVL 13

    Accepted Solution

    I can't see you adding the CenterPanel to the JFrame somewhere
    LVL 13

    Expert Comment

    And in the CenterPanel override the getPreferredSize() method:

        public Dimension getPreferredSize() {
            return new Dimension(TestImage.getWidth(this), TestImage.getHeight(this));


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    EvenOdd challenge 10 69
    wordsWithout 49 64
    High Resolution .gif files 3 36
    Printing to a specific printer tray (HP 806dn printer) 3 33
    Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now