public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
boolean last = false;
try {
View rootView = displayPane.getUI().getRootView(displayPane);
double scaleX = pageFormat.getImageableWidth() / displayPane.getMinimumSize().getWidth();
scaleX = Math.min(scaleX, 1.0);
double scaleY = scaleX;
int end = (int) (pageIndex * ((1.0f / scaleY) * (double) pageFormat.getImageableHeight()));
Rectangle allocation = new Rectangle(0,
-end,
(int) pageFormat.getImageableWidth(),
(int) pageFormat.getImageableHeight());
((Graphics2D) graphics).scale(scaleX, scaleY);
graphics.setClip((int) (pageFormat.getImageableX() / scaleX),
(int) (pageFormat.getImageableY() / scaleY),
(int) (pageFormat.getImageableWidth() / scaleX),
(int) (pageFormat.getImageableHeight() / scaleY));
((Graphics2D) graphics).translate(((Graphics2D) graphics).getClipBounds().getX(),
((Graphics2D) graphics).getClipBounds().getY());
rootView.paint(graphics, allocation);
last = end > displayPane.getUI().getPreferredSize(displayPane).getHeight();
if ((last)) {
return Printable.NO_SUCH_PAGE;