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.LineBor
der;
public class E_Blind_App{
public static void main(String[]args){//main method
E_BLIND frame = new E_BLIND();
frame.setDefaultCloseOpera
tion(JFram
e.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.addAc
tionListen
er(Bottom_
Button);
Bottom_Panel.EButton.addAc
tionListen
er(Bottom_
Button);
Bottom_Panel.EditButton.ad
dActionLis
tener(Bott
om_Button)
;
Bottom_Panel.DButton.addAc
tionListen
er(Bottom_
Button);
getContentPane().add( Top_Panel,BorderLayout.NOR
TH );
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,10
000)){
System.out.println("Load error.");
System.exit(1);
}
}
catch(InterruptedException
e){
e.printStackTrace();
System.exit(1);
}
if((tracker.statusAll(fals
e) & 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