DisplayGraphicsAction.java

00001 package edu.stanford.hci.r3.actions.types;
00002 
00003 import java.awt.Color;
00004 import java.awt.Graphics;
00005 import java.awt.Graphics2D;
00006 import java.awt.Point;
00007 import java.awt.Shape;
00008 import java.awt.geom.AffineTransform;
00009 import java.io.File;
00010 import java.util.ArrayList;
00011 import java.util.Collections;
00012 import java.util.List;
00013 
00014 import javax.swing.JFrame;
00015 import javax.swing.JPanel;
00016 
00017 import edu.stanford.hci.r3.actions.R3Action;
00018 import edu.stanford.hci.r3.actions.types.graphicscommands.DrawImageCommand;
00019 import edu.stanford.hci.r3.actions.types.graphicscommands.DrawRectCommand;
00020 import edu.stanford.hci.r3.actions.types.graphicscommands.DrawShape;
00021 import edu.stanford.hci.r3.actions.types.graphicscommands.FillRectCommand;
00022 import edu.stanford.hci.r3.actions.types.graphicscommands.GraphicsCommand;
00023 import edu.stanford.hci.r3.actions.types.graphicscommands.SetColorCommand;
00024 import edu.stanford.hci.r3.util.WindowUtils;
00025 import edu.stanford.hci.r3.util.graphics.GraphicsUtils;
00026 
00042 public class DisplayGraphicsAction implements R3Action {
00043 
00050         public static class LocalDisplay extends JFrame {
00051 
00052                 private static LocalDisplay instance = null;
00053 
00057                 public static LocalDisplay getInstance() {
00058                         if (instance == null) {
00059                                 instance = new LocalDisplay();
00060                         }
00061                         return instance;
00062                 }
00063 
00064                 private List<GraphicsCommand> commands = Collections
00065                                 .synchronizedList(new ArrayList<GraphicsCommand>());
00066 
00067                 private JPanel mainPanel;
00068 
00072                 private LocalDisplay() {
00073                         super("Local Display");
00074                         setLocation(0, 0);
00075                         super.setContentPane(getMainPanel());
00076                         setSize(800, 600);
00077                         WindowUtils.getWindowOrigin(this, WindowUtils.DESKTOP_CENTER);
00078                         setVisible(false);
00079                 }
00080 
00084                 public void addRenderingCommands(List<GraphicsCommand> commandsToAppend) {
00085                         commands.addAll(commandsToAppend);
00086                 }
00087 
00091                 private JPanel getMainPanel() {
00092                         if (mainPanel == null) {
00093                                 mainPanel = new JPanel() {
00094                                         @Override
00095                                         protected void paintComponent(Graphics g) {
00096                                                 super.paintComponent(g);
00097                                                 final Graphics2D g2d = (Graphics2D) g;
00098                                                 g2d.setRenderingHints(GraphicsUtils.getBestRenderingHints());
00099 
00100                                                 // run my commands....
00101                                                 for (GraphicsCommand gcmd : commands) {
00102                                                         gcmd.invoke(g2d);
00103                                                 }
00104                                         }
00105                                 };
00106                         }
00107                         return mainPanel;
00108                 }
00109 
00113                 public void setRenderingCommands(List<GraphicsCommand> commandsToRun) {
00114                         commands.clear();
00115                         addRenderingCommands(commandsToRun);
00116                 }
00117         }
00118 
00122         private List<GraphicsCommand> commandsToRun = new ArrayList<GraphicsCommand>();
00123 
00127         private boolean exitOnCloseFlag;
00128 
00129         private int extendedState = JFrame.NORMAL;
00130 
00131         private int frameHeight = 600;
00132 
00133         private Point frameOrigin;
00134 
00135         private int frameWidth = 800;
00136 
00137         private boolean bringToFront;
00138 
00142         public DisplayGraphicsAction() {
00143 
00144         }
00145 
00151         public void drawImage(File imgFile, int x, int y) {
00152                 commandsToRun.add(new DrawImageCommand(imgFile, x, y));
00153         }
00154 
00161         public void drawImage(File imgFile, int x, int y, double scaleFactor) {
00162                 commandsToRun.add(new DrawImageCommand(imgFile, x, y, AffineTransform.getScaleInstance(
00163                                 scaleFactor, scaleFactor)));
00164         }
00165 
00172         public void drawRect(int x, int y, int w, int h) {
00173                 commandsToRun.add(new DrawRectCommand(x, y, w, h));
00174         }
00175 
00179         public void drawShape(Shape s) {
00180                 commandsToRun.add(new DrawShape(s));
00181         }
00182 
00191         public void fillRect(int x, int y, int w, int h) {
00192                 commandsToRun.add(new FillRectCommand(x, y, w, h));
00193         }
00194 
00200         public void invoke() {
00201                 LocalDisplay display = LocalDisplay.getInstance();
00202                 display.setSize(frameWidth, frameHeight);
00203                 display.setLocation(frameOrigin);
00204                 display.setExtendedState(extendedState);
00205                 if (exitOnCloseFlag) {
00206                         display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00207                 } else {
00208                         display.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
00209                 }
00210 
00211                 display.addRenderingCommands(commandsToRun);
00212                 display.setVisible(true); // if it's already visible, nothing changes
00213                 if (bringToFront) {
00214                         // a hack to bring the window to the top...
00215                         display.setAlwaysOnTop(true);
00216                         display.setAlwaysOnTop(false);
00217                 }
00218         }
00219 
00223         public void maximizeFrame() {
00224                 extendedState = JFrame.MAXIMIZED_BOTH;
00225         }
00226 
00230         public void minimizeFrame() {
00231                 extendedState = JFrame.ICONIFIED;
00232         }
00233 
00237         public void restoreFrame() {
00238                 extendedState = JFrame.NORMAL;
00239         }
00240 
00244         public void setColor(Color col) {
00245                 commandsToRun.add(new SetColorCommand(col));
00246         }
00247 
00251         public void setExitOnClose(boolean flag) {
00252                 exitOnCloseFlag = flag;
00253         }
00254 
00255         public void setFrameLocation(Point windowOrigin) {
00256                 frameOrigin = windowOrigin;
00257         }
00258 
00259         public void setFrameSize(int w, int h) {
00260                 frameWidth = w;
00261                 frameHeight = h;
00262         }
00263 
00264         public void setBringToFront(boolean flag) {
00265                 bringToFront = flag;
00266         }
00267 }

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