serialization - Java can you make a predefined class in java serializable? -
i want send image object on network in java.
im getting error.
java.io.writeabortedexception: writing aborted; java.io.notserializableexception: sun.awt.image.offscreenimage
the java image object doesn't implement serializable. there way around this?
ive tried making subclass of image , implement got errors when using createimage method. help.
edit*
ok here code there kinda lot. idea of program pictionary game. can draw using basic tools , send on network , draw image on other clients screen.
this basic draw area user , draw using line tool. on mouse released try , send image object on server.
class paddraw extends jcomponent { image image; graphics2d graphics2d; int currentx, currenty, oldx, oldy; int linesize = 1; public paddraw() { setdoublebuffered(false); addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e) { oldx = e.getx(); oldy = e.gety(); } }); addmousemotionlistener(new mousemotionadapter() { @override public void mousedragged(mouseevent e) { currentx = e.getx(); currenty = e.gety(); //graphics2d.drawline(oldx, oldy, currentx, currenty); //this drawing //it seems draw line between old coordinate point , new coordinate point rather drawing points //test see if can drawoval work rather line //graphics2d.filloval(currentx-1, currenty-1, 2, 2); //if works should draw oval @ cursor position rather drawing line //technically works, without line causes gaps //i may have found it. testing setstroke method graphics2d.setstroke(new basicstroke(linesize)); graphics2d.drawline(oldx, oldy, currentx, currenty); repaint(); oldx = currentx; oldy = currenty; } }); addmouselistener(new mouseadapter() { @override public void mousereleased(mouseevent e) { try { clientoutputstream.writeobject(image); } catch (ioexception ex) { logger.getlogger(chatclient.class.getname()).log(level.severe, null, ex); } } }); } @override public void paintcomponent(graphics g) { if (image == null) { image = (image) createimage(getsize().width, getsize().height); graphics2d = (graphics2d) image.getgraphics(); graphics2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); clear(); } g.drawimage(image, 0, 0, null); } public void updateimage(image image){ this.image = image; repaint(); } public void clear() { graphics2d.setpaint(color.white); graphics2d.fillrect(0, 0, getsize().width, getsize().height); //graphics2d.setpaint(color.black); linesize = 1; repaint(); } public void fill(){ color c = findcolor(); graphics2d.setpaint(c); graphics2d.fillrect(0, 0, getsize().width, getsize().height); repaint(); } public void changecolor(color thecolor) { graphics2d.setpaint(thecolor); repaint(); } public color findcolor() { return graphics2d.getcolor(); } public void changesize(int size) { linesize = size; } }
this threaded class image on server.
private static class handler2 extends thread { private socket socket1; private objectinputstream serverinputstream; private objectoutputstream serveroutputstream; public handler2(socket sock1) { socket1 = sock1; } @override public void run() { image image = null; try { serverinputstream = new objectinputstream(socket1.getinputstream()); serveroutputstream = new objectoutputstream(socket1.getoutputstream()); oos.add(serveroutputstream); while (true) { image = (image)serverinputstream.readobject(); (objectoutputstream ooss : oos) { ooss.writeobject(image); } } } catch (ioexception e) { system.out.println(e); } catch (classnotfoundexception ex) { logger.getlogger(chatserver.class.getname()).log(level.severe, null, ex); } { if (serveroutputstream != null) { oos.remove(serveroutputstream); } try { socket1.close(); } catch (ioexception e) { system.out.println(e); } } } }
and in client have method getting image server.
public void run2() throws ioexception, classnotfoundexception, interruptedexception { // make connection , initialize streams serveraddress = getserveraddress(); socket socket2 = new socket(serveraddress, 9999); //string theip = getip(); //socket socket2 = new socket(theip, 9999); // process messages server, according protocol. clientoutputstream = new objectoutputstream(socket2.getoutputstream()); clientinputstream = new objectinputstream(socket2.getinputstream()); while (true) { image ni = (image)clientinputstream.readobject(); drawpad.updateimage(ni); } }
i know code kinda bad. split thinks lot test individual parts. fare network code. should work. problem believe not serializable.
the java image object doesn't implement serializable. there way around this?
you can serialize yourself. can wrap class externalizable, or can write data in image without using writeobject.
Comments
Post a Comment