0

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;
    }
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
lsxliron
  • 540
  • 5
  • 11
  • 1
    for better help sooner post an [SSCCE](http://sscce.org/) , btw for BLOB you can to use FileIO for read / write to / from the Database – mKorbel Oct 12 '12 at 06:58
  • You should at least post your JPanel. – Guillaume Polet Oct 12 '12 at 07:16
  • Don't try to save a `JPanel`. Draw in a `BufferedImage` and display it in a panel. Here is [an example](http://stackoverflow.com/a/12683632/418556). Serialize the `BufferedImage`. – Andrew Thompson Oct 12 '12 at 08:46

1 Answers1

3

It's a strange idea to store panel. Instead create a model of your drawings e.g. list of drawing Shapes and serialize the model to store it in DB. Then deserialize the model when you have to load the drawings into panel.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • *"list of drawing Shapes and serialize the model"* Not as easy as it might sound. I was trying to create a serializable undo/redo for a small paint app. but it is surprising how few of the `Graphics` related classes are serializable. `Shape` was not too bad using `XMLEncoder` - I 'only' needed to get a path iterator, navigate the elements, get the point, winding rule and type and drop them into a data bean. ..umm transform the `ArrayList` of segments to an array, and the job was done, same basic process with `Stroke` but `RenderingHints`, `Paint` and `Composite` were looking more tricky.. – Andrew Thompson Oct 12 '12 at 08:40
  • If you get a chance, surf on over to [this thread](http://stackoverflow.com/q/12894526/418556) where 3 people apparently had trouble copying one of your excellent code samples. – Andrew Thompson Oct 15 '12 at 11:31