Serialize GeneralPath

I have a class which creates objects of the below class and serializes it. But for some reason its throwing the exception: "java.io.NotSerializableException: java.awt.geom.GeneralPath"

I think its something to do with the Graphics class as it cannot be serialized.

Could anyone please help me out?

class DrawPanelextends JPanelimplements Serializable

{

Point from =new Point(), to =new Point(), origin =new Point();

transient GeneralPath polyPath;

Vector passPoints[] =new Vector[2];

DrawPanel(Vector[] vPoints)//index "0" denotes the x co-ordinate and "1" denotes y

{

origin.x = (Integer) Collections.min(vPoints[0]);

origin.y = (Integer) Collections.min(vPoints[1]);

passPoints = vPoints;

setSize((Integer) Collections.max(vPoints[0]),(Integer) Collections.max(vPoints[1]));//gets the max "x" and "y"

setLocation(origin);//The panel should reside here not from where the first click is

from.x =((Integer) vPoints[0].firstElement()) - origin.x;

from.y =((Integer) vPoints[1].firstElement()) - origin.y;

polyPath =new GeneralPath(GeneralPath.WIND_EVEN_ODD,vPoints[0].size());

polyPath.moveTo(from.x, from.y);

for(int i=1; i< vPoints[0].size(); i++)

{

from.x =( (Integer) vPoints[0].elementAt(i) ) - origin.x;

from.y =( (Integer) vPoints[1].elementAt(i) ) - origin.y;

polyPath.lineTo(from.x, from.y);

}

repaint();//after the polypath is complete send it to the paintComponent for drawing it

}

publicvoid paintComponent(Graphics g)//It reaches this part after getting out of the "mousePressed" event

{

setOpaque(true);

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

g2.draw(polyPath);

}

}

[2708 byte] By [getusamaa] at [2007-11-26 16:26:13]
# 1

> "java.io.NotSerializableException: java.awt.geom.GeneralPath"

>

> I think its something to do with the Graphics class

> as it cannot be serialized.

Kind of answering your own question - GeneralPath doesn't implement the Serializable interface and therefore can not be serialized without writing custom code.

kirillga at 2007-7-8 22:50:19 > top of Java-index,Desktop,Core GUI APIs...
# 2

I have changed the code below but I dont know why but I cannot access "Vector passedVPoints[ ]" (which is initiailsed in the constructor) in the paint method. Could anyone please tell me how I can do that then my problem will be solved?

Thanks a lot

class DrawPanel extends JPanel implements Serializable

{

Point from = new Point(), to = new Point(), origin = new Point();

Vector passedVPoints[] = new Vector[2];

DrawPanel(Vector[] vPoints)//index "0" denotes the x co-ordinate and "1" denotes y

{

origin.x = (Integer) Collections.min(vPoints[0]);

origin.y = (Integer) Collections.min(vPoints[1]);

passedVPoints = vPoints;

setSize((Integer) Collections.max(vPoints[0]),(Integer) Collections.max(vPoints[1]));//gets the max "x" and "y"

setLocation(origin); //The panel should reside here not from where the first click is

repaint(); //after the polypath is complete send it to the paintComponent for drawing it

}

public void paintComponent(Graphics g)//It reaches this part after getting out of the "mousePressed" event

{

setOpaque(true);

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

//g2.draw(polyPath);

System.out.println(passedVPoints[0].size());

from.x =((Integer) passedVPoints[0].firstElement()) - origin.x;

from.y =((Integer) passedVPoints[1].firstElement()) - origin.y;

for(int i=1; i< passedVPoints[0].size(); i++)

{

to.x =( (Integer) passedVPoints[0].elementAt(i) ) - origin.x;

to.y =( (Integer) passedVPoints[1].elementAt(i) ) - origin.y;

g2.drawLine(from.x,from.y, to.x,to.y);

from.x = to.x;

from.y = to.y;

}

}

}

getusamaa at 2007-7-8 22:50:19 > top of Java-index,Desktop,Core GUI APIs...
# 3

