Hey guys,

I am working on a masters project. I am creating L-System java applet... it draws lines on to panel and i need to figure out whats the best way to get that to save as a image?

here is the code:

Java Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.*;
import javax.swing.event.*;

public class LSystem extends JApplet implements ActionListener, ChangeListener
{
	int userSelectedIterations;		// the number of iterations user wants
	int userSelectedSystem; 		// the system which user wants to be drawn on the screen
	int userSelectedScale;			// the user can scale the curve to fit on the screen
	int startX,startY; 				// the location where the drawing of the curve will start
	String systemName[] = new String [40];
	
	JLabel systemLabel; 			// label for L-System menu
	JLabel iterationLabel; 			// label for iteration menu
	JComboBox iterMenu; 			// ComboBox for iteration menu
	JComboBox selectSystemMenu;		// ComboBox for system menu
	JLabel scaleLabel;				// label for scale slider
	JSlider scaler; 			    // actual slider
	
	
   public void init()
   {	  
	   resize(1200,600); //the screen size
	  
	   userSelectedIterations = 0;
	   userSelectedScale = 20;
	   startX= 20;
	   startY= 20;
	   
	   JPanel printToScreen = new JPanel()
	   {
		   public void paintComponent(Graphics g)
		   {
			   if(userSelectedSystem==0)
				   curveOne(g, userSelectedIterations, userSelectedScale, startX, startY); // Just draw the curveOne.
			   if(userSelectedSystem==1)
				   curveOne2(g, userSelectedIterations, userSelectedScale, startX, startY); // Just draw the curveOne.
		   }
	   };
	   this.getContentPane().add(printToScreen, BorderLayout.CENTER);
	   JPanel optionPanel = new JPanel();
	   optionPanel.setLayout(new GridLayout(2,3));
	   systemLabel = new JLabel("Select the LSystem!",JLabel.CENTER);
	   optionPanel.add(systemLabel);		//add label to optionPanel
	   
	   iterationLabel = new JLabel("Select number of iterations!",JLabel.CENTER);
	   optionPanel.add(iterationLabel);		//add label to optionPanel
	   
	   scaleLabel = new JLabel("Slide to Scale",JLabel.CENTER);
	   optionPanel.add(scaleLabel);			//add label to optionPanel
	  
	   selectSystemMenu = new JComboBox();
	   
	   selectSystemMenu.addItem("Koch Curve");
	   selectSystemMenu.addItem("Curve Two");
	   selectSystemMenu.addActionListener(this);
	   optionPanel.add(selectSystemMenu);
	 //*******************************Define the iteration Menu
		iterMenu = new JComboBox();
		for(int i=0; i<6; i++)
		{
			iterMenu.addItem(""+i);
		}//end for loop to add iteration options from one to five
		iterMenu.setSelectedIndex(userSelectedIterations);
		iterMenu.addActionListener(this);
		optionPanel.add(iterMenu);
		
		//*******************************Define the scale slider
		scaler = new JSlider(1,25,userSelectedScale);
		scaler.setMajorTickSpacing(25);
		scaler.setMinorTickSpacing(5);
		scaler.setPaintTicks(true);
		scaler.addChangeListener(this);
		optionPanel.add(scaler);
		
		this.getContentPane().add(optionPanel, BorderLayout.SOUTH);
	   
   }//end initiate
   
   public void curveOne(Graphics g, int iter, int scale, int x, int y)
	{
	   	Graphics2D g2= (Graphics2D)g;
		String commandLine;				// the string of variables that commands the "turtle movement"
		commandLine = "F";				// defines the first iteration of the L-system
		String commandInput = "F+F-F-F+F";	// defines the Input update to the commandLine
		AffineTransform origin = g2.getTransform();//set the origin place holder to the original top-left corner of the applet
		int commandLength = commandLine.length(); //gets the number of commands in the commandLine String
		char current;					//defines the current command being inspected when the string is read
		double turnAngle = Math.PI/2; 	//defines the constant number of radians a left or right turn is equal to
		//System.out.println(""+Math.PI/2);
		int unit = scale;			//defines the constant length of the unit a forward move equals
		int iterations = iter;
		
		//System.out.println(commandLength);
		//System.out.println(commandLine);	//to test to see what we have in commandline
		
		g2.translate(x,y);//move origin of the drawing to point (x,y) so the graphic shows up on the screen
		
		//loop that draws the L-system at the given number of iterations
		for(int i=0; i<iterations; i++)
		{
			commandLine= myReplace(commandLine, 'F', commandInput);//each iteration changes F's to the following new command
			commandLength = commandLine.length();
			//System.out.println(commandLength);
			//System.out.println(commandLine);	//to test the performance of the code
		}//finish with for i loop
				
		for(int k=0; k<=commandLength; k++)
		{
			if(k == commandLength)
			{
				g2.setTransform(origin);//reset origin to the original applet top left corner. 
			}else if (k < commandLength)
			{
				//g.setColor(color);
				current = commandLine.charAt(k);
				//System.out.println(k);
				//System.out.println(current);	//to test the performance of the code
				if(current == 'F')
				{
					g.drawLine(0, 0, unit, 0);	//draw the line forward one unit from the translated origin along the x axist
					g2.translate(unit, 0);// move the origin to the end of the line
				}
				else if(current == '-')
				{
					g2.rotate(-(turnAngle), 0, 0);//rotate around the start point counter clockwise
				}
				else if(current == '+')
				{
					g2.rotate(turnAngle, 0, 0);//rotate around the start point clockwise
				}
			}
				
		}//finish with for k loop
		
	}//finish w/ koch curve
   
