I have a class that I wrote which extends JPanel. When the program starts there is a default drawing on the panel (implemented in paintComponent) and the user can draw on this panel.
I'm trying to save the whole JPanel to mySQL database (using BLOB) which goes fine but when I load it, i can see only the default drawing (without the user input). I guess it's because Graphics is not serializable and therefore cannot be saved using ObjectOutputStream. Any idea how can I save the whole thing and then reload it?
The way i save the JPanel to the database:
protected byte [] convertImageToBytes()
{
try
{
Connection conn = new SQLConnection("MYDB").getConnection();
PreparedStatement ps=null;
String sql;
RoundTop rt = StoneGUI.getStoneTop(); //MY CUSTOM PANEL CLASS
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
//oos.writeObject(StoneGUI.getStoneTop());
oos.writeObject(rt);
oos.flush();
oos.close();
bos.close();
byte [] data= bos.toByteArray();
sql="UPDATE StonesDB SET image= ? WHERE lotNumber=5555;";
ps=conn.prepareStatement(sql);
ps.setObject(1, data);
ps.execute();
return data;
}
catch (Exception ex)
{
System.err.println(ex);
return null;
}