📄 gvobject.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 com.sun.spot.spotworld.common.LocaleUtil;import java.util.Iterator;import java.util.Vector;import javax.swing.JComponent;import java.awt.*;import javax.swing.JOptionPane;/** * Root of the class heirarchy for objects tht can live in GridView. * * A GVObject can have many "loosePieces" A loose piece is anotehr GVObject . LoosePieces * appear as independent objects in the view, but when the user grabs an object to carry it * around with the mouse, the user discovers that the loosepices come along too, * retaining their relative position. * * That is, loosePieces appear to be somehow stuck together. Because a GVObject that is a loosePiece can itself have loosePieces, * a loosePiece tree is formed. Each GVObject has at most one looseParent as a back pointer up this tree. * * @author as152207, randy */public class GVObject extends JComponent { private Object viewLock = new Object(); protected GVObject looseParent; //back pointer for the loosePeice relationship. looseParent == this, if this is not a loosePiece. protected Vector<GVObject> loosePieces; private boolean selected = false; public int shadowOffsetX = 15; public int shadowOffsetY = 10; protected Animator currentAnimator; public GVObject() { super(); init(); } public void init() { setSize(getPreferredSize()); loosePieces = new Vector<GVObject>(); looseParent = this; } public void msg(String m){ System.out.println("[" + this + "] " + m); } public int getWidth(){ return isShadowed() ? super.getWidth() + shadowOffsetX : super.getWidth(); } public int getHeight(){ return isShadowed() ? super.getHeight() + shadowOffsetY : super.getHeight(); } /* * basic width and height are the natural width and height you probably have in mind, * as width and height alone account for the need to draw a shadow from time to time. */ public int getBasicWidth(){ return super.getWidth(); } public int getBasicHeight(){ return super.getHeight(); } /** * @return whether or not the object is currently in a view */ public boolean isInView() { return getView() != null; } /* * The location w.r.t the view's x,y coordinate system. */ public Point getLocationInView(){ Component p = getParent(); if(p == null || p == getView() ) return getLocation(); // p is either a GVObject or a GridView, but we have returned already in the latter case GVObject g = (GVObject)p; int xLiv = g.getLocationInView().x + getLocation().x; int yLiv = g.getLocationInView().y + getLocation().y; return new Point(xLiv, yLiv); } public void setLocationInView(int x, int y){ Point liv = getLocationInView(); int dx = x - liv.x; int dy = y - liv.y; Point loc = getLocation(); setLocation(loc.x + dx, loc.y + dy); } public void delete(){ GVObject lp = getLooseParent(); if(lp != null){ lp.removeLoosePiece(this); } for(GVObject part : getLoosePieces()){ part.setLooseParent(null); } Container p = getParent(); if(p != null){ p.remove(this); p.repaint(); } } /* * This is called by a looseParent object as it drags aroung loosePieces. * So when this object is a loosePiece, this will be called. It is overriden * in subclasses that need to react in a special way. */ public void followDrag(int dX, int dY){ } /** * @return the root object that this object currently is contained within. * This will be one short of the View in the graphical parent chain.. * * This function is used for logical "groupings" of objects. Essentially, * a root object is the first object before the view. Note we allow for * non-GridView objects in the stack. */ public GVObject getRoot() { if( getParent() == null ) return this; /* * Search up the tree */ Component c = (Component)this; while( c.getParent() != null && !(c.getParent() instanceof GridView) ) { c = c.getParent(); } return (GVObject) c; } public GridView getView(){ GVObject root = getRoot(); if(root.getParent() == null){ return null; } return (GridView) root.getParent(); } public void setInitialPositionInView(GridView gv){ //This strategy is to try a bunch of positions from upper left //to lower right, looking for a non-overlapping location. int xi = Math.min(100, gv.getWidth()/4 ); int yi = Math.min(100, gv.getWidth()/4 ); int xT = xi; int yT = yi; boolean overlap = false; while(yT < gv.getHeight()){ while(xT < gv.getWidth() - getWidth() - 80){ overlap = false; for(GVObject g : gv.getGridObjects()){ if(g != this){ overlap = overlap || (g.getBounds().intersects(xT, yT, getWidth(), getHeight())); } } if(! overlap) break; xT = xT + getWidth() + 120; } if(! overlap) break; yT = yT + getHeight() + 80; xT = xi; } //If the screen is really crowded, (always have overlap) just go tho the middle: if(overlap) setLocation(gv.getWidth()/2 - getWidth()/2, gv.getHeight()/2 - getHeight()/2); setLocation(xT, yT); } /** * Hook called after entering a world. * * This may be called without necessary being added to a view. In particular, * this will be called after each object is moved and "let go" to fall back onto the Grid. */ public void justDroppedOntoGrid() { } /** * Hook for determining an object's depth location within a view. * @return a level in the view. */ public int getLayerInView() { return 5;} /** * Hook called to add this to a view. * NOTE: This is the proper way to add an object to the view. * Objects are expected to call view.add(...) within this method. * @param the view this is being added to */ public void addToView(GridView view) { synchronized( viewLock ) { view.add(this,0); validate(); //This is what made the tComponentHolder finally be fully visible. repaint(); viewLock.notifyAll(); } } /* * Invoked when the user "let's go" of this object. This defualt behavior is nothing special, * but override to get drag and DROP effects. Note this is only sent TO THE ONE OBJECT UNDeR THE MOUSE * See InvisoObjectHolder, where the originator is the thing the user grabbed. * */ public void dropIntoView(GridView gv){ addToView(gv); Vector<GVObject> lp = getAllLoosePieces(); for(GVObject g : lp){ g.addToView(gv); } } /** * Hook called to remove this from a world. * Remove this and all its loose peices, transitively. * Implementation calls getWorld().remove(this) */ public void removeFromView() { if (!isInView()) return; synchronized( viewLock ) { Vector<GVObject> lp = getLoosePieces(); for(GVObject p : lp){ p.removeFromView(); //note how this should recursively remove everyone.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -