graphics.setClip(null);
public int print(java.awt.Graphics graphics, java.awt.print.PageFormat pageFormat, int pageIndex) throws java.awt.print.PrinterException {		graphics.translate((int)pageFormat.getImageableX(),					   (int)pageFormat.getImageableY());	int pageWidth = (int)pageFormat.getImageableWidth();	int pageHeight = (int)pageFormat.getImageableHeight();		/*------------------------------------------------------------*/		ViewState view = ViewState.getInstance();	double printScale = 1.0;			double screen2printer = 0;	switch (Page.getPrintMode()) {		case Page.TRUE_SCALE:			// 1:1 ratio			screen2printer = 72.0/getWorkspacePanel().getToolkit().getScreenResolution();			printScale = 1.0;			break;					case Page.USER_SCALE:			screen2printer = 72.0/getWorkspacePanel().getToolkit().getScreenResolution();			screen2printer *= Page.getUserScale();			printScale = Page.getUserScale();			break;					case Page.FIT_SCALE:			// fit to paper			double xscale = pageWidth/(double)view.getViewWidth();			double yscale = pageHeight/(double)view.getViewHeight();			screen2printer = Math.min(xscale, yscale)*view.getScale();						printScale = screen2printer / 72.0 * getWorkspacePanel().getToolkit().getScreenResolution();			break;	}		double converter = screen2printer/view.getScale();	int w = (int)(view.getViewWidth()*converter);	int h = (int)(view.getViewHeight()*converter);	if (w==0 || h==0)		return NO_SUCH_PAGE;	if (Page.getPrintMode()==Page.FIT_SCALE)	{		// center (surely one page)		graphics.translate((pageWidth-w)/2,						   (pageHeight-h)/2);	}			int nCol = Math.max((int)Math.ceil((double)w/pageWidth), 1);	int nRow = Math.max((int)Math.ceil((double)h/pageHeight), 1);	int maxNumPage = nCol * nRow;	if (pageIndex>=maxNumPage)		return NO_SUCH_PAGE;	int iCol = pageIndex % nCol;	int iRow = pageIndex / nCol;	int x = iCol * pageWidth;	int y = iRow * pageHeight;	int imageWidth = Math.min(pageWidth, w-x);	int imageHeight = Math.min(pageHeight, h-y);		graphics.clipRect(0, 0, imageWidth, imageHeight);		int rx = view.getRx(); int ry = view.getRy();	double scale = view.getScale();	int viewWidth = view.getViewWidth(), viewHeight = view.getViewHeight();	AffineTransform transf = ((Graphics2D)graphics).getTransform();	FontMetricsBuffer fmb = FontMetricsBuffer.getInstance();	try	{		((Graphics2D)graphics).scale(screen2printer, screen2printer);				printing = true;						view.setScale(1.0);		view.setRx((int)(rx/scale+x));		view.setRy((int)(ry/scale+y));		view.setViewWidth((int)(imageWidth/screen2printer));		view.setViewHeight((int)(imageHeight/screen2printer));				FontMetricsBuffer.setInstance(new FontMetricsBuffer(graphics));			/*Shape clip = graphics.getClip();		graphics.setClip(null);		graphics.setColor(Color.black);		graphics.drawRect(0,0,view.getViewWidth(), view.getViewHeight());		graphics.setClip(clip);*/				if (Settings.getInstance().getShowGrid())		{			graphics.setColor(Constants.GRID_COLOR);			int gridSize = view.getGridSize();			int sx = view.getGridSize() - view.getRx() % gridSize;			int y0 = view.getGridSize() - view.getRy() % gridSize;			int xsteps = view.getViewWidth() / gridSize + 1;			int ysteps = view.getViewHeight() / gridSize + 1;				if (gridSize >= 15)			// crosses			for (int gy=0; gy < ysteps; gy++) {				int x0 = sx;				for (int gx=0; gx < xsteps; gx++) {					graphics.drawLine(x0-1, y0-1, x0+1, y0+1);					graphics.drawLine(x0-1, y0+1, x0+1, y0-1);					x0+=gridSize;				}				y0+=gridSize;			}			else			// dots			for (int gy=0; gy < ysteps; gy++) {				int x0 = sx;				for (int gx=0; gx < xsteps; gx++) {					graphics.drawLine(x0, y0, x0, y0);					x0+=gridSize;				}				y0+=gridSize;			}		}		// change color sheme 		loadBlackOnWhiteColorScheme();		viewGroup.paintComponents(graphics, false, isFlat());		//resets clipping		((Graphics2D)graphics).setTransform(transf);		if (Page.getPrintMode()==Page.FIT_SCALE) graphics.translate(-(pageWidth-w)/2, -(pageHeight-h)/2);		graphics.setClip(null);				//prints legend		if ((Settings.getInstance().getLegendVisibility()==1 && pageIndex==0) || Settings.getInstance().getLegendVisibility()==2) {			if (Settings.getInstance().getLegendVisibility()==1) { // correct navigator rect				view.setViewWidth(viewWidth);				view.setViewHeight(viewHeight);			}			printLegend(graphics, pageWidth, pageHeight, pageIndex+1, maxNumPage, printScale);		}		}	catch (Exception e)	{		e.printStackTrace();		com.cosylab.vdct.Console.getInstance().println("Error while generating print image: "+e);	}	finally	{//		resets clipping		((Graphics2D)graphics).setTransform(transf);		graphics.setClip(null);				view.setScale(scale);		view.setRx(rx); view.setRy(ry);		view.setViewWidth(viewWidth); view.setViewHeight(viewHeight);				FontMetricsBuffer.setInstance(fmb);		//		restore color sheme 		 loadWhiteOnBlackColorScheme();		 		 printing = false;	}		/*------------------------------------------------------------*/		System.gc();		return PAGE_EXISTS;}