Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/awt/CustomCursor.java
38827 views
/*1* Copyright (c) 1997, 1999, 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*/35public abstract class CustomCursor extends Cursor {3637protected Image image;3839public CustomCursor(Image cursor, Point hotSpot, String name)40throws IndexOutOfBoundsException {41super(name);42image = cursor;43Toolkit toolkit = Toolkit.getDefaultToolkit();4445// Make sure image is fully loaded.46Component c = new Canvas(); // for its imageUpdate method47MediaTracker tracker = new MediaTracker(c);48tracker.addImage(cursor, 0);49try {50tracker.waitForAll();51} catch (InterruptedException e) {52}53int width = cursor.getWidth(c);54int height = cursor.getHeight(c);5556// Fix for bug 4212593 The Toolkit.createCustomCursor does not57// check absence of the image of cursor58// If the image is invalid, the cursor will be hidden (made completely59// transparent). In this case, getBestCursorSize() will adjust negative w and h,60// but we need to set the hotspot inside the image here.61if (tracker.isErrorAny() || width < 0 || height < 0) {62hotSpot.x = hotSpot.y = 0;63}6465// Scale image to nearest supported size.66Dimension nativeSize = toolkit.getBestCursorSize(width, height);67if ((nativeSize.width != width || nativeSize.height != height) &&68(nativeSize.width != 0 && nativeSize.height != 0)) {69cursor = cursor.getScaledInstance(nativeSize.width,70nativeSize.height,71Image.SCALE_DEFAULT);72width = nativeSize.width;73height = nativeSize.height;74}7576// Verify that the hotspot is within cursor bounds.77if (hotSpot.x >= width || hotSpot.y >= height || hotSpot.x < 0 || hotSpot.y < 0) {78throw new IndexOutOfBoundsException("invalid hotSpot");79}8081/* Extract ARGB array from image.82*83* A transparency mask can be created in native code by checking84* each pixel's top byte -- a 0 value means the pixel's transparent.85* Since each platform's format of the bitmap and mask are likely to86* be different, their creation shouldn't be here.87*/88int[] pixels = new int[width * height];89ImageProducer ip = cursor.getSource();90PixelGrabber pg = new PixelGrabber(ip, 0, 0, width, height,91pixels, 0, width);92try {93pg.grabPixels();94} catch (InterruptedException e) {95}9697createNativeCursor(image, pixels, width, height, hotSpot.x, hotSpot.y);98}99100protected abstract void createNativeCursor(Image im, int[] pixels,101int width, int height,102int xHotSpot, int yHotSpot);103}104105106