View Javadoc
1 /***************************************************************************** 2 * Virtual Mockup for Machine Vision 3 * Copyright (C) 2001-2003 Fabio R. de Miranda, João E. Kogler Jr., 4 * Carlos S. Santos. 5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP 6 * 7 * Permission is granted to redistribute and/or modify this 8 * software under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This software is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html) 16 * for more details. 17 * 18 *****************************************************************************/ 19 20 package camera3d.gui; 21 22 import javax.swing.JInternalFrame; 23 import javax.swing.JFrame; 24 import java.awt.*; 25 import java.awt.event.*; 26 import javax.swing.*; 27 import camera3d.*; 28 import camera3d.test.*; 29 30 31 /* 32 * This class, as of its remodelling (06/Dec/2001), displays a CommandPane and a Viewport, 33 * and put them in either a JInternalFrame (that enables the whole component to go 34 * inside a JDesktopPane),or a JFrame, and allows each viewport to have its own Window 35 * At a first moment, It won't be allowed to turn a window from JInternalFrame to JFrame and vice-versa 36 */ 37 38 /*** 39 * Frame to which are attached viewports. 40 * 41 * @author Fábio Roberto de Miranda 42 * @version 1.0 43 * @see camera3d.Viewport 44 */ 45 public class ViewportFrame { 46 47 /*** Frame used when ViewportFrame is shown as an internal window */ 48 JInternalFrame innerFrame; 49 /*** Frame used when ViewportFrame is shown as an outer window */ 50 JFrame outerFrame; 51 /*** Pane which holds the Viewport object. When ViewportFrame switches between the 52 * internal/external states, viewportPane is passed from innerFrame to outerFrame 53 * (or vice-versa). */ 54 ViewportPane viewportPane; 55 56 /*** Label which shows the mouse position */ 57 private JLabel cursorLabel; 58 /* This is a mouse listener attached to the Canvas3D object and used to print 59 the mouse coordinates to cursorLabel 60 */ 61 private InternalListener internalListener; 62 63 private static int openFrameCount = 0; 64 private static final int xOffset = 30, yOffset = 30; 65 66 private ViewportContextMenu contextMenu; 67 68 // Discovering if this object will be based on a JFrame or a JInternalFrame 69 private boolean isInternalFrame; 70 71 ViewportCommandPane commandPane; 72 73 private BorderLayout borderLayout1 = new BorderLayout(); 74 75 ViewportFrame(Viewport viewport, ViewportCommandPane commandPane){ 76 this(); 77 viewportPane = new ViewportPane(viewport, commandPane); 78 contextMenu = new ViewportContextMenu(this); 79 cursorLabel = new JLabel("Cursor Label"); 80 viewportPane.add(cursorLabel,BorderLayout.NORTH); 81 82 internalListener = new InternalListener(); 83 Viewport tempVp = viewportPane.getViewport(); 84 if(tempVp!=null){ 85 tempVp.getCanvas3D().addMouseListener(internalListener); 86 tempVp.getCanvas3D().addMouseMotionListener((MouseMotionListener)internalListener); 87 } 88 if (isInternalFrame){ 89 if (commandPane == null){ 90 debugln("CommandPane is null."); 91 } 92 innerFrame.getContentPane().add(viewportPane, BorderLayout.CENTER); 93 innerFrame.setVisible(true); 94 innerFrame.show(); 95 }else { 96 if (commandPane == null){ 97 debugln("CommandPane is null."); 98 } 99 outerFrame.getContentPane().add(viewportPane, BorderLayout.CENTER); 100 outerFrame.setVisible(true); 101 outerFrame.show(); 102 } 103 104 this.commandPane = commandPane; 105 } 106 107 public ViewportFrame(Viewport viewport){ 108 this(viewport, new ViewportCommandPane()); 109 } 110 111 112 113 public ViewportFrame() { 114 this.isInternalFrame = GUIControl.viewportsInJInternalFrames; 115 116 innerFrame = new JInternalFrame("Viewport #" + (++openFrameCount), 117 true, //resizable 118 true, //closable 119 true, //maximizable 120 true);//iconifiable 121 outerFrame = new JFrame("Viewport #" + (++openFrameCount)); 122 if (isInternalFrame){ 123 innerFrame.setSize(200, 200); 124 innerFrame.setLocation(xOffset*openFrameCount, yOffset*openFrameCount); 125 innerFrame.getContentPane().setLayout(borderLayout1); 126 innerFrame.setBackground(Color.red); 127 if (commandPane == null){ 128 debugln("CommandPane is null."); 129 } 130 } else { 131 outerFrame.setResizable(true); 132 outerFrame.setSize(200, 200); 133 outerFrame.getContentPane().setLayout(borderLayout1); 134 outerFrame.setVisible(true); 135 outerFrame.setBackground(Color.green); 136 outerFrame.show(); 137 if (commandPane == null){ 138 debugln("CommandPane is null."); 139 } 140 141 } 142 143 try { 144 jbInit(); 145 } 146 catch(Exception e) { 147 e.printStackTrace(); 148 } 149 150 } 151 152 public Viewport getViewport(){ 153 return viewportPane.getViewport(); 154 } 155 156 157 private void jbInit() throws Exception { 158 } 159 160 void debugln(String s){ 161 if (GUIControl.debugflag){ 162 System.out.println(s); 163 } 164 } 165 166 167 public boolean isInternalFrame(){ 168 return this.isInternalFrame; 169 } 170 171 public java.awt.Dimension getSize(){ 172 if (isInternalFrame()){ 173 return innerFrame.getSize(); 174 } else { 175 return outerFrame.getSize(); 176 } 177 } 178 179 public void setSize(int width, int height){ 180 if (isInternalFrame()){ 181 innerFrame.setSize(width, height); 182 } else { 183 outerFrame.setSize(width, height); 184 } 185 } 186 187 public java.awt.Point getPosition(){ 188 if (isInternalFrame()){ 189 return innerFrame.getLocation(); 190 } else { 191 return outerFrame.getLocation(); 192 } 193 } 194 195 public void toFront(){ 196 if(isInternalFrame()){ 197 innerFrame.toFront(); 198 }else{ 199 outerFrame.toFront(); 200 } 201 } 202 203 public void setPosition(int x, int y){ 204 if (isInternalFrame()){ 205 innerFrame.setLocation(x, y); 206 } else { 207 outerFrame.setLocation(x, y); 208 } 209 } 210 211 public void maximize(boolean maximum){ 212 if (isInternalFrame()){ 213 try { 214 innerFrame.setMaximum(true); 215 } 216 catch (Exception ex) { 217 218 } 219 } else { 220 //outerFrame. 221 } 222 } 223 224 public void setInternalFrame(boolean value){ 225 this.isInternalFrame = value; 226 } 227 228 public JInternalFrame getJInternalFrame(){ 229 return this.innerFrame; 230 } 231 232 public void changeToJInternalFrame(){ 233 if (isInternalFrame()){ 234 } else { 235 if (innerFrame==null){ 236 System.out.println("InnerFrame was null. Recreating it"); 237 innerFrame = new JInternalFrame("Viewport #" + (++openFrameCount), 238 true, //resizable 239 true, //closable 240 true, //maximizable 241 true);//iconifiable 242 } 243 outerFrame.setVisible(false); 244 innerFrame.setSize(outerFrame.getWidth(), outerFrame.getHeight()); 245 int x = outerFrame.getX(); 246 int y = outerFrame.getY(); 247 innerFrame.setLocation(xOffset*openFrameCount, yOffset*openFrameCount); 248 249 innerFrame.getContentPane().setLayout(borderLayout1); 250 innerFrame.setBackground(Color.red); 251 252 outerFrame.getContentPane().remove(viewportPane); 253 innerFrame.getContentPane().add(viewportPane, BorderLayout.CENTER); 254 innerFrame.setVisible(true); 255 innerFrame.show(); 256 if (commandPane == null){ 257 debugln("CommandPane is null."); 258 } 259 /*** 260 try { 261 innerFrame.setMaximum(false); 262 } 263 catch (java.beans.PropertyVetoException ex) { 264 System.out.println("ViewportFrame: Could not restore frame."); 265 } 266 */ 267 268 this.setInternalFrame(true); 269 } 270 } 271 272 public void changeToJFrame(){ 273 if (isInternalFrame()){ 274 outerFrame.setSize(innerFrame.getWidth(), innerFrame.getHeight()); 275 innerFrame.setVisible(false); 276 innerFrame.getContentPane().remove(viewportPane); 277 outerFrame.getContentPane().add(viewportPane, BorderLayout.CENTER); 278 outerFrame.setVisible(true); 279 outerFrame.show(); 280 this.setInternalFrame(false); 281 } else { 282 } 283 } 284 285 public void setFrameIcon(ImageIcon icon){ 286 if (innerFrame!=null){ 287 this.innerFrame.setFrameIcon(icon); 288 } 289 if (outerFrame!=null){ 290 this.outerFrame.setIconImage(icon.getImage()); 291 } 292 } 293 294 295 public void setVisible(boolean visible){ 296 if (isInternalFrame()) { 297 innerFrame.setVisible(true); 298 } else { 299 outerFrame.setVisible(true); 300 } 301 } 302 303 public void setViewportsEventHandler(ViewportsEventHandler handler){ 304 handler.addViewportFrame(this); 305 contextMenu.setViewportEventsHandler(handler); 306 } 307 308 public void setViewportContextMenu(ViewportContextMenu menu){ 309 this.contextMenu = menu; 310 } 311 312 public ViewportContextMenu getViewportContextMenu(){ 313 return this.contextMenu; 314 } 315 316 public ViewportCommandPane getViewportCommandPane(){ 317 return this.commandPane; 318 } 319 320 /* Code pasted from Java's PopupDemo.java */ 321 /* included here to allow printing mouse coordinates on screen */ 322 class InternalListener extends MouseAdapter implements MouseMotionListener { 323 //public void mousePressed(MouseEvent e) { } 324 325 //public void mouseReleased(MouseEvent e) {} 326 327 public void mouseEntered(MouseEvent e){ 328 cursorLabel.setText("X:"+e.getX()+" Y:"+e.getY()); 329 } 330 331 public void mouseExited(MouseEvent e){ 332 cursorLabel.setText("X: Y:"); 333 } 334 335 // MouseMotionListener methods 336 public void mouseMoved(MouseEvent e){ 337 cursorLabel.setText("X:"+e.getX()+" Y:"+e.getY()); 338 } 339 340 public void mouseDragged(MouseEvent e){ 341 cursorLabel.setText("X:"+e.getX()+" Y:"+e.getY()); 342 } 343 } 344 345 346 347 348 }

This page was automatically generated by Maven