DocumentPanel.java

00001 package edu.stanford.hci.r3.tools.design.gui;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.Color;
00005 import java.awt.Dimension;
00006 import java.awt.GradientPaint;
00007 import java.awt.Graphics;
00008 import java.awt.Graphics2D;
00009 import java.awt.Rectangle;
00010 
00011 import javax.swing.JFrame;
00012 import javax.swing.JPanel;
00013 import javax.swing.JScrollPane;
00014 
00015 import edu.stanford.hci.r3.paper.Sheet;
00016 import edu.stanford.hci.r3.units.Pixels;
00017 import edu.stanford.hci.r3.util.MathUtils;
00018 import edu.stanford.hci.r3.util.graphics.GraphicsUtils;
00019 
00032 public class DocumentPanel extends JPanel {
00033 
00037         private static final Color BG_BEGIN_COLOR = new Color(114, 121, 139);
00038 
00042         private static final Color BG_END_COLOR = new Color(167, 177, 202);
00043 
00047         public static final int DROP_SHADOW_DISTANCE = 4;
00048 
00049         private static final int MIN_PADDING_HORIZ = 20;
00050 
00051         private static final int MIN_PADDING_VERT = 25;
00052 
00056         public static final Color SHADOW_COLOR = new Color(0, 0, 0, 99);
00057 
00061         private int paddingHorizontal = MIN_PADDING_HORIZ;
00062 
00066         private int paddingVertical = MIN_PADDING_VERT;
00067 
00068         private JFrame parentFrame;
00069 
00070         private JScrollPane parentScrollPane;
00071 
00072         final Pixels pixelsReferenceUnit = new Pixels();
00073 
00077         private Sheet sheet = new Sheet(8.5, 11);
00078 
00082         public DocumentPanel() {
00083                 setBackground(Color.WHITE);
00084                 setLayout(new BorderLayout());
00085         }
00086 
00093         private void drawGradientBackground(Graphics2D g2d) {
00094                 final GradientPaint gradient = new GradientPaint(0, 0, BG_BEGIN_COLOR, 0, getHeight(),
00095                                 BG_END_COLOR, false);
00096                 g2d.setPaint(gradient);
00097                 g2d.fillRect(0, 0, getWidth(), getHeight());
00098         }
00099 
00105         public int getDefaultHorizontalScreenPadding() {
00106                 return 2 * paddingHorizontal;
00107         }
00108 
00114         public int getDefaultVerticalScreenPadding() {
00115                 return 2 * paddingVertical;
00116         }
00117 
00118         private int getDisplayedDocumentHeight() {
00119                 return MathUtils.rint(sheet.getHeight().getValueIn(pixelsReferenceUnit));
00120         }
00121 
00122         private int getDisplayedDocumentWidth() {
00123                 return MathUtils.rint(sheet.getWidth().getValueIn(pixelsReferenceUnit));
00124         }
00125 
00129         public Dimension getPreferredSize() {
00130                 return new Dimension(getDisplayedDocumentWidth() + getDefaultHorizontalScreenPadding()
00131                                 - parentScrollPane.getVerticalScrollBar().getWidth(), getDisplayedDocumentHeight()
00132                                 + getDefaultVerticalScreenPadding()
00133                                 - parentScrollPane.getHorizontalScrollBar().getHeight());
00134         }
00135 
00143         protected void paintComponent(Graphics g) {
00144                 // do whatever JPanel/JComponent likes to do...
00145                 super.paintComponent(g);
00146 
00147                 final Graphics2D g2d = (Graphics2D) g;
00148                 g2d.setRenderingHints(GraphicsUtils.getBestRenderingHints());
00149 
00150                 // smooth gradient
00151                 drawGradientBackground(g2d);
00152 
00153                 // convert this to pixels
00154                 int widthPixels = getDisplayedDocumentWidth();
00155                 int heightPixels = getDisplayedDocumentHeight();
00156                 // System.out.println("Sheet size is: " + sheet.getWidth() + ", " + sheet.getHeight());
00157                 // System.out.println("Sheet size is: " + widthPixels + ", " + heightPixels);
00158 
00159                 int panelWidth = parentFrame.getWidth();
00160                 int panelHeight = parentScrollPane.getHeight();
00161 
00162                 paddingHorizontal = Math.max(MathUtils.rint((panelWidth - widthPixels) / 2.0),
00163                                 MIN_PADDING_HORIZ);
00164                 paddingVertical = Math.max(MathUtils.rint((panelHeight - heightPixels) / 2.0),
00165                                 MIN_PADDING_VERT);
00166 
00167                 // draw the jpeg image of the sheet...
00168 
00169                 // the dark drop shadow
00170                 g2d.setColor(SHADOW_COLOR);
00171                 final Rectangle docBounds = new Rectangle(paddingHorizontal, paddingVertical, widthPixels,
00172                                 heightPixels);
00173                 g2d.fillRect((int) docBounds.getX() + DROP_SHADOW_DISTANCE, (int) docBounds.getY()
00174                                 + DROP_SHADOW_DISTANCE, docBounds.width, docBounds.height);
00175 
00176         }
00177 
00178         public void setParentFrame(JFrame mainFrame) {
00179                 parentFrame = mainFrame;
00180         }
00181 
00182         public void setParentScrollPane(JScrollPane scrollPane) {
00183                 parentScrollPane = scrollPane;
00184         }
00185 }

Generated on Sat Apr 14 18:21:34 2007 for R3 Paper Toolkit by  doxygen 1.4.7