test 2

foo foo foo
public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException {
if (pageIndex >= mNumPages) {
throw new IndexOutOfBoundsException();
}
BufferedImage image = new BufferedImage(300,5000,BufferedImage.TYPE_INT_RGB);
Graphics2D imageGraphics = (Graphics2D)image.getGraphics();
double originX = (pageIndex % mNumPagesX) * mFormat.getImageableWidth();
//double originY = (pageIndex / mNumPagesX) * mFormat.getImageableHeight();
double originY = 0;
for (int i = 0; i < pageIndex; i++)
{
originY+=heights[i];
}
imageGraphics.translate(-originX/mScaleX, -originY/mScaleY);
//System.out.println("OrigX:"+originX+ " OrigY:"+originY);
//System.out.println("SOrigX:"+originX/mScaleX+ " SOrigY:"+originY/mScaleY);
mComponent.paint(imageGraphics);
Raster raster = image.getData();
DataBuffer db = raster.getDataBuffer();
SampleModel sm = raster.getSampleModel();
int[] pixels = new int[30000];
PageFormat pageFormat = getPageFormat();
//System.out.println("Printing page:"+pageIndex+ " Y: "+ pageFormat.getImageableHeight()/mScaleY);
pixels = sm.getPixels(100,(int) (pageFormat.getImageableHeight()/mScaleY )-50,200,50,pixels,db);
int bestRow = 49;
int bestCount = 0;
for (int row = 49; row >= 0; row--)
{
int count = 0;
for (int col = 0; col < 600; col ++)
{
count += pixels[row*600+col];
}
if (count > bestCount)
{
bestRow = row;
bestCount = count;
}
// System.out.println("Row:"+row +" Count:"+count);
}
/*
* The following print statements are quite
* useful for debugging but slow things down.
*
System.out.println("Best Row:"+bestRow +" Best Count:"+bestCount);
for (int i = 0; i < pixels.length; i+=3)
{
if (i%600 == 0)
{
System.out.print("\n");
if (i/600 == bestRow)
{
System.out.print("***");
}
else
{
if (i/600 < 100) System.out.print("0");
if (i/600 < 10 ) System.out.print("0");
System.out.print(i/600);
}
}
if (pixels[i]==0)
{
System.out.print("X");
}
else
{
System.out.print(" ");
}
}
*/
int diff = 50 - bestRow;
Paper newPaper = (Paper) pageFormat.getPaper().clone();
heights[pageIndex]= (int) (pageFormat.getImageableHeight()-diff*mScaleY);
newPaper.setImageableArea(72,72,pageFormat.getImageableWidth(), pageFormat.getImageableHeight()-diff*mScaleY);
pageFormat.setPaper(newPaper);
//System.out.print("\n");
return pageFormat;
}