InkPanel.java

00001 package edu.stanford.hci.r3.components;
00002 
00003 import java.awt.Color;
00004 import java.awt.Graphics;
00005 import java.awt.Graphics2D;
00006 import java.awt.geom.AffineTransform;
00007 import java.util.Collections;
00008 import java.util.LinkedList;
00009 import java.util.List;
00010 
00011 import javax.swing.JPanel;
00012 
00013 import edu.stanford.hci.r3.pen.ink.Ink;
00014 import edu.stanford.hci.r3.render.ink.InkRenderer;
00015 import edu.stanford.hci.r3.util.graphics.GraphicsUtils;
00016 
00030 public class InkPanel extends JPanel {
00031 
00032         private Ink currentInk;
00033 
00037         private double inkScale = 1.0;
00038 
00043         protected List<Ink> inkWell = Collections.synchronizedList(new LinkedList<Ink>());
00044 
00045         private boolean invertInkColors = false;
00046 
00050         private InkRenderer renderer;
00051 
00055         public InkPanel() {
00056                 this(new InkRenderer(), Color.WHITE);
00057         }
00058 
00065         public InkPanel(InkRenderer inkRenderer, Color bgColor) {
00066                 setBackground(bgColor);
00067                 renderer = inkRenderer;
00068         }
00069 
00073         public void addInk(Ink ink) {
00074                 currentInk = ink;
00075                 inkWell.add(ink);
00076                 repaint();
00077         }
00078 
00082         public Ink addNewInk() {
00083                 currentInk = new Ink();
00084                 inkWell.add(currentInk);
00085                 repaint();
00086                 return currentInk;
00087         }
00088 
00092         public void clear() {
00093                 inkWell.clear();
00094                 repaint();
00095         }
00096 
00100         public void displayInvertedInkColor() {
00101                 invertInkColors = true;
00102         }
00103 
00107         public List<Ink> getAllInk() {
00108                 return inkWell;
00109         }
00110 
00111         public double getScale() {
00112                 return inkScale;
00113         }
00114 
00120         @Override
00121         protected void paintComponent(Graphics g) {
00122                 super.paintComponent(g);
00123                 final Graphics2D g2d = (Graphics2D) g;
00124                 g2d.setRenderingHints(GraphicsUtils.getBestRenderingHints());
00125 
00126                 final AffineTransform transform = g2d.getTransform();
00127                 g2d.scale(inkScale, inkScale);
00128 
00129                 // the drawing of ink is "atomic" with respect to
00130                 // adding and removing from the inkWell
00131                 synchronized (inkWell) {
00132                         for (Ink ink : inkWell) {
00133                                 renderer.setInk(ink);
00134                                 renderer.useInvertedInkColors();
00135                                 renderer.renderToG2D(g2d);
00136                         }
00137                 }
00138                 g2d.setTransform(transform);
00139         }
00140 
00144         public void removeLastBatchOfInk() {
00145                 synchronized (inkWell) {
00146                         if (inkWell.size() > 0) {
00147                                 inkWell.remove(inkWell.size() - 1);
00148                                 repaint();
00149                         }
00150                 }
00151         }
00152 
00156         public void setAllInk(List<Ink> theInk) {
00157                 inkWell = theInk;
00158                 repaint();
00159         }
00160 
00164         public void setRenderer(InkRenderer r) {
00165                 renderer = r;
00166         }
00167 
00171         public void setScale(double theScale) {
00172                 inkScale = theScale;
00173         }
00174 }

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