Path: blob/master/src/java.desktop/share/classes/sun/awt/CustomCursor.java
41152 views
/*1* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt;2627import java.awt.*;28import java.awt.image.*;2930/**31* A class to encapsulate a custom image-based cursor.32*33* @author ThomasBall34*/35@SuppressWarnings("serial") // JDK-implementation class36public abstract class CustomCursor extends Cursor {3738protected Image image;3940public CustomCursor(Image cursor, Point hotSpot, String name)41throws IndexOutOfBoundsException {42super(name);43image = cursor;44Toolkit toolkit = Toolkit.getDefaultToolkit();4546// Make sure image is fully loaded.47Component c = new Canvas(); // for its imageUpdate method48MediaTracker tracker = new MediaTracker(c);49tracker.addImage(cursor, 0);50try {51tracker.waitForAll();52} catch (InterruptedException e) {53}54int width = cursor.getWidth(c);55int height = cursor.getHeight(c);5657// Fix for bug 4212593 The Toolkit.createCustomCursor does not58// check absence of the image of cursor59// If the image is invalid, the cursor will be hidden (made completely60// transparent). In this case, getBestCursorSize() will adjust negative w and h,61// but we need to set the hotspot inside the image here.62if (tracker.isErrorAny() || width < 0 || height < 0) {63hotSpot.x = hotSpot.y = 0;64}6566// Scale image to nearest supported size.67Dimension nativeSize = toolkit.getBestCursorSize(width, height);68if ((nativeSize.width != width || nativeSize.height != height) &&69(nativeSize.width != 0 && nativeSize.height != 0)) {70cursor = cursor.getScaledInstance(nativeSize.width,71nativeSize.height,72Image.SCALE_DEFAULT);73width = nativeSize.width;74height = nativeSize.height;75}7677// Verify that the hotspot is within cursor bounds.78if (hotSpot.x >= width || hotSpot.y >= height || hotSpot.x < 0 || hotSpot.y < 0) {79throw new IndexOutOfBoundsException("invalid hotSpot");80}8182/* Extract ARGB array from image.83*84* A transparency mask can be created in native code by checking85* each pixel's top byte -- a 0 value means the pixel's transparent.86* Since each platform's format of the bitmap and mask are likely to87* be different, their creation shouldn't be here.88*/89int[] pixels = new int[width * height];90ImageProducer ip = cursor.getSource();91PixelGrabber pg = new PixelGrabber(ip, 0, 0, width, height,92pixels, 0, width);93try {94pg.grabPixels();95} catch (InterruptedException e) {96}9798createNativeCursor(image, pixels, width, height, hotSpot.x, hotSpot.y);99}100101protected abstract void createNativeCursor(Image im, int[] pixels,102int width, int height,103int xHotSpot, int yHotSpot);104}105106107