> but I cannot access "Vector passedVPoints[ ]" (which is initiailsed in the constructor

Then the parameter you pass into the constructor is not initialized. But since you didn't post a SSCCE again we can't really help.

There is no need to invoke repaint() in the constructor of the panel. The panel will automatically be painted when the GUI is made visible.

camickra at 2007-7-8 22:50:19 > top of Java-index,Desktop,Core GUI APIs...
# 4

okay, find below the simpler code:

I just need a way to pass values into the paintComponent() method

Thanks

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

import java.util.*;

import java.io.*;

public class Test extends javax.swing.JFrame

{

Vector vPoly = new Vector();

public Test()

{

initComponents();

}

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents()

{

layeredPane = new javax.swing.JLayeredPane();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

layeredPane.addMouseMotionListener(new java.awt.event.MouseMotionAdapter()

{

public void mouseDragged(java.awt.event.MouseEvent evt)

{

layeredPaneMouseDragged(evt);

}

});

layeredPane.addMouseListener(new java.awt.event.MouseAdapter()

{

public void mouseClicked(java.awt.event.MouseEvent evt)

{

layeredPaneMouseClicked(evt);

}

public void mousePressed(java.awt.event.MouseEvent evt)

{

layeredPaneMousePressed(evt);

}

});

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.addContainerGap()

.add(layeredPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 377, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.addContainerGap()

.add(layeredPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)

.addContainerGap())

);

pack();

}// </editor-fold>

private void layeredPaneMouseDragged(java.awt.event.MouseEvent evt)

{

System.out.println(evt);

}

private void layeredPaneMouseClicked(java.awt.event.MouseEvent evt)

{

int clickCount = evt.getClickCount();

if (clickCount == 2)

{

Vector vt[] = new Vector[2];

vt[0] = new Vector();

vt[1] = new Vector();

int x[] = {20,50,200,100,300,50,45};

int y[] = {40,100,40,300,60,80,35};

for(int i = 0; i<2; i++)

{

for(int j=0; j< x.length; j++)

{

if (i==0)

{

vt[i].add(x[j]);

}

else

{

vt[i].add(y[j]);

}

}

}

DrawPanel dp = new DrawPanel(vt);

layeredPane.add(dp, new Integer(2));//Lies on top of all other components

}

}

private void layeredPaneMousePressed(java.awt.event.MouseEvent evt)

{

System.out.println(evt);

}

public static void main(String args[])

{

java.awt.EventQueue.invokeLater(new Runnable()

{

public void run()

{

new Test().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JLayeredPane layeredPane;

// End of variables declaration

}

class DrawPanel extends JPanel implements Serializable

{

Point from = new Point(), to = new Point(), origin = new Point();

Vector passedVPoints[] = new Vector[2];

DrawPanel(Vector[] vPoints)//index "0" denotes the x co-ordinate and "1" denotes y

{

origin.x = (Integer) Collections.min(vPoints[0]);

origin.y = (Integer) Collections.min(vPoints[1]);

passedVPoints = vPoints;

setSize((Integer) Collections.max(vPoints[0]),(Integer) Collections.max(vPoints[1]));//gets the max "x" and "y"

setLocation(origin); //The panel should reside here not from where the first click is

repaint(); //after the polypath is complete send it to the paintComponent for drawing it

}

public void paintComponent(Graphics g)//It reaches this part after getting out of the "mousePressed" event

{

setOpaque(true);

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

//g2.draw(polyPath);

System.out.println(passedVPoints[0].size());

from.x =((Integer) passedVPoints[0].firstElement()) - origin.x;

from.y =((Integer) passedVPoints[1].firstElement()) - origin.y;

for(int i=1; i< passedVPoints[0].size(); i++)

{

to.x =( (Integer) passedVPoints[0].elementAt(i) ) - origin.x;

to.y =( (Integer) passedVPoints[1].elementAt(i) ) - origin.y;

g2.drawLine(from.x,from.y, to.x,to.y);

from.x = to.x;

from.y = to.y;

}

}

}

getusamaa at 2007-7-8 22:50:19 > top of Java-index,Desktop,Core GUI APIs...
# 5
Is there any way you can convert a GenaralPath object to something serializable?
getusamaa at 2007-7-8 22:50:19 > top of Java-index,Desktop,Core GUI APIs...