Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Images in an Applet  RSS feed

 
Sachin Deshpande
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am facing a problem. I have an applet. I am making a URLConnection to send data to a servlet and I received data back in the form of bytes. I have to build a Img object out of those bytes and display it on the applet. How do I do that? heres the code that handles the image displaying part.
try {
URLConnection conn = mapServer.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
} catch (IOException ioe) {
System.out.println("Exception in creating UrlConnection/ Connection " + ioe.getMessage());
//ioe.printStackTrace();
}
try {
PrintWriter out = new PrintWriter(conn.getOutputStream());
//next line tells the servlet which image file to read and send back as bytes
out.println("req=" + URLEncoder.encode("try gif"));
out.close();
} catch (IOException ioe) {
System.out.println("Exception in sending data " + ioe.getMessage());
//ioe.printStackTrace();
}
byte[] img = new byte[68000];
try {
DataInputStream in = new DataInputStream(conn.getInputStream());
in.readFully(img);
in.close();
} catch (IOException ioe) {
System.out.println("Exception in getting Img data! Error:" + ioe.getMessage());
//ioe.printStackTrace();
}
Image image = getToolkit().createImage(img);
m_panel.setImage(image);
m_panel.update(m_panel.getGraphics());

Any suggestions...?
TIA
sachin
PS: I dont get any exceptions. Also I read that getToolKit() might not be a good idea(?). I am able to load Images via appletviewer
 
Sachin Deshpande
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but I missed to explain the last part: 'm_panel.update(m_panel.getGraphics())'
Just convert it to
public void update(Graphics g) {
g.drawImage(image, 0, 0, this);
}
and m_panel is a object of PanelExtn (a simple extension on the java.awt.Panel class)
Thanx again
 
John Palmieri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you marshal data you need to make sure that the objects that you are dealing with implement the serializable interface. I don't think the image class implements this interface.
Good Luck.
John Palmieri
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!