Advertisement

10.10.2007 at 02:02PM PDT, ID: 22885442
[x]
Attachment Details

Image Not Displaying In JFrame

Asked by talltk in Java AWT & Swing, New to Java Programming, Graphics

Tags: displaying, image, 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.
Start Free Trial
[+][-]10.10.2007 at 11:41PM PDT, ID: 20054664

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java AWT & Swing, New to Java Programming, Graphics
Tags: displaying, image, jframe
Sign Up Now!
Solution Provided By: Bart_Cr
Participating Experts: 1
Solution Grade: A
 
 
[+][-]10.10.2007 at 11:45PM PDT, ID: 20054680

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628