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;
    }
3
  • 1
    for better help sooner post an SSCCE , btw for BLOB you can to use FileIO for read / write to / from the Database Commented Oct 12, 2012 at 6:58
  • You should at least post your JPanel. Commented Oct 12, 2012 at 7:16
  • Don't try to save a JPanel. Draw in a BufferedImage and display it in a panel. Here is an example. Serialize the BufferedImage. Commented Oct 12, 2012 at 8:46

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"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..
If you get a chance, surf on over to this thread where 3 people apparently had trouble copying one of your excellent code samples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.