1 17 18 19 20 package org.apache.fop.render.awt.viewer; 21 22 import java.awt.Dimension ; 23 import java.awt.Graphics ; 24 import java.awt.Insets ; 25 import java.awt.image.BufferedImage ; 26 import java.lang.ref.Reference ; 27 import java.lang.ref.SoftReference ; 28 import javax.swing.JPanel ; 29 30 import org.apache.fop.apps.FOPException; 31 import org.apache.fop.render.awt.AWTRenderer; 32 33 42 public class ImageProxyPanel extends JPanel { 43 44 45 private Reference imageRef; 46 47 48 private Dimension size; 49 50 51 private AWTRenderer renderer; 52 53 54 private int page; 55 56 61 public ImageProxyPanel(AWTRenderer renderer, int page) { 62 this.renderer = renderer; 63 this.page = page; 64 setOpaque(false); 67 } 68 69 72 public Dimension getMinimumSize() { 73 return getPreferredSize(); 74 } 75 76 79 public Dimension getPreferredSize() { 80 if (size == null) { 81 try { 82 Insets insets = getInsets(); 83 size = renderer.getPageImageSize(page); 84 size = new Dimension (size.width + insets.left + insets.right, 85 size.height + insets.top + insets.bottom); 86 } catch (FOPException fopEx) { 87 return new Dimension (10, 10); 89 } 90 } 91 return size; 92 } 93 94 98 public void setPage(int pg) { 99 if (page != pg) { 100 page = pg; 101 imageRef = null; 102 repaint(); 103 } 104 } 105 106 113 public synchronized void paintComponent(Graphics graphics) { 114 try { 115 if (isOpaque()) { graphics.setColor(getBackground()); 117 graphics.fillRect(0, 0, getWidth(), getHeight()); 118 } 119 120 super.paintComponent(graphics); 121 122 BufferedImage image = null; 123 if (imageRef == null || imageRef.get() == null) { 124 image = renderer.getPageImage(page); 125 imageRef = new SoftReference (image); 126 } else { 127 image = (BufferedImage )imageRef.get(); 128 } 129 130 int x = (getWidth() - image.getWidth()) / 2; 131 int y = (getHeight() - image.getHeight()) / 2; 132 133 graphics.drawImage(image, x, y, image.getWidth(), image.getHeight(), null); 134 } catch (FOPException fopEx) { 135 fopEx.printStackTrace(); 136 } 137 } 138 } 139 | Popular Tags |