📄 invisoobjectholder.java
字号:
/* * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is * described in this document. In particular, and without limitation, these intellectual property rights may * include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents * or pending patent applications in the U.S. and in other countries. * * U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. * standard license agreement and applicable provisions of the FAR and its supplements. * * Use is subject to license terms. * * This distribution may include materials developed by third parties. Sun, Sun Microsystems, the Sun logo and * Java are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. * * Copyright (c) 2006 Sun Microsystems, Inc. Tous droits r?serv?s. * * Sun Microsystems, Inc. d?tient les droits de propri?t? intellectuels relatifs ? la technologie incorpor?e dans * le produit qui est d?crit dans ce document. En particulier, et ce sans limitation, ces droits de propri?t? * intellectuelle peuvent inclure un ou plus des brevets am?ricains list?s ? l'adresse http://www.sun.com/patents * et un ou les brevets suppl?mentaires ou les applications de brevet en attente aux Etats - Unis et dans les * autres pays. * * L'utilisation est soumise aux termes du contrat de licence. * * Cette distribution peut comprendre des composants d?velopp?s par des tierces parties. * Sun, Sun Microsystems, le logo Sun et Java sont des marques de fabrique ou des marques d?pos?es de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */package com.sun.spot.spotworld.gridview;import java.awt.Component;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import java.util.Vector;/** * Used to support mouse dragging of many objects at once, keeping their relative position in tact.This obejct is *large enough to hold the object and all its loose pieces. It then deletes itslef when the mouse button is released and the objects fall back into the view. * * @author randy */public class InvisoObjectHolder extends GVTangibleObject implements KeyListener,MouseMotionListener{ /* * The drag offsets are the offsets to maintain for moving an object. */ protected int dragOffsetX; protected int dragOffsetY; /* * The originator is the object that created this InvisoHolder. */ GVObject originator; /* * * Initialization functions. * */ /** * Perform initialization tasks and register self as a key listener. */ public void init() { super.init(); this.addKeyListener(this); } /* * * Drawing & UI functions. * */ public void paintComponent(Graphics g) { /* * This component is invisible: * We paint nothing on the screen. */// PLEASE LEAVE THE FOLLOWING COMMENTS IN PLACE -- USEFUL TO UNCOMMENT FOR TESTING -- Thanks, Randy.// Graphics2D g2 = (Graphics2D)g;// g2.setColor(new Color(128, 45, 73));// g2.drawRect(0,1, getWidth()-1, getHeight()-1); } /** * @return the layer that this InvisoHolder requests in the world */ public int getLayerInView() { return 5; } /** * Primary function of this holder is to wrap an object AND * all its loosePieces so they can be draggeed around the screen together. * */ public void addAndEncompass(GVObject go){ double goX, goY; int rX, rY; Rectangle rect = go.getBounds(); if(go instanceof GVTangibleObject){ GVTangibleObject to = (GVTangibleObject)go; Vector<GVObject> lp = to.getAllLoosePieces(); for(GVObject go2 : lp){ rect = rect.union(go2.getBounds()); } for(GVObject go2 : lp){ goX = go2.getLocation().getX(); goY = go2.getLocation().getY(); go2.setLocation((int)(goX - rect.getX()), (int)(goY - rect.getY())); add(go2); } } setBounds(rect); goX = go.getLocation().getX(); goY = go.getLocation().getY(); go.setLocation((int)(goX - rect.getX()), (int)(goY - rect.getY())); add(go); } /* * Sometimes, while this object is being dragged, one of its components * will have moved or resized so as to go beyond our bounds. This is a * chance to grow and/or move as necessary to keep all the components visually * in the proper place on the screen. */ public void stretchIfNecessary(){ Component[] comps = getComponents(); int minX = 0; int minY = 0; for(Component c : comps){ int cX = c.getX(); int cY = c.getY(); if(cX < minX) minX = cX; if(cY < minY) minY = cY; } if(minX < 0 || minY < 0){ /* Have to move to re-contain the entire bounds */ setLocation(getX() + minX, getY() + minY); //nudged enough to the left for(Component c : comps){ c.setLocation(c.getX()-minX, c.getY() - minY); //See how the component c stays in place on the screen? } } int maxX = getWidth(); int maxY = getHeight(); for(Component c : comps){ int cX = c.getX() + c.getWidth(); int cY = c.getY() + c.getHeight(); if(cX > maxX) maxX = cX; if(cY > maxY) maxY = cY; } if(maxX > getWidth() || maxY > getHeight() ){ /* Have to stretch to re-contain everyone's bounds */ setSize(maxX, maxY); } } /* * * Mouse Events * */ public void mouseDragged(MouseEvent e) { int deltaX = (e.getX() - dragOffsetX); int deltaY = (e.getY() - dragOffsetY); for(Component c : getComponents()){ if(c instanceof GVObject){ ((GVObject)c).followDrag(deltaX, deltaY); } } setLocation(getX() + deltaX, getY() + deltaY); stretchIfNecessary(); repaint(); } /* * * Originator manipulator functions. * */ /** * @return the originator for this InvisoHolder */ public GVObject getOriginator() { return originator; } /** * @param originator the new originator for this InvisoHolder */ public void setOriginator(GVObject originator) { this.originator = originator; } public void mouseButton1Released(MouseEvent e){ //Drop everything int pX = getX(); int pY = getY(); Component[] comps = getComponents(); for (int i = 0; i < comps.length; i++) { Component c = comps[i]; dropComponent(c, pX, pY); } GridView currentView = getView(); currentView.remove(this); currentView.repaint(); //after a remove, you would think there would be a repaint call, but no! } /* * This holder is done carrying the compnent around. Add it back into the world. * The args sepcify the location of this, so that they act as an offset fo the component. */ public void dropComponent(Component c, int offsetX, int offsetY){ c.setLocation(c.getX() + offsetX, c.getY() + offsetY); if(c instanceof GVObject && (c == originator)){ ((GVObject)c).dropIntoView(getView()); } c.repaint(); } /** * @param x the x offset to use * @param y the y offset to use */ public void setDragOffset(int x, int y) { dragOffsetX = x; dragOffsetY = y; } /* * * Key Events * */ /** * Checks for a delete or 'cut' key press. */ public void keyPressed(KeyEvent e) { boolean delete = (e.getKeyCode() == KeyEvent.VK_DELETE) || (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) || (e.getKeyCode() == KeyEvent.VK_CUT); if(delete) { //stopDrag(); removeFromView(); repaint(); } } /** * {@inheritDoc} */ public void keyReleased(KeyEvent arg0) { } /** * {@inheritDoc} */ public void keyTyped(KeyEvent arg0) { } boolean isDragging() { return false; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -