We have a program for displaying an image wherein image uri is got from xml structure.As output we got only blank frame.
Program is :
public class DOM extends Frame
{
ImageCanvas c;
BufferedImage image=null;
public static void main(String[] arg)
{
DOM ob=new DOM();
ob.addWindowLis tener( new WindowAdapter()
{ public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
} );
ob.setSize( 500, 500 );
ob.show();
}
public DOM()
{
try
{
image =ImageIO.read(t his.getClass(). getResource("lo tus.jpg")); //Image is read as null .This is the problem
geturi();
}
catch(Exception e)
{
System.out.prin tln(e);
}
}
public void geturi()
{
String[] ds1 =new String[100] ;
try {
BufferedReader bf = new BufferedReader( new InputStreamRead er(System.in));
System.out.prin t("Enter XML File name: ");
String xmlFile = bf.readLine();
File file = new File(xmlFile);
byte buf[]=new byte[100];
if(file.exists( ))
{
int j=0;
DocumentBuilder Factory factory = DocumentBuilder Factory.newInst ance();
DocumentBuilder builder = factory.newDocu mentBuilder();
Document doc = builder.parse(x mlFile);
NodeList list = doc.getElements ByTagName("*");
System.out.prin tln("XML Elements: ");
BufferedReader br=new BufferedReader( new InputStreamRead er(System.in));
System.out.prin tln("Enter keyword");
String str=br.readLine ();
System.out.prin tln("String="+s tr);
for (int i=0; i<list.getLengt h(); i++)
{
Element element =(Element)list. item(i);
ds1[j]=element.getTex tContent();
if(ds1[j].equals(str))
{
System.out.prin tln("OUR path" +ds1[2]);
}
j++;
}
String uri= ds1[2];
imagdisp(uri);
return;
}
else
{
System.out.prin t("File not found!");
}
}
catch (Exception e)
{
System.out.prin tln(e);
System.exit(1);
}
}
public void imagdisp(String m)
{
try
{
System.out.prin tln("******"+m) ;
}
catch(Exception e)
{
System.out.prin tln(e);
}
c = new ImageCanvas( image );
add( c );
return;
}
}
class ImageCanvas extends Canvas
{
Image i;
public ImageCanvas( Image i )
{
this.i = i;
}
public void paint( Graphics g )
{
g.drawImage( i, 1200, 1000, this );
return;
}
}
Program is :
public class DOM extends Frame
{
ImageCanvas c;
BufferedImage image=null;
public static void main(String[] arg)
{
DOM ob=new DOM();
ob.addWindowLis tener( new WindowAdapter()
{ public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
} );
ob.setSize( 500, 500 );
ob.show();
}
public DOM()
{
try
{
image =ImageIO.read(t his.getClass(). getResource("lo tus.jpg")); //Image is read as null .This is the problem
geturi();
}
catch(Exception e)
{
System.out.prin tln(e);
}
}
public void geturi()
{
String[] ds1 =new String[100] ;
try {
BufferedReader bf = new BufferedReader( new InputStreamRead er(System.in));
System.out.prin t("Enter XML File name: ");
String xmlFile = bf.readLine();
File file = new File(xmlFile);
byte buf[]=new byte[100];
if(file.exists( ))
{
int j=0;
DocumentBuilder Factory factory = DocumentBuilder Factory.newInst ance();
DocumentBuilder builder = factory.newDocu mentBuilder();
Document doc = builder.parse(x mlFile);
NodeList list = doc.getElements ByTagName("*");
System.out.prin tln("XML Elements: ");
BufferedReader br=new BufferedReader( new InputStreamRead er(System.in));
System.out.prin tln("Enter keyword");
String str=br.readLine ();
System.out.prin tln("String="+s tr);
for (int i=0; i<list.getLengt h(); i++)
{
Element element =(Element)list. item(i);
ds1[j]=element.getTex tContent();
if(ds1[j].equals(str))
{
System.out.prin tln("OUR path" +ds1[2]);
}
j++;
}
String uri= ds1[2];
imagdisp(uri);
return;
}
else
{
System.out.prin t("File not found!");
}
}
catch (Exception e)
{
System.out.prin tln(e);
System.exit(1);
}
}
public void imagdisp(String m)
{
try
{
System.out.prin tln("******"+m) ;
}
catch(Exception e)
{
System.out.prin tln(e);
}
c = new ImageCanvas( image );
add( c );
return;
}
}
class ImageCanvas extends Canvas
{
Image i;
public ImageCanvas( Image i )
{
this.i = i;
}
public void paint( Graphics g )
{
g.drawImage( i, 1200, 1000, this );
return;
}
}