Adding image in awt
I was trying to add an image in one of GridBagLayout cells. But i am not able to achieve it.
public class imageInAwt extends Applet{
Panel p;
BufferedImage img;
public void init(){
setLayout(new GridBagLayout());
GridBagConstraints gbc =new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
try {
img = ImageIO.read(new File("settings.gif"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
add(new junaid(img),gbc);
开发者_C百科 gbc.gridy = GridBagConstraints.RELATIVE;
add(new Label("Junaid"),gbc);
}
}
class junaid extends Canvas {
Image img;
public junaid(Image img) {
this.img = img;
}
public void paint (Graphics g) {
BufferedImage image = null;
g.drawImage(image, 2, 3, null);
g.drawString("Junaid", 22, 16);
}
public void setImage (Image img){
this.img = img;
}
}
Please help.
Thanks in advance
public void paint (Graphics g) {
BufferedImage image = null;
g.drawImage(image, 2, 3, null);
g.drawString("Junaid", 22, 16);
}
Huh?!?
public void paint (Graphics g) {
g.drawImage(img, 2, 3, this);
g.drawString("Junaid", 22, 16);
}
Use Applet.getImage()
method of the Applet class.
Image img=getImage(getCodeBase(),"settings.gif");
精彩评论