I have a Serializable object which is supposed to hold a java.awt.Image as its member. How should I go about serializing it? (Edited from a not so clear first version, sorry.)
You can't serialize an Image . It is typically "hooked into" the sender's graphics environment in ways that serialization is not able to deal with. What you need to do is to mark the img field as transient . The net effect will be that the receiver sees null as the value.
To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.
The ObjectOutputStream class contains writeObject() method for serializing an Object.
ImageIcon implements Serializable and it can be used to wrap an Image class
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/ImageIcon.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With