1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.pdfviewer; 18 19 import java.awt.Dimension; 20 import java.awt.Graphics; 21 22 import java.io.IOException; 23 24 import javax.swing.JPanel; 25 26 import org.apache.pdfbox.pdmodel.PDPage; 27 28 import org.apache.pdfbox.pdmodel.common.PDRectangle; 29 30 /** 31 * This is a simple JPanel that can be used to display a PDF page. 32 * 33 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 34 * @version $Revision: 1.4 $ 35 */ 36 public class PDFPagePanel extends JPanel 37 { 38 39 private PDPage page; 40 private PageDrawer drawer = null; 41 private Dimension pageDimension = null; 42 43 /** 44 * Constructor. 45 * 46 * @throws IOException If there is an error creating the Page drawing objects. 47 */ 48 public PDFPagePanel() throws IOException 49 { 50 drawer = new PageDrawer(); 51 } 52 53 /** 54 * This will set the page that should be displayed in this panel. 55 * 56 * @param pdfPage The page to draw. 57 */ 58 public void setPage( PDPage pdfPage ) 59 { 60 page = pdfPage; 61 PDRectangle pageSize = page.findMediaBox(); 62 pageDimension = pageSize.createDimension(); 63 setSize( pageDimension ); 64 setBackground( java.awt.Color.white ); 65 } 66 67 /** 68 * {@inheritDoc} 69 */ 70 public void paint(Graphics g ) 71 { 72 try 73 { 74 g.setColor( getBackground() ); 75 g.fillRect( 0, 0, getWidth(), getHeight() ); 76 drawer.drawPage( g, page, pageDimension ); 77 } 78 catch( IOException e ) 79 { 80 e.printStackTrace(); 81 } 82 } 83 }