Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Throwing an Exception in paintComponent

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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

     
    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();
    	}


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Throwing an Exception in paintComponent

    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 .
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Problem with throwing an exception...
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 22nd, 2011, 11:06 PM
  2. why paintComponent method is not invoked?
    By ice in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 10th, 2010, 11:30 PM
  3. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 12:00 AM
  4. Throwing arrays as objects
    By Audemars in forum Collections and Generics
    Replies: 1
    Last Post: September 23rd, 2009, 06:29 PM
  5. Java GUI problem in paintComponent?
    By Richard_ in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2009, 08:19 AM