• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1016
  • Last Modified:

Image Not Displaying In JFrame

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();
       
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(900,700);
        frame.setVisible(true);
    }
}

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();
       Bottom_Panel.OButton.addActionListener(Bottom_Button);
       Bottom_Panel.EButton.addActionListener(Bottom_Button);
       Bottom_Panel.EditButton.addActionListener(Bottom_Button);
       Bottom_Panel.DButton.addActionListener(Bottom_Button);
       
       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);
        tracker.addImage(TestImage,0);
       
        try{
            if(!tracker.waitForID(0,10000)){
            System.out.println("Load error.");
            System.exit(1);
            }
        }
        catch(InterruptedException e){
            e.printStackTrace();
            System.exit(1);
        }
       
        if((tracker.statusAll(false) & MediaTracker.ERRORED & MediaTracker.ABORTED) != 0){
            System.out.println("Load errored or aborted");
            System.exit(1);
        }
       
        ImgCols = TestImage.getWidth(this);
        ImgRows = TestImage.getHeight(this);
       
        System.out.println(ImgRows);
        System.out.println(ImgCols);
             
    }
    public void paintComponent(Graphics g) {
        System.out.println("Hello");
        super.paintComponent(g);
        g.drawImage(TestImage,0,0,this);
       
    }
}

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.
0
talltk
Asked:
talltk
  • 2
1 Solution
 
Bart CremersJava ArchitectCommented:
I can't see you adding the CenterPanel to the JFrame somewhere
0
 
Bart CremersJava ArchitectCommented:
And in the CenterPanel override the getPreferredSize() method:

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

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now