001: /*
002: * Copyright 1997-1999 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt;
027:
028: import java.awt.*;
029: import java.awt.image.*;
030:
031: /**
032: * A class to encapsulate a custom image-based cursor.
033: *
034: * @version 1.21, 05/05/07
035: * @author ThomasBall
036: */
037: public abstract class CustomCursor extends Cursor {
038:
039: protected Image image;
040:
041: public CustomCursor(Image cursor, Point hotSpot, String name)
042: throws IndexOutOfBoundsException {
043: super (name);
044: image = cursor;
045: Toolkit toolkit = Toolkit.getDefaultToolkit();
046:
047: // Make sure image is fully loaded.
048: Component c = new Canvas(); // for its imageUpdate method
049: MediaTracker tracker = new MediaTracker(c);
050: tracker.addImage(cursor, 0);
051: try {
052: tracker.waitForAll();
053: } catch (InterruptedException e) {
054: }
055: int width = cursor.getWidth(c);
056: int height = cursor.getHeight(c);
057:
058: // Fix for bug 4212593 The Toolkit.createCustomCursor does not
059: // check absence of the image of cursor
060: // If the image is invalid, the cursor will be hidden (made completely
061: // transparent). In this case, getBestCursorSize() will adjust negative w and h,
062: // but we need to set the hotspot inside the image here.
063: if (tracker.isErrorAny() || width < 0 || height < 0) {
064: hotSpot.x = hotSpot.y = 0;
065: }
066:
067: // Scale image to nearest supported size.
068: Dimension nativeSize = toolkit.getBestCursorSize(width, height);
069: if (nativeSize.width != width || nativeSize.height != height) {
070: cursor = cursor.getScaledInstance(nativeSize.width,
071: nativeSize.height, Image.SCALE_DEFAULT);
072: width = nativeSize.width;
073: height = nativeSize.height;
074: }
075:
076: // Verify that the hotspot is within cursor bounds.
077: if (hotSpot.x >= width || hotSpot.y >= height || hotSpot.x < 0
078: || hotSpot.y < 0) {
079: throw new IndexOutOfBoundsException("invalid hotSpot");
080: }
081:
082: /* Extract ARGB array from image.
083: *
084: * A transparency mask can be created in native code by checking
085: * each pixel's top byte -- a 0 value means the pixel's transparent.
086: * Since each platform's format of the bitmap and mask are likely to
087: * be different, their creation shouldn't be here.
088: */
089: int[] pixels = new int[width * height];
090: ImageProducer ip = cursor.getSource();
091: PixelGrabber pg = new PixelGrabber(ip, 0, 0, width, height,
092: pixels, 0, width);
093: try {
094: pg.grabPixels();
095: } catch (InterruptedException e) {
096: }
097:
098: createNativeCursor(image, pixels, width, height, hotSpot.x,
099: hotSpot.y);
100: }
101:
102: protected abstract void createNativeCursor(Image im, int[] pixels,
103: int width, int height, int xHotSpot, int yHotSpot);
104: }
|