private int paintStr(int i, int sides)
	{
		
		// Try to find an empty space of the string
		Point position = null;	
		Rectangle  bounds = null;
		Shape draw_word = null;
		AffineTransform tx = null;
		boolean found = false;
		Point[] str_vertex = new Point[4];
		while (!found)
		{
			// Set the font
			try 
			{
				font = fontBase.deriveFont(Font.PLAIN, words.get(i).getSize());
			} 
			catch (Exception ex)
			{
				ex.printStackTrace();
			}
			g.setFont(font);
			TextShape textshape = new TextShape(font,words.get(i).getStr());
			draw_word=textshape.getShape();
			tx = new AffineTransform();
			bounds = draw_word.getBounds();
			str_vertex[0] = new Point(bounds.x, bounds.y);
			str_vertex[1] = new Point(bounds.x, bounds.y + bounds.height);
			str_vertex[2] = new Point(bounds.x + bounds.width, bounds.y + bounds.height);
			str_vertex[3] = new Point(bounds.x + bounds.width, bounds.y);
			
			if(Math.random()>0.5) {
				is_rotate=false;
			} else {
				is_rotate=true;
			}
			if(is_rotate) {	
				AffineTransform ax = new AffineTransform();
				ax.rotate(Math.PI/4*Math.random() ,0,0);
				ax.transform(str_vertex, 0, str_vertex, 0, 4);
				draw_word=ax.createTransformedShape(draw_word);
			}
			// Get the bounds of the string
			bounds = draw_word.getBounds();
			for (int j = sides; j > -1; j--)
			{
				position = searchSpace(bounds, j, i, words.get(i).P());
				if (position != null)
				{
					found = true;
					j = -1;
					break;
				}
			}
			
			if (!found) // no space, try to make the word smaller
			{
				words.get(i).setSize(words.get(i).getSize() - 10);
				if (words.get(i).getSize() < font_min) // too small. no space available on the canvas
				{
					System.out.println("Warning! No space available!");
					return 0;		
				}
					
			}
		}
		
		setColor(position,words.get(i));// Set the color of the string which is related to its position
		// Draw the string
		int x = (int) (position.x - bounds.getMinX());
		int y = (int) (position.y - bounds.getMinY());
		
		tx.setToTranslation(x, y);
		tx.transform(str_vertex, 0, str_vertex, 0, 4);
		draw_word=tx.createTransformedShape(draw_word);
		g.fill(draw_word);
		words.get(i).setPoint(x, y);
		int[] str_x = {(int)str_vertex[0].x, (int)str_vertex[1].x, (int)str_vertex[2].x, (int)str_vertex[3].x};
		int[] str_y = {(int)str_vertex[0].y, (int)str_vertex[1].y, (int)str_vertex[2].y, (int)str_vertex[3].y};
		words.get(i).setBounds(new Polygon(str_x, str_y, 4));
		if (update) observer.imageUpdate(img, ImageObserver.ALLBITS, position.x, position.y, bounds.width, bounds.height);
		return 1;	
	}