Throwing an Exception in paintComponent
Im writing a class called PolygonPanel. The class stores the users mouse clicks and redraws a polyline everytime the user clicks the mouse, then when the user presses the c key the polyline becomes a polygon and the coordinates of the polygon are stored in a text file
The problem i am having is trying to get the save method to work in the paintComponent method. Any help would be appreciated and i can provide more of the class if needed.
below is the paintComponent and the save method of the PolygonPanel
Code java:
public void paintComponent (Graphics page)
{
page.setColor (Color.red);
page.drawPolyline(x_coords, y_coords, numPoints);
if (capture == true)// in other words when user presses c key
{
page.drawPolygon (x_coords, y_coords, numPoints);
save("SavedPolygons.rtf");//write the coordinates to this file
//resests the coordinates for another polygon/polyline to be created
for (int i = 0; i<numPoints; i++)
{
x_coords[numPoints] = 0;
y_coords[numPoints] = 0;
}
numPoints = 0;
}
}
public void save(String file) throws IOException
{
PrintWriter out = new PrintWriter(new FileWriter(file));
out.println(toString());
out.close();
}
Re: Throwing an Exception in paintComponent
Quote:
public void save(String file) throws IOException
this method is throwing exception , but you are not catching the exception in the calling method.
paintComponent is an overrided method , so its not possible to throw exception from paintComponent,
so you need to handle it in save itself or in paintComponent .