   public void curveOne2(Graphics g, int iter, int scale, int x, int y)
	{
	   	Graphics2D g2= (Graphics2D)g;
		String commandLine;				// the string of variables that commands the "turtle movement"
		commandLine = "F";				// defines the first iteration of the L-system
		String commandInput = "F+F-F-F+F";	// defines the Input update to the commandLine
		AffineTransform origin = g2.getTransform();//set the origin place holder to the original top-left corner of the applet
		int commandLength = commandLine.length(); //gets the number of commands in the commandLine String
		char current;					//defines the current command being inspected when the string is read
		double turnAngle = Math.PI/2; 	//defines the constant number of radians a left or right turn is equal to
		int unit = scale;			//defines the constant length of the unit a forward move equals
		int iterations = iter;
		
		//System.out.println(commandLength);
		//System.out.println(commandLine);	//to test the performance of the code
		
		//g2.translate(x,y);//move origin of the drawing to point (x,y) which is the beginning point of the graphic
		g2.translate(100,100);
		//loop that draws the L-system at the given number of iterations
		for(int i=0; i<iterations; i++)
		{
			commandLine= myReplace(commandLine, 'F', commandInput);//each iteration changes F's to the following new command
			commandLength = commandLine.length();
			//System.out.println(commandLength);
			//System.out.println(commandLine);	//to test the performance of the code
		}//finish with for i loop
				
		for(int z=0; z<=commandLength; z++)
		{
			if(z == commandLength)
			{
				g2.setTransform(origin);//reset origin to the original applet top left corner. 
			}else if (z < commandLength)
			{
				//g.setColor(color);
				current = commandLine.charAt(z);
				//System.out.println(k);
				//System.out.println(current);	//to test the performance of the code
				if(current == 'F')
				{
					g.drawLine(0, 0, unit, 0);	//draw the line forward one unit from the translated origin along the x axist
					g2.translate(unit, 0);// move the origin to the end of the line
				}
				else if(current == '-')
				{
					g2.rotate(-(turnAngle), 0, 0);//rotate around the start point counter clockwise
				}
				else if(current == '+')
				{
					g2.rotate(turnAngle, 0, 0);//rotate around the start point clockwise
				}
			}
				
		}//done with the z loop
		
	}//done with the curve
   
   
   	//this method takes tree arguments.
    //first one is the full string.
    //2nd is a character which will be replaced.
    //3rd is a string that will replace character.
    //so this method will change 'char letter' with 
	//with a newString that changes every character (c) to with the replacement string(r) and leaves other characters alone
	private String myReplace(String oldString, char letter, String replacement)
	{
		char currentChar;					//to keep track of current character being inspected
		String updatedString = null;		//the updated string which will be returned.
		for(int i=0; i < oldString.length(); i++)
		{
			if(updatedString == null)       //for the first iteration of the loop...
			{
				currentChar = oldString.charAt(i);
				if(currentChar == letter)
					updatedString = (replacement);   //assign updatedString the value of replacement
				else updatedString = ""+ currentChar;//assign updatedString the value of currentChar
			}
			else{ //at least 2nd or more iterations if u reached here
				currentChar = oldString.charAt(i);
				if(currentChar == letter)
					updatedString = (updatedString + replacement);//add to the string (updatedString), the replacement line
				else updatedString = (updatedString + currentChar);//add to the string (updatedString), the currentChar
			}
		}//done with the loop
		return updatedString;
	}//close the myReplace method

	public void actionPerformed(ActionEvent e)
	{
		//when the iteration is changed on the menu by a user, get it and assign it to userSelectedIterations
		userSelectedIterations = iterMenu.getSelectedIndex();
		//when the system is changed on the menu by a user, get it and assign it to userSelectedSystem
		userSelectedSystem = selectSystemMenu.getSelectedIndex();
		//System.out.println(selectSystemMenu.getSelectedIndex());
		//System.out.println(iterMenu.getSelectedIndex());
		repaint();
	}

	public void stateChanged(ChangeEvent e)
	{
		userSelectedScale = scaler.getValue();
		repaint();
	}
	
	public void readSystemName()
	{
		 int d=0;
			try 
			{
				BufferedReader reader = new BufferedReader(new FileReader("C:/Users/Ketan/Desktop/Master Project/MS PROJECT/bin/input.txt"));
			
			    String line=null;
			    while (reader.readLine()!=null)
			    {
			    	line = reader.readLine();
			    	if(line.startsWith("Name:"))
			    	{
			    		systemName[d]=line;
			    		System.out.println("Printing from an array: " + systemName[d]);
			    		d++;
			    	}
			    		
			    }
			} catch (FileNotFoundException ex) {
			      ex.printStackTrace();
			      } catch (IOException ex) {
			      ex.printStackTrace();
			      }
		   
	}
	
	
	
	
	
}