?? sprite.java
字號:
* before transformation. This corner may no longer appear as the * upper-left corner in the painter's coordinate system under * current transformation. * <p> * By default, a Sprite's reference pixel is located at (0,0); that is, * the pixel in the upper-left corner of the raw frame. * <p> * Changing the reference pixel does not change the * Sprite's physical position in the painter's coordinate system; * that is, the values returned by {@link #getX getX()} and * {@link #getY getY()} will not change as a result of defining the * reference pixel. However, subsequent calls to methods that * involve the reference pixel will be impacted by its new definition. * * @param x the horizontal location of the reference pixel, relative * to the left edge of the un-transformed frame * @param y the vertical location of the reference pixel, relative * to the top edge of the un-transformed frame * @see #setRefPixelPosition * @see #getRefPixelX * @see #getRefPixelY */ public void defineReferencePixel(int x, int y) { dRefX = x; dRefY = y; } /** * Sets this Sprite's position such that its reference pixel is located * at (x,y) in the painter's coordinate system. * @param x the horizontal location at which to place the reference pixel * @param y the vertical location at which to place the reference pixel * @see #defineReferencePixel * @see #getRefPixelX * @see #getRefPixelY */ public void setRefPixelPosition(int x, int y) { // update this.x and this.y this.x = x - getTransformedPtX(dRefX, dRefY, this.t_currentTransformation); this.y = y - getTransformedPtY(dRefX, dRefY, this.t_currentTransformation); } /** * Gets the horizontal position of this Sprite's reference pixel * in the painter's coordinate system. * @return the horizontal location of the reference pixel * @see #defineReferencePixel * @see #setRefPixelPosition * @see #getRefPixelY */ public int getRefPixelX() { return (this.x + getTransformedPtX(dRefX, dRefY, this.t_currentTransformation)); } /** * Gets the vertical position of this Sprite's reference pixel * in the painter's coordinate system. * @return the vertical location of the reference pixel * @see #defineReferencePixel * @see #setRefPixelPosition * @see #getRefPixelX */ public int getRefPixelY() { return (this.y + getTransformedPtY(dRefX, dRefY, this.t_currentTransformation)); } /** * Selects the current frame in the frame sequence. <p> * The current frame is rendered when {@link #paint(Graphics)} is called. * <p> * The index provided refers to the desired entry in the frame sequence, * not the index of the actual frame itself. * @param sequenceIndex the index of of the desired entry in the frame * sequence * @throws IndexOutOfBoundsException if <code>frameIndex</code> is * less than<code>0</code> * @throws IndexOutOfBoundsException if <code>frameIndex</code> is * equal to or greater than the length of the current frame * sequence (or the number of raw frames for the default sequence) * @see #setFrameSequence(int[]) * @see #getFrame */ public void setFrame(int sequenceIndex) { if (sequenceIndex < 0 || sequenceIndex >= frameSequence.length) { throw new IndexOutOfBoundsException(); } this.sequenceIndex = sequenceIndex; } /** * Gets the current index in the frame sequence. <p> * The index returned refers to the current entry in the frame sequence, * not the index of the actual frame that is displayed. * * @return the current index in the frame sequence * @see #setFrameSequence(int[]) * @see #setFrame */ public final int getFrame() { return sequenceIndex; } /** * Gets the number of raw frames for this Sprite. The value returned * reflects the number of frames; it does not reflect the length of the * Sprite's frame sequence. However, these two values will be the same * if the default frame sequence is used. * * @return the number of raw frames for this Sprite * @see #getFrameSequenceLength */ public int getRawFrameCount() { return numberFrames; } /** * Gets the number of elements in the frame sequence. The value returned * reflects the length of the Sprite's frame sequence; it does not reflect * the number of raw frames. However, these two values will be the same * if the default frame sequence is used. * * @return the number of elements in this Sprite's frame sequence * @see #getRawFrameCount */ public int getFrameSequenceLength() { return frameSequence.length; } /** * Selects the next frame in the frame sequence. <p> * * The frame sequence is considered to be circular, i.e. if * {@link #nextFrame} is called when at the end of the sequence, * this method will advance to the first entry in the sequence. * * @see #setFrameSequence(int[]) * @see #prevFrame */ public void nextFrame() { sequenceIndex = (sequenceIndex + 1) % frameSequence.length; } /** * Selects the previous frame in the frame sequence. <p> * * The frame sequence is considered to be circular, i.e. if * {@link #prevFrame} is called when at the start of the sequence, * this method will advance to the last entry in the sequence. * * @see #setFrameSequence(int[]) * @see #nextFrame */ public void prevFrame() { if (sequenceIndex == 0) { sequenceIndex = frameSequence.length - 1; } else { sequenceIndex--; } } /** * Draws the Sprite. * <P> * Draws current frame of Sprite using the provided Graphics object. * The Sprite's upper left corner is rendered at the Sprite's current * position relative to the origin of the Graphics object. The current * position of the Sprite's upper-left corner can be retrieved by * calling {@link #getX()} and {@link #getY()}. * <P> * Rendering is subject to the clip region of the Graphics object. * The Sprite will be drawn only if it is visible. * <p> * If the Sprite's Image is mutable, the Sprite is rendered using the * current contents of the Image. * * @param g the graphics object to draw <code>Sprite</code> on * @throws NullPointerException if <code>g</code> is <code>null</code> * */ public final void paint(Graphics g) { // managing the painting order is the responsibility of // the layermanager, so depth is ignored if (g == null) { throw new NullPointerException(); } if (visible) { // width and height of the source // image is the width and height // of the original frame g.drawRegion(sourceImage, frameCoordsX[frameSequence[sequenceIndex]], frameCoordsY[frameSequence[sequenceIndex]], srcFrameWidth, srcFrameHeight, t_currentTransformation, this.x, this.y, Graphics.TOP | Graphics.LEFT); } } /** * Set the frame sequence for this Sprite. <p> * * All Sprites have a default sequence that displays the Sprites * frames in order. This method allows for the creation of an * arbitrary sequence using the available frames. The current * index in the frame sequence is reset to zero as a result of * calling this method. * <p> * The contents of the sequence array are copied when this method * is called; thus, any changes made to the array after this method * returns have no effect on the Sprite's frame sequence. * <P> * Passing in <code>null</code> causes the Sprite to revert to the * default frame sequence.<p> * * @param sequence an array of integers, where each integer represents * a frame index * * @throws ArrayIndexOutOfBoundsException if seq is non-null and any member * of the array has a value less than <code>0</code> or * greater than or equal to the * number of frames as reported by {@link #getRawFrameCount()} * @throws IllegalArgumentException if the array has less than * <code>1</code> element * @see #nextFrame * @see #prevFrame * @see #setFrame * @see #getFrame * */ public void setFrameSequence(int sequence[]) { if (sequence == null) { // revert to the default sequence sequenceIndex = 0; customSequenceDefined = false; frameSequence = new int[numberFrames]; // copy frames indices into frameSequence for (int i = 0; i < numberFrames; i++) { frameSequence[i] = i; } return; } if (sequence.length < 1) { throw new IllegalArgumentException(); } for (int i = 0; i < sequence.length; i++) { if (sequence[i] < 0 || sequence[i] >= numberFrames) { throw new ArrayIndexOutOfBoundsException(); } } customSequenceDefined = true; frameSequence = new int[sequence.length]; System.arraycopy(sequence, 0, frameSequence, 0, sequence.length); sequenceIndex = 0; } /** * Changes the Image containing the Sprite's frames. * <p> * Replaces the current raw frames of the Sprite with a new set of raw * frames. See the constructor {@link #Sprite(Image, int, int)} for * information on how the frames are created from the image. The * values returned by {@link Layer#getWidth} and {@link Layer#getHeight} * will reflect the new frame width and frame height subject to the * Sprite's current transform. * <p> * Changing the image for the Sprite could change the number of raw * frames. If the new frame set has as many or more raw frames than the * previous frame set, then: * <ul> * <li>The current frame will be unchanged * <li>If a custom frame sequence has been defined (using * {@link #setFrameSequence(int[])}), it will remain unchanged. If no * custom frame sequence is defined (i.e. the default frame * sequence * is in use), the default frame sequence will be updated to * be the default frame sequence for the new frame set. In other * words, the new default frame sequence will include all of the * frames from the new raw frame set, as if this new image had been * used in the constructor. * </ul> * <p> * If the new frame set has fewer frames than the previous frame set, * then: * <ul> * <li>The current frame will be reset to entry 0 * <li>Any custom frame sequence will be discarded and the frame sequence * will revert to the default frame sequence for the new frame * set. * </ul> * <p> * The reference point location is unchanged as a result of calling this * method, both in terms of its defined location within the Sprite and its * position in the painter's coordinate system. However, if the frame * size is changed and the Sprite has been transformed, the position of * the Sprite's upper-left corner may change such that the reference * point remains stationary. * <p> * If the Sprite's frame size is changed by this method, the collision * rectangle is reset to its default value (i.e. it is set to the new * bounds of the untransformed Sprite). * <p> * @param img the <code>Image</code> to use for * <code>Sprite</code> * @param frameWidth the width in pixels of the individual raw frames * @param frameHeight the height in pixels of the individual raw frames * @throws NullPointerException if <code>img</code> is <code>null</code> * @throws IllegalArgumentException if <code>frameHeight</code> or * <code>frameWidth</code> is less than <code>1</code> * @throws IllegalArgumentException if the image width is not an integer * multiple of the <code>frameWidth</code> * @throws IllegalArgumentException if the image height is not an integer * multiple of the <code>frameHeight</code> */ public void setImage(Image img, int frameWidth, int frameHeight) { // if image is null image.getWidth() will throw NullPointerException if ((frameWidth < 1 || frameHeight < 1) || ((img.getWidth() % frameWidth) != 0) || ((img.getHeight() % frameHeight) != 0)) { throw new IllegalArgumentException(); } int noOfFrames = (img.getWidth() / frameWidth)*(img.getHeight() / frameHeight); boolean maintainCurFrame = true; if (noOfFrames < numberFrames) { // use default frame , sequence index = 0 maintainCurFrame = false; customSequenceDefined = false; } if (! ((srcFrameWidth == frameWidth) && (srcFrameHeight == frameHeight))) { // computing is the location // of the reference pixel in the painter's coordinate system. // and then use this to find x and y position of the Sprite int oldX = this.x + getTransformedPtX(dRefX, dRefY, this.t_currentTransformation); int oldY = this.y + getTransformedPtY(dRefX, dRefY, this.t_currentTransformation); setWidthImpl(frameWidth); setHeightImpl(frameHeight); initializeFrames(img, frameWidth, frameHeight, maintainCurFrame); // initialize collision rectangle initCollisionRectBounds(); // set the new x and y position of the Sprite this.x = oldX - getTransformedPtX(dRefX, dRefY, this.t_currentTransformation);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -