source file name: FrameImageDemo.java
Note: Animals images source files exists on storage media.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class FrameImageDemo extends Frame implements ItemListener
{
Choice ch;
String str="";
Image img=null;
public static void main(String[] args) {
FrameImageDemo f=new FrameImageDemo();
f.setTitle("Image programs");
f.setSize(400,500);
f.setVisible(true);
}
public FrameImageDemo()
{
FlowLayout fl=new FlowLayout();
setLayout(fl);
ch=new Choice();
Label lb=new Label("select your favorite Animal : ");
ch.add("Tiger");
ch.add("Giraffe");
ch.add("Lion");
ch.add("Bibtya");
ch.add("Zebra");
add(lb); add(ch);
ch.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
String name=ch.getSelectedItem();
System.out.println(name);
if(name.equals("Tiger"))
{
str="F:\\Nalawade\\java1819\\tiger.jpg";
}
if(name.equals("Giraffe"))
{
str="giraffe.jpg";
}
if(name.equals("Lion"))
{
str="lion.jpg";
}
if(name.equals("Bibtya"))
{
str="bibtya.jpg";
}
if(name.equals("Zebra"))
{
str="zebra.jpg";
}
repaint();
}
public void paint(Graphics g)
{
g.drawString("See your Favorite animal: ",50,100);
img=Toolkit.getDefaultToolkit().getImage(str);
g.drawImage(img,50,150,this);
}
}
output:
0 Comments
Post a Comment