/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pdfbox.tools.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import java.io.IOException;

import javax.swing.JPanel;

import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.pdmodel.PDPage;

import org.apache.pdfbox.pdmodel.common.PDRectangle;

/**
 * This is a simple JPanel that can be used to display a PDF page.
 *
 * @author Ben Litchfield
 */
public class PDFPagePanel extends JPanel
{
    private static final long serialVersionUID = -4629033339560890669L;

    private PDFRenderer renderer;
    private int pageNum;
    private Dimension pageDimension = null;
    private Dimension drawDimension = null;

    /**
     * This will set the page that should be displayed in this panel.
     *
     * @param renderer The renderer to render the page.
     * @param page The PDF page to display.
     * @param pageNum The number of the page.
     * @throws IOException if something goes wrong.
     */
    public void setPage(PDFRenderer renderer, PDPage page, int pageNum) throws IOException
    {
        this.renderer = renderer;
        this.pageNum = pageNum;

        PDRectangle cropBox = page.getCropBox();
        drawDimension = new Dimension((int)cropBox.getWidth(), (int)cropBox.getHeight());
        int rotationAngle = page.getRotation();
        if (rotationAngle == 90 || rotationAngle == 270)
        {
            pageDimension = new Dimension(drawDimension.height, drawDimension.width);
        }
        else
        {
            pageDimension = drawDimension;
        }
        setSize(pageDimension);
        setBackground(java.awt.Color.white);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void paint(Graphics g)
    {
        try
        {
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
            renderer.renderPageToGraphics(pageNum, (Graphics2D) g);
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }
}

@@ -834,16 +834,39 @@
 s.gui;%0A%0A
+import java.awt.Color;%0A
 import j
@@ -884,16 +884,16 @@
 ension;%0A
-
 import j
@@ -1422,96 +1422,8 @@
 Num;
-%0A    private Dimension pageDimension = null;%0A    private Dimension drawDimension = null;
 %0A%0A  
@@ -1932,16 +1932,26 @@
         
+Dimension 
 drawDime
@@ -1973,24 +1973,25 @@
 ension((int)
+ 
 cropBox.getW
@@ -2003,16 +2003,17 @@
 ), (int)
+ 
 cropBox.
@@ -2018,32 +2018,65 @@
 x.getHeight());%0A
+        Dimension pageDimension;%0A
         int rota
@@ -2378,16 +2378,16 @@
 nsion);%0A
+
         
@@ -2400,25 +2400,16 @@
 kground(
-java.awt.
 Color.wh

change private fields to local